Hi guys,
I am writing a PowerShell script which among other things will need to stop some services. These services can run on different server, and the script reads the server names from a config file.
My problem comes when I want to stop the services. I can stop a single service no problem (after having stolen most of the script from Scripting Guy), and could in theory just copy-paste my code x number of times while editing the appropriate variables, but I want to try my hand at using a foreach loop with an array.
If I just had to stop the services on a single server, I could just run the code below (Where I stop the services on the machine servername). However, the computername of each an every service varies, and is read from the config file.
How would I go about doing this? Any ideas?
I know I should make it a variable, and probably store the computernames from the config file in an array, but that is as far as I get.
Hope I made my problem clear enough!
I am writing a PowerShell script which among other things will need to stop some services. These services can run on different server, and the script reads the server names from a config file.
My problem comes when I want to stop the services. I can stop a single service no problem (after having stolen most of the script from Scripting Guy), and could in theory just copy-paste my code x number of times while editing the appropriate variables, but I want to try my hand at using a foreach loop with an array.
If I just had to stop the services on a single server, I could just run the code below (Where I stop the services on the machine servername). However, the computername of each an every service varies, and is read from the config file.
How would I go about doing this? Any ideas?
I know I should make it a variable, and probably store the computernames from the config file in an array, but that is as far as I get.
Hope I made my problem clear enough!
Code:
function shutdownservices {
#Shutdown services (script stolen from Scripting Guy - http://blogs.technet.com/b/heyscriptingguy/archive/2009/10/01/hey-scripting-guy-october-1-2009.aspx
$logfilename = "c:\pstest\arraytest.txt"
#Create array
$aryServices = "hub", "biz"
#Shutdown services
foreach ($strService in $aryServices)
{
$strClass = "win32_service"
$objWmiService = Get-Wmiobject -Class $strClass -computer servername # -----------------------------Here Is My Problem
-filter "name = '$strService'"
if( $objWMIService.Acceptstop )
{
$rtn = $objWMIService.stopService()
Switch ($rtn.returnvalue)
{
0 { $ErrorCodeBiz = "$strService stopped" }
2 { $ErrorCodeBiz = "$strService service reports access denied" }
5 { $ErrorCodeBiz = "$strService service cannot accept control at this time" }
10 { $ErrorCodeBiz = "$strService service is already stopped" }
DEFAULT { $ErrorCodeBiz = "$strService service reports ERROR $($rtn.returnValue)" }
}
}
ELSE
{
$ErrorCodeBiz = "$strService will not accept a stop request"
}
#Write to logfile
Add-Content $logfilename "Stopping $strService"
Add-Content $logfilename "$ErrorCodeBiz"
}
}
shutdownservices