Skip to content

Instantly share code, notes, and snippets.

@ddhahn
Created February 1, 2013 17:01
Show Gist options
  • Save ddhahn/4692584 to your computer and use it in GitHub Desktop.
Save ddhahn/4692584 to your computer and use it in GitHub Desktop.
Stop a few services on a list of computers
function WriteLog($info, $errormsg=$false)
{
#Funtion will write log entries along with a time and date stamp
$nowdate = date -format G
if ($errormsg) {
Write-Host "[$nowdate] $info" -foregroundcolor Red
}
else {
Write-Host "[$nowdate] $info"
}
"[$nowdate] $info" | out-file "c:\temp\newflex\StopFLMM.txt" -enc ASCII -append
}
#load the correct assembly so we can use the serviceprocess object
[void][reflection.assembly]::loadwithpartialname("system.serviceprocess")
#serversfile is a text file containing the list of servers to be updated. All
#server names are separated by CRLF
$serversfile = "C:\temp\newflex\servers.txt"
#get the contents of the servers file
$servers = get-content($serversfile)
foreach ($server in $servers)
{
writelog "-------------------------------"
#verify that the server is up by pinging it
$PingStatus = Gwmi Win32_PingStatus -Filter "Address = '$server'" | Select-Object StatusCode
#if the statuscode property is 0, the ping succeeded.
if ($PingStatus.StatusCode -eq 0) {
WriteLog "Getting handle to Local Flexlm Service on $server"
#get a handle to the flexlmmonitor service on the remote server
$svcobj = new-object system.serviceprocess.servicecontroller('flexlmmonitor',$server)
$svcobjflm = new-object system.serviceprocess.servicecontroller('Local autodesk flexlm',$server)
$svcobj.Refresh()
$svcobjflm.Refresh()
set-service -name 'flexlmmonitor' -computername $server -startuptype Manual #start autodesk flexlm service
set-service -name 'local autodesk flexlm' -computername $server -startuptype Automatic #start autodesk flexlm service
#if the service isn't stopped, stop it before attempting to overwrite it.
if ($svcobj.status -ne 'Stopped'){
$svcobj.Stop() #stop the service
$svcobj.WaitForStatus('Stopped',(new-timespan -seconds 10)) #wait for the status
$svcobj.Refresh()
#check to see if the service is stopped OK.
if ($svcobj.Status -eq 'Stopped') { #we stopped the service successfully
writelog "Stopped FLMM on $server"}
#make sure the service is set to manual
}
else {writelog "FLMM was already stopped on $server"}
if ($svcobjflm.Status -eq 'Stopped') { #start the service
$svcobjflm.Start()
$svcobjflm.WaitForStatus('Running',(new-timespan -seconds 10)) #wait for the status
$svcobjflm.Refresh()
if ($svcobjflm.Status -eq 'Running') { #start the service
writelog "Started FlexLM Service OK on $server"}
}
else {writelog "FlexLM Service was already started on $server"}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment