Stop Different Services On Different Remote Computers

Tagged: ,

This topic contains 3 replies, has 2 voices, and was last updated by Profile photo of Daniel Krebs Daniel Krebs 2 years, 1 month ago.

  • Author
    Posts
  • #24563
    Profile photo of Tee
    Tee
    Participant

    Hello all, please forgive me but I'm new to Powershell and I'm still in the early learning phase. I have assigned myself a project and seem to be stuck. My goal is to stop a service on different remote computers using the Credential flag. So, I'm requiring the user to enter in the password for the account only once. Note, my machine has ps3 installed while the remote computer(s) has ps2 installed. So far I wrote a script in the powershell IDE but it seems to only be stopping the services on my local computer. Here is the code.

    $computernames = 'computerA','localhost'
    $servicestostop = 'BITS','Apple Mobile Device'
    
    $session = New-PSSession -ComputerName $computernames -Credential mydomain\me
    
    #display the sessions
    Get-PSSession
    
    for($i = 0; $i -lt $computernames.count; $i++)
    {
       Enter-PSSession -Session $session[$i]
       Stop-Service -Name $servicetostop[$i] -PassThru
       Exit-PSSession
       Remove-PSSession -Session $session[$i]
    }
    
    Write-Host 'Completed...'
    

    As I said before, when running this it's disabling both services on my localhost and nothing on ComputerA. It should be only disabling the 'Apple Mobile Device' service on the first localhost. I ran each statement separately except I didn't implement the for loop I pretty much just ran it from the cmd window. Can someone point me in the errors of my ways?

  • #24566
    Profile photo of Daniel Krebs
    Daniel Krebs
    Moderator

    Hi Tee,

    Thanks for reaching out. You're on the right track. A few things:

    – Enter-PSSession is for interactive usage. Invoke-Command is the correct cmdlet for this situation.
    – Invoke-Command supports parallel execution. Just give it all the sessions and it will execute/invoke your commands in parallel.

    Improved version of script. Not tested because I am answering this on my phone but it should work.

    $computernames = 'computerA','localhost'
    $servicestostop = 'BITS','Apple Mobile Device'
    
    $session = New-PSSession -ComputerName $computernames -Credential mydomain\me
    
    #display the sessions
    Get-PSSession
    
    $Results = Invoke-Command -Session $session -ScriptBlock {
        Stop-Service -Name $Using:servicestostop -PassThru
    }
    
    Remove-PSSession -Session $session
    
    $Results
    
    Write-Host 'Completed...'
    

    Best,
    Daniel

  • #24575
    Profile photo of Tee
    Tee
    Participant

    Thanks Daniel for the kind reply. I'll give this a try. Can you explain the `$Using:servicestostop` syntax. I do some coding in C# and my first thought is that the $Using is used exit out of the session.

    Thanks

  • #24703
    Profile photo of Daniel Krebs
    Daniel Krebs
    Moderator

    $Using is a scope modifier in PowerShell. It tells the PowerShell engine to look outside of the scriptblock { Stop-Service -Name $Using:servicestostop -PassThru } for the variable to inject the value.

    Check out the documentation here https://technet.microsoft.com/en-us/library/jj149005.aspx in the section for "Using local variables".

You must be logged in to reply to this topic.