How to get the service status for a remote computer that needs a user name and password to log in?
I am trying to find a solution using the following code:
Invoke-Command
can accomplish this.
$Cred = Get-Credential -Credential ""
$ScriptBlock = {
param($Service)
Get-Service -Name "$Service"
}
$ServiceStatus = Invoke-Command -ComputerName $MachineName -Credential $Cred -ScriptBlock $ScriptBlock -ArgumentList "$Service"
Note that in order to pass the variable $Service
to the Get-Service
cmdlet in the script block, you have to declare the script block beforehand and define the $Service
variable in the param
block. You can then pass the $Service
argument into the script block via the -ArgumentList
parameter.
Invoke-Command
also supports running on multiple servers by supplying a comma delimited list of computers for the -ComputerName
parameter.