Hi,
I want to use powershell to query all of our servers from a input file to check the state of the SNMP service.
There can be two states stopped or running, if the state is stopped I want to start the SNMP service.
All the checks need to be logged to a log file.
Can you help me with this and also explain.
Thanks in advance.
Remco
Hi
This should do what you want
function test-service{ [CmdletBinding(SupportsShouldProcess=$true)] param ( [parameter(Position=0, ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true)] [Alias("CN", "ComputerName")] [string]$computer="." ,
[string]$service="BITS" , [parameter(Mandatory=$true)] [string]$file ) BEGIN { if (!(Test-Path -Path $file)){Throw "log file not found"} } PROCESS { $result = Get-WmiObject -Class Win32_Service -ComputerName $computer -Filter "Name='$service'" Write-Verbose "$($result.State)" if ($($result.State) -eq "Running"){ $logdata = "{0,14} {1} {2}" -f $computer, $($result.State), (get-date -Format f ) } else{ $start = $result.StartService() if ($start.ReturnValue -eq 0){ $logdata = "{0,14} {1} {2}" -f $computer, "Service Started", (get-date -Format f ) } else { $logdata = "{0,14} {1} {2}" -f $computer, "Service FAILED to Start", (get-date -Format f ) } } Add-Content -Path $file -Value $logdata } } ## end function
use it like this
Import-Csv computers.csv | test-service -service BITS -file log.txt
the CSV file has a single field called Computer
Need to define the log file AND it needs to exist - create an empty file with notepad
The file path is tested. The call is made to Win32_service and the service started if it isn't running.
Data is added to the log file.
A couple of ideas to improve this:
I took the following steps to get the script to work.
1. I created a CSV file, with collumn Computer with in each row under the collumn a server name. Currently two servers are added, one server with the SNMP service stopped and one server with the service enabled.
2. Created a logfile.
3. Added path to log file to function. $file = path to log file
When i run the script with the following command:
Import-Csv computers.csv | test-service -service SNMP -file log.txt
The following is shown in the log, the script just keeps running.
@{computers=OMS1001} Service FAILED to Start woensdag 31 augustus 2011 9:38@{computers=RMS1001} Service FAILED to Start woensdag 31 augustus 2011 9:38 @{computers=} Service FAILED to Start woensdag 31 augustus 2011 9:38 @{computers=} Service FAILED to Start woensdag 31 augustus 2011 9:38 @{computers=} Service FAILED to Start woensdag 31 augustus 2011 9:38 @{computers=} Service FAILED to Start woensdag 31 augustus 2011 9:38 @{computers=} Service FAILED to Start woensdag 31 augustus 2011 9:38 @{computers=} Service FAILED to Start woensdag 31 augustus 2011 9:38 @{computers=} Service FAILED to Start woensdag 31 augustus 2011 9:38 @{computers=} Service FAILED to Start woensdag 31 augustus 2011 9:38
The following is shown, when function is called with -Whatif
What if: Performing operation "Add Content" on Target "Path: D:\scripts\SNMP\log.txt".Get-WmiObject : De RPC-server is niet beschikbaar. (Uitzondering van HRESULT: 0x800706BA)At line:19 char:25+ $result = Get-WmiObject <<<< -Class Win32_Service -ComputerName $computer -Filter "Name='$service'" + CategoryInfo : InvalidOperation: (:) [Get-WmiObject], COMException + FullyQualifiedErrorId : GetWMICOMException,Microsoft.PowerShell.Commands.GetWmiObjectCommand You cannot call a method on a null-valued expression.At line:27 char:30+ $start = $result.StartService <<<< () + CategoryInfo : InvalidOperation: (StartService:String) [], RuntimeException + FullyQualifiedErrorId : InvokeMethodOnNull
Two questions: 1. The script keeps running as seen in the log file, it runs against no computer. HOw come? 2. The check and start commands are not functioning, I run the script as Domain admin?
Two questions:
1. The script keeps running as seen in the log file, it runs against no computer. HOw come?
2. The check and start commands are not functioning, I run the script as Domain admin?
When I run the following command in PS ISE, it works well
result = Get-WmiObject -Class Win32_Service -ComputerName Servername -Filter "Name='SNMP'"
Check that the computers listed in your csv file actually exist