This script is supposed to check if a service is started on server1 and to start it if it is not.
$cred = Import-Clixml -Path F:\Powershell\Safe\xxxx.txt
$server1 = Invoke-Command -ComputerName xxxx -ArgumentList $servicename -ScriptBlock {
Param($servicename)
Get-Service -Name $servicename
} -Credential $cred
if ($server1.Status -eq "Running"){
Write-Host "The Telephony service is started on xxxx"
} else {
Write-Host "The Telephony service is stopped on xxxx, starting up service"
Start-Sleep -Seconds 5
Invoke-Command -ComputerName xxxx -ArgumentList $servicename -ScriptBlock {
Param($servicename)
Start-Service -Name $servicename
} -Credential $cred
Write-Host "Telephony service is starting xxxx"
Start-Sleep -Seconds 10
$server1.Refresh()
if ($server1.status -eq "Running") {
Write-Host "Telephony service is now started on xxxx"
} else {
Write-Host "The Telephony service failed to start on xxxx please check services and try again."
I get an error stating:
Method invocation failed because [Deserialized.System.ServiceProcess.ServiceController] does not contain a method named 'refresh'
However, when working when using the $server.Refresh() command on a local service and not a remote PC it works just fine. How do I refresh my variable for the service status on a remote PC?