2017-08-01 139 views
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 
+0

那么问题是什么?如何启动电脑?如何将结果写入文件?还有别的吗? – vonPryz

+0

您可能想要查看导入/导出为CSV(使用'Get-Help * csv *'),或者您想更正代码中的错误(您可以指定),但是您还没有完成。如果您希望导出为CSV,那么这也不是堆栈溢出问题,因为快速的谷歌搜索将为您提供所需的所有答案。 –

+0

@vonPryz自从我刚开始使用PowerShell时,我无法在一个脚本中找到所有这些方法。问题是如果你可以帮我制作一个能够重启已经运行了10天以上的计算机的脚本,并给我一个很好的,易于阅读的概述。提前致谢! –

回答

0

既然你已经有一些地区覆盖,这ISN只是给我一个代码,因此可以提供帮助。那么让我们来概述整个脚本应该如何看待。

# Write computer names into a file, one per row for easier management 
$clients = get-content "c:\ListOfComputers.txt" 

foreach ($client in $clients) { 
# If computer's not up, there's no need to check uptime 
    if (test-connection -computername $client -BufferSize 16 -Count 1 -Quiet) { 
     write-Host $client is online 

     # Get uptime 
     $uptime = (get-date) - (gcim Win32_OperatingSystem -computer $client).LastBootUpTime 
     # Get start time 
     $startTime = [Management.ManagementDateTimeConverter]::ToDateTime((gwmi Win32_OperatingSystem -computer $client).lastbootuptime) 

     # Restart the client if uptime's at least 10 days. 
     if($uptime.days -ge 10) { 
      restart-computer -computername $client 
     # Add client name, start date and uptime into a log file 
      add-content -path "c:\path\to\log.txt" -value "$client, $startTime, $uptime" 
     } 
    } 
    else { 
    write-Host $client is offline 
    } 
} 

这个框架可以通过添加一些错误处理和正确的CSV导出进一步改进。

+0

它似乎可以正常工作,但在更改后出现此错误。 (见文章) –

相关问题