我在這裏做的是生成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