2017-08-01 138 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

它似乎可以正常工作,但在更改後出現此錯誤。 (見文章) –

相關問題