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天以上的计算机的脚本,并给我一个很好的,易于阅读的概述。提前致谢! –