1

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?

0

1 Answer 1

2

You will have to query the service each time you want to get the status using the second line of your script. Methods are stripped when it is serialized, which happens when the objects get returned from the remote server. Each time you want to get the current status of that service you will need to run:

$server1 = Invoke-Command -ComputerName xxxxxxxxx -ArgumentList $servicename -ScriptBlock {Param($servicename) Get-Service -name $servicename} -Credential $cred

Or put that all in one scriptblock, and do it all on the remote server.

$cred = Import-Clixml -path F:\Powershell\Safe\xxxxxxxx.txt 

$SBlock = {
    Param($servicename)

    $Service = Get-Service -name $servicename
    if ($Service.Status -eq "Running"){
        "The Telephony service is started on xxxxxxxxx"
    }
    Else{
        "The Telephony service is stopped on xxxxxxxxx, starting up service"
        start-sleep -seconds 5
        Start-Service -name $servicename
        "Telephony service is starting xxxxxxxxx"
        start-sleep -seconds 10
        $Service.Refresh()
        if ($server1.status -eq "Running"){
            "Telephony service is now started on xxxxxxxxx"
        }
        else {
            "The Telephony service failed to start on xxxxxxxxx please check services and try again."
        }
    }
}

Invoke-Command -ComputerName xxxxxxxxx -ArgumentList $servicename -ScriptBlock $SBlock -Credential $cred
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.