Here is my expectation:
I have to write a script to start/stop services that are running on windows 2003 virtual machines. eg., spoolsv.exe, rtvscan.exe.
I believe that the versions of my VC, ESX is not needed to mention here. All I am expecting from this place is, simple script that makes my job easier. It would be most appreciated, If anyone give me an exact script that fullfill my expectation or some useful links or any inputs.
Thanks,
Ganesh
You can use the Stop-Service and Start-Service cmdlets.
There are plenty of examples available on the Internet, this is just one Start Windows Services with PowerShell
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Is there a special requirement why you want to do this using PowerCLI? If not, the Windows sc command line tool can do this too.
André
There isn't a special requirement to do this using PowerCLI. I never heard about windows SC command, thats the reason why I am looking for a automated process using PowerCLI.
Once I googled about SC command, found that the windows SC command will fullfill my expectation. Will let you know the status. Thanks Anyway ! ! !
Also I believed that I could get some useful information to my requirement from this place. You guys, proved that this the place where all can get immediate solution. Hats Off ! ! !
Hi Ganesh,
Once i get Into the office I will post the script that I use.
Meanwhile feel free to share your SC command powershell script here too
Kind Regards,
Albert Widjaja T.
Hi Albert,
SC command is not related to PowerCLI script. SC command will work on windows command prompt or you can integrate the same command in PowerShell. Right now, I dont have any script to manage services running on VM.
Desperately waiting for your turn. Why not you go to office on Sunday??? ha ha ha . . .
Here it is Ganesh,
Monday morning from the office:
$path = "C:\tmp\"$serverlist = get-content "$path\serverlist.txt"$servicename = "HPAgent"foreach ($server in $serverlist){$result = Test-Connection $server -Count 1 -Quietif ($result -eq "True") {$servicetorestart = Get-Service -name $servicename -ComputerName $server -ErrorAction SilentlyContinueif ($servicetorestart.Name -ne $servicename) {Add-Content "$path\noservice.txt" -Value ("$server" + " " + "Service $servicename is not present")}else {sc.exe \\$server stop "$servicename"Start-Sleep -Seconds 5sc.exe \\$server start "$servicename"Add-Content "$path\ServiceRestarted.txt" -Value $server}}else {Add-Content "$path\NotRespondingServers.txt" -Value $server}}
You can also change the line:
$serverlist = get-content "$path\serverlist.txt"
with:
$serverlist = get-vm
to make it working.
Hope that can be the solution for you Ganesh.
Cheers,
Albert