无法使用Powershell从远程服务器获取IIS状态
我正在尝试使用powershell从远程服务器获取IIS状态。 我使用了命令Get Service,但是我没有从这个命令收到任何输出。 下面是我的代码块无法使用Powershell从远程服务器获取IIS状态,powershell,Powershell,我正在尝试使用powershell从远程服务器获取IIS状态。 我使用了命令Get Service,但是我没有从这个命令收到任何输出。 下面是我的代码块 $pass='pass'|ConvertTo-SecureString -AsPlainText -Force; $Credentials = New-Object System.Management.Automation.PsCredential("user",$pass); $Service=invoke-command -comp
$pass='pass'|ConvertTo-SecureString -AsPlainText -Force;
$Credentials = New-Object
System.Management.Automation.PsCredential("user",$pass);
$Service=invoke-command -computername "server" -credential $Credentials -
scriptblock {Get-Service|Where-Object Name -eq 'IISADMIN'}
if($Service.Status -eq 'Running')
{
write-host "IIS Running"
}
else
{
throw "IIS not running or Not installed"
}
我检查了您的代码,没有发现任何问题,您是否使用
Get service
或service Manager检查了本地存在的服务
无论如何,您不必使用Invoke命令
为此,您可以使用Get-Service
cmdlet的内置-ComputerName
参数
如果需要提供凭据,可以使用WMI:
$Credential = New-Object System.Management.Automation.PsCredential($username, $encrypted)
$Service = Get-WmiObject -Class win32_service -ComputerName Server -Filter 'Name = "W3SVC"' -Credential $Credentials
尝试改用WMI调用,我发现它在处理远程服务器时更加可靠
$Service=Get WmiObject-Computername$computer-credentials$credentials Win32_Service-ErrorAction Continue |其中{$\uu.Name-eq'IISADMIN'}什么是Windows服务器版本?如果它是Win2008R2,IIS服务实际上被称为:W3SVC(万维网发布服务)-只需查找此服务,而不是“IISADMIN”