0
我試圖做一個腳本來檢查網絡上的計算機多長時間,如果他們在10天以上,他們需要重新啓動。我打算每個星期天都使用任務管理器自動運行腳本。使用powershell重新啓動計算機如果-LastBootupTime = -gt 10天
感謝@vonPryz我有現在這樣的事情:
$clients = get-content "C:\Documents\lijstcomputers.txt"
foreach ($client in $clients) {
if (test-connection -computername $client -BufferSize 16 -Count 1 -Quiet) {
write-Host $client is online
$uptime = (get-date) - (gcim Win32_OperatingSystem -computer $client).LastBootUpTime
$startTime = [Management.ManagementDateTimeConverter]::ToDateTime((gwmi Win32_OperatingSystem -computer $client).lastbootuptime)
if($uptime.days -ge 10) {
restart-computer -computername $client
add-content -path "c:\path\to\log.txt" -value "$client, $startTime, $uptime"
}
}
else {
write-Host $client is offline
}
}
但現在我得到這個錯誤:
gcim : WinRM cannot complete the operation. Verify that the specified computer name is valid, that the computer is accessible over the network, and
that a firewall exception for the WinRM service is enabled and allows access from this computer. By default, the WinRM firewall exception for publ
ic profiles limits access to remote computers within the same local subnet.
At line:1 char:25
+ $uptime = (get-date) - (gcim Win32_OperatingSystem -computer $client).LastBootUp ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ConnectionError: (root\cimv2:Win32_OperatingSystem:String) [Get-CimInstance], CimException
+ FullyQualifiedErrorId : HRESULT 0x80338126,Microsoft.Management.Infrastructure.CimCmdlets.GetCimInstanceCommand
+ PSComputerName : ASND0042
Cannot find an overload for "op_Subtraction" and the argument count: "2".
At line:1 char:1
+ $uptime = (get-date) - (gcim Win32_OperatingSystem -computer $client).LastBootUp ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodException
+ FullyQualifiedErrorId : MethodCountCouldNotFindBest
那麼問題是什麼?如何啓動電腦?如何將結果寫入文件?還有別的嗎? – vonPryz
您可能想要查看導入/導出爲CSV(使用'Get-Help * csv *'),或者您想更正代碼中的錯誤(您可以指定),但是您還沒有完成。如果您希望導出爲CSV,那麼這也不是堆棧溢出問題,因爲快速的谷歌搜索將爲您提供所需的所有答案。 –
@vonPryz自從我剛開始使用PowerShell時,我無法在一個腳本中找到所有這些方法。問題是如果你可以幫我製作一個能夠重啓已經運行了10天以上的計算機的腳本,並給我一個很好的,易於閱讀的概述。提前致謝! –