我在这里做的是生成PRTG在运行脚本时可以解析的XML输出。该脚本旨在确定最后一次安装Windows Update的时间。它适用于所有远程服务器,但当它在本地计算机上运行时,它不会获得正确的$值。它最终是空的,而不是一个整数。我想我在这里错过了一些关于Invoke-Command如何在本地服务器和远程服务器上工作的东西。有人会介意让我知道我犯了什么错误吗?所有的Invoke-Command脚本块从远程服务器返回变量,但不从本地服务器返回变量
$ErrorActionPreference = "Stop"
#Get a list of servers
$servers = Get-ADComputer -SearchBase 'DC=<removed>,DC=int' -Filter {OperatingSystem -NotLike "Windows Server 2003*"} | Sort Name | Select -ExpandProperty Name
$value = ""
#This is the start of the XML output that PRTG will be parsing when the code runs
Write-Host "<prtg>"
#Loop through all servers and attempt to get a value for last windows update install.
foreach($server in $servers) {
Write-Host "`t<result>`n`t<channel> $server </channel>"
try {
Invoke-Command -ComputerName $server -ScriptBlock {
$props = @{
LastDetect = Get-ItemProperty -Path ‘HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto Update\Results\Detect’ -Name LastSuccessTime | select -ExpandProperty LastSuccessTime
LastInstall = Get-ItemProperty -Path ‘HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto Update\Results\Install’ -Name LastSuccessTime | select -ExpandProperty LastSuccessTime
}
$stringdato = $props.LastInstall;
$DATO = ([datetime]::ParseExact($stringdato, "yyyy-MM-dd HH:mm:ss", $null))
$today = (Get-Date)
$timeout = ($today - $DATO)
$value = $timeout.Days
}
} catch {
# set value to 999 if there is a problem, for PRTG's error threshold.
$value = 999
}
Write-Host "`t<value>$value</value>"
Write-Host "`t<CustomUnit>days</CustomUnit>`n`t<LimitMaxError>90</LimitMaxError>`n`t<LimitMaxWarning>60</LimitMaxWarning>`n`t<LimitMode>1</LimitMode>`n`t</result>`n"
}
Write-Host "</prtg>"
这没有什么错远程到本地系统的作为概念。可能会出现这种情况,这并不合理,但没有关于远程访问本地计算机的具体内容。还有一些情况很有意义。 – briantist
@briantist:如果它在那里,那么你必须将本地系统添加到TrustedHosts列表中,并且我相信验证应该有所帮助。 –
这不一定是真的。远程处理需要启用,但域环境中不可信的主机。 – briantist