2016-10-04 57 views
0

我想在工作場所關閉幾乎所有的個人電腦(如果它們運行超過2天) 我已經在上一週和本週的腳本上工作並試圖擺脫錯誤辦法。遠程關閉多臺PC

$days = -0 
$date = (get-date).adddays($days) 
$lastboot = (Get-WmiObject Win32_OperatingSystem).LastBootUpTime 
$Computer = Get-ADComputer -SearchBase 'OU=______,OU=______,DC=______,DC=______' ` -Filter '*' | Select -EXP Name 

$lastbootconverted = ([WMI]'').ConvertToDateTime($lastboot) 

write-host $date 

write-host $lastboot 

write-host $lastbootconverted 

if($date -gt $lastbootconverted) 
{ 
write-host Need to reboot 
(Stop-Computer -$Computer -Force) 
} 
else 
{ 
write-host no need to reboot 
} 

當我運行它,它說 的「RPC-服務器不可用(例外HRESULT:0x800706BA)。」 但如果我只是把代替「$電腦」一臺PC的名稱,它關閉個人電腦像我想要的。什麼是RPC服務器錯誤?我沒有防火牆的啓動,所以我無言以對......

的OU = _____和DC = ______是私人公司名稱

+0

你 - $計算機應該是沒有$計算機 –

+0

@Taylor吉布我不這麼認爲,但我嘗試過了,它並沒有改變。 – Gunter

+0

$電腦的輸出是什麼? – BenH

回答

0

我有沒有AD環境中測試GET-ADComputer查詢,但這對我來說只是一個電腦陣列,所以應該適合你。

function Get-LastBootUpTime {    
param (
    $ComputerName 
) 
    $OperatingSystem = Get-WmiObject Win32_OperatingSystem -ComputerName $ComputerName    
    [Management.ManagementDateTimeConverter]::ToDateTime($OperatingSystem.LastBootUpTime)    
} 

$Days = -2 
$ShutdownDate = (Get-Date).adddays($days) 

$ComputerList = Get-ADComputer -SearchBase 'OU=______,OU=______,DC=______,DC=______' ` -Filter '*' | Select -EXP Name 

$ComputerList | foreach { 
    $Bootup = Get-LastBootUpTime -ComputerName $_ 

    Write-Host "$_ last booted: $Bootup" 

    if ($ShutdownDate -gt $Bootup) { 
     Write-Host "Rebooting Computer: $_" -ForegroundColor Red 
     Restart-Computer $_ -Force 
    } 
    else { 
     Write-Host "No need to reboot: $_" -ForegroundColor Green 
    } 
} 
+0

嘿,下面的解決方案不再幫助我,它以不同的方式失敗。我們雖然似乎工作。有一件事情不起作用:$計算機不重新啓動....我想BC它沒有指定$電腦是什麼。任何解決方案 – Gunter

+0

我已將_Stop-Computer_更新爲_Restart-Computer_,因此它現在將重新啓動而不是關閉。你可以再試一次,讓我知道會發生什麼。 –

+0

問題是$電腦:)你也編輯過,所以我認爲你的腳本應該工作正常,非常感謝你 – Gunter