If statement 添加if语句以更改文本输出
我有一些PowerShell代码,可以获取服务器列表的正常运行时间,并输出服务器运行的天数、小时数和分钟数 我正在尝试添加一条语句,该语句将占用少于10小时的正常运行时间并输出到文本文件-重新启动 如果服务器运行了30天或更长时间,则文本输出将为-Reboot 我只是不知道该怎么做。这是我的正常运行时间If statement 添加if语句以更改文本输出,if-statement,powershell-2.0,uptime,If Statement,Powershell 2.0,Uptime,我有一些PowerShell代码,可以获取服务器列表的正常运行时间,并输出服务器运行的天数、小时数和分钟数 我正在尝试添加一条语句,该语句将占用少于10小时的正常运行时间并输出到文本文件-重新启动 如果服务器运行了30天或更长时间,则文本输出将为-Reboot 我只是不知道该怎么做。这是我的正常运行时间 $names = Get-Content "C:\Users\david.sechler\Documents\PowerShell\Get Uptime\servers.txt"
$names = Get-Content "C:\Users\david.sechler\Documents\PowerShell\Get Uptime\servers.txt"
@(
foreach ($name in $names)
{
if ( Test-Connection -ComputerName $name -Count 1 -ErrorAction SilentlyContinue )
{
$wmi = gwmi -class Win32_OperatingSystem -computer $name
$LBTime = $wmi.ConvertToDateTime($wmi.Lastbootuptime)
[TimeSpan]$uptime = New-TimeSpan $LBTime $(get-date)
Write-output "$name Uptime is $($uptime.days) Days $($uptime.hours) Hours $($uptime.minutes) Minutes $($uptime.seconds) Seconds"
}
else {
Write-output "$name is not pinging"
}
}
) | Out-file -FilePath "C:\Users\david.sechler\Documents\PowerShell\Get Uptime\results.txt"
试试这个
$toreboot = @()
$rebooted = @()
[timespan]$recentboot = new-timespan $(get-date).AddHours(-10) $(get-date)
[timespan]$needboot = new-timespan $(get-date) $(get-date).Adddays(30)
$recentboot
$needboot
$names = Get-Content "d:\scrap\servers.txt"
foreach ($name in $names)
{
if ( Test-Connection -ComputerName $name -Count 1 -ErrorAction SilentlyContinue )
{
$wmi = gwmi -class Win32_OperatingSystem -computer $name
$LBTime = $wmi.ConvertToDateTime($wmi.Lastbootuptime)
[TimeSpan]$uptime = New-TimeSpan $LBTime $(get-date)
Write-output "$name Uptime is $($uptime.days) Days $($uptime.hours) Hours $($uptime.minutes) Minutes $($uptime.seconds) Seconds"
if ($uptime -lt $recentboot)
{$rebooted += $names}
if($uptime -gt $needboot)
{$toreboot += $name}
}
else {
Write-output "$name is not pinging"
}
if ($toreboot -ne $null)
{set-content -Path "d:\scrap\serverstoboot.txt" -Value $toreboot}
if ($rebooted -ne $null)
{set-content -Path "d:\scrap\recentlybooted.txt" -value $rebooted}
}
这是什么语言?对不起,丹,问得好!这是powershell。