Pumpernickel
asked on
Powershell Restart Service
I'm trying to do this command from a remote server, but I keep getting any error. I tried adding the remote machine as a trusted host, but that still doesn't work.
$session = New-PSsession -Computername "123.123.123.123"
Invoke-Command -Session $Session -ScriptBlock {Restart-Service "SERVICENAMEHERE"}
Remove-PSSession $Session
failed with the following error message : The WinRM client cannot
process the request. Default authentication may be used with an IP address under the following conditions: the transport is HTTPS or the
destination is in the TrustedHosts list, and explicit credentials are provided. Use winrm.cmd to configure TrustedHosts. Note that computers in
the TrustedHosts list might not be authenticated. For more information on how to set TrustedHosts run the following command: winrm help config.
For more information, see the about_Remote_Troubleshooti ng Help topic.
$session = New-PSsession -Computername "123.123.123.123"
Invoke-Command -Session $Session -ScriptBlock {Restart-Service "SERVICENAMEHERE"}
Remove-PSSession $Session
failed with the following error message : The WinRM client cannot
process the request. Default authentication may be used with an IP address under the following conditions: the transport is HTTPS or the
destination is in the TrustedHosts list, and explicit credentials are provided. Use winrm.cmd to configure TrustedHosts. Note that computers in
the TrustedHosts list might not be authenticated. For more information on how to set TrustedHosts run the following command: winrm help config.
For more information, see the about_Remote_Troubleshooti
Have you tried putting authentication details into the command?
No real need for a remote session here; Restart-Service doesn't have its own -ComputerName argument, but it accepts a service object retrieved from a remote machine with Get-Service:
Restart-Service -InputObject (Get-Service -Name "SERVICENAMEHERE" -ComputerName "123.123.123.123")
ASKER
oBdA, where can I put auth details?
ASKER
Also how can I make that so someone can just double click on it and it runs. Right now if I save it as a ps1, it just opens in notepad
Save this as Whatever.cmd. It's PowerShell at heart, but wrapped in batch.
@PowerShell.exe -Command "Invoke-Expression -Command ((Get-Content -Path '%~f0' | Select-Object -Skip 3) -join [environment]::NewLine)"
@if errorlevel 1 pause
@exit /b
$ServiceName = 'Spooler'
$UserName = 'SomeUser'
$ComputerName = 'SomeComputer'
$Credential = Get-Credential -Message "Credentials to restart '$($ServiceName)' on '$($ComputerName)'" -UserName $UserName
$TimeOut = 120 ## seconds
## 'Win32_Service Methods', https://msdn.microsoft.com/en-us/library/gg196691(v=vs.85).aspx
$gwmiArgs = @{
'Credential' = $Credential
'ComputerName' = $ComputerName
'Filter' = "Name='$($ServiceName)'"
'Class' = 'Win32_Service'
}
$Service = Get-WmiObject @gwmiArgs -ErrorAction Stop
If (-not $Service) {
Throw "Service '$($ServiceName)' not found on '$($ComputerName)'!"
}
$StopDate = (Get-Date).AddSeconds($TimeOut)
"Stopping '$($ServiceName)' on '$($ComputerName)' ." | Write-Host -ForegroundColor White -NoNewline
$Result = $Service.StopService()
If (0, 5 -notcontains $Result.ReturnValue) {
"" | Write-Host
Throw "ReturnValue $($Result.ReturnValue) trying to stop the service '$($ServiceName)'!"
}
Do {
"." | Write-Host -ForegroundColor White -NoNewline
Start-Sleep -Seconds 2
$Service = Get-WmiObject @gwmiArgs
} Until (($Service.State -eq 'Stopped') -or ((Get-Date) -gt $StopDate))
If ($Service.State -ne 'Stopped') {
"" | Write-Host
Throw "Unable to stop the service '$($ServiceName)'!"
} Else {
" OK." | Write-Host -ForegroundColor Green
}
$StopDate = (Get-Date).AddSeconds($TimeOut)
"Starting '$($ServiceName)' on '$($ComputerName)' ." | Write-Host -ForegroundColor White -NoNewline
$Result = $Service.StartService()
If (0 -notcontains $Result.ReturnValue) {
"" | Write-Host
Throw "ReturnValue $($Result.ReturnValue) trying to start the service '$($ServiceName)'!"
}
Do {
Start-Sleep -Seconds 2
$Service = Get-WmiObject @gwmiArgs
} Until (($Service.State -eq 'Running') -or ((Get-Date) -gt $StopDate))
If ($Service.State -ne 'Running') {
"" | Write-Host
Throw "Unable to start the service '$($ServiceName)'!"
} Else {
" OK." | Write-Host -ForegroundColor Green
}
Start-Sleep -Seconds 5
ASKER
oBdA, this would work perfect, but I need a way to store the password also. Any ideas?
What's the exact purpose of this script, that is, who will it run where and why?
ASKER
It will be pushed via Citrix to a client who needs to restart a service every once in a while for their application
ASKER
Any ideas?
How savvy is this client? The problem with this is that the password can be revealed with little effort.
What is "once in a while", and why?
What is "once in a while", and why?
ASKER
I'm not worried about the password being revealed as the service has special permissions allowing it to be controlled by only that account. That special account can't do anything else.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.