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?