2017-08-01 14 views
0

ネットワーク上のコンピュータがどれくらいの期間オンになっているかを確認するスクリプトを作成しようとしています。 。私は毎週日曜日にタスクマネージャを使ってスクリプトを自動的に実行する予定です。-LastBootupTime = -gtの場合、powershellを使用してコンピュータを再起動する10日

$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にエクスポートしようとしている場合、それは迅速なGoogle検索はあなたが必要とするすべての答えを提供するので、スタックオーバーフローの問題でもありません。 –

+0

@vonPryz私はちょうどpowershellを使い始めて以来、1つのスクリプトですべてのことを行う方法を見つけることができません。問題は、あなたが10日以上稼働しているコンピュータを再起動するスクリプトを作成するのを手伝ってくれて、何が起こったのかをわかりやすく簡単に読むことができるかどうかです。前もって感謝します! –

答えて

0

すでにいくつかの部分がカバーされてしまったので、このISN私は今、このような何かを持っている@vonPryzへ

感謝'ちょうどPLZは私にコードを与えて、助けを借りている。それでは、スクリプト全体の見方を概説しましょう。

# 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

これはうまくいくように思えましたが、変更後にこのエラーが発生しています。 (記事を参照) –

関連する問題