我有这个脚本:
function Get-LastBootUpTime {
param (
$ComputerName
)
$OperatingSystem = Get-WmiObject Win32_OperatingSystem -ComputerName $ComputerName
[Management.ManagementDateTimeConverter]::ToDateTime($OperatingSystem.LastBootUpTime)
}
$Days = -1
$ShutdownDate = (Get-Date).adddays($days)
$ComputerList = Get-ADComputer -SearchBase 'OU=X,OU=X,DC=X,DC=X' -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
}
}
关闭每台在线超过1天的电脑。
它工作正常,但我也想要一个Out-File
作为.txt,它显示哪个PC已关闭。所以要么消息" PCx需要关闭"或PCx的正常运行时间。我尝试用Out-File
来做,但它从未读取我想要的信息。有什么想法吗?
答案 0 :(得分:0)
您可以使用Tee-Object
- 命令:
"$_ last booted: $Bootup" | tee -Append log.txt
它会将文本附加到文件中并将其打印在屏幕上。