繁体   English   中英

远程关闭多台PC

[英]Shutting down multiple PCs remotely

我想关闭我工作场所中的几乎所有PC(如果它们运行超过2天),我已经在上个星期和本周使用了Script,并试图消除途中的错误。

$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名称而不是“ $ Computer”,它将按我的意愿关闭PC。 这是什么RPC服务器错误? 我没有激活防火墙,所以我一无所知...

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

我没有用于测试您的Get-ADComputer查询的AD环境,但这仅对一台计算机阵列有效,因此对您来说应该没问题。

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
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM