OU=_是一家私人公司的名称。我知道它是重新启动的,这只是为了在它进入真正的崩溃过程之前进行测试。
function Get-LastBootUpTime {
param (
$ComputerName
)
$OperatingSystem = Get-WmiObject Win32_OperatingSystem -ComputerName $ComputerName
[Management.ManagementDateTimeConverter]::ToDateTime($OperatingSystem.LastBootUpTime)
}
$Days = -0
$ShutdownDate = (Get-Date).adddays($days)
$ComputerList = Get-ADComputer -SearchBase 'OU=TEST-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 $Computer -Force
}
else {
Write-Host "No need to reboot: $_" -ForegroundColor Green
}
}我正试图关闭我公司的所有运行时间超过2天的个人电脑。脚本已经完成了,但是当涉及到问题时,它会显示一个错误:
restart-Computer $Computer -Force如果我输入而不是$Computer,$ComputerList脚本会关闭OU中的每台PC,即使它们的运行时间不会超过2天。因此,关闭整个公司只需要一台PC机运行超过2天,这不是我想要的。当PC机已经运行超过2天时,我如何告诉脚本只关闭它们?
发布于 2016-10-07 09:50:16
您的$Computer未定义。你应该使用:
Restart-Computer $_ -Force但是更好的方法是收集所有应该在变量中重新启动的计算机,然后重新启动它们。会工作得快得多:
$toBeRestarted = $ComputerList | Where-Object { $ShutdownDate -gt (Get-LastBootUpTime -ComputerName $_) }
Restart-Computer $toBeRestarted -Force如果你愿意的话,你可以在周围增加一些日志。
https://stackoverflow.com/questions/39914278
复制相似问题