2016-10-04 70 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