【发布时间】:2019-01-15 22:42:06
【问题描述】:
这是我第一次在这里提问,如果没有多大意义,请见谅。
我目前正在尝试做一个项目,在该项目中我查找操作系统信息、磁盘大小、处理器类型和服务器列表的制造商,并从单个变量中输出所有这些信息。之后,如果服务器的可用空间小于5gb,则以红色列出服务器的磁盘大小。
这就是我目前所拥有的脚本:
$ServerList = Get-Content $env:USERPROFILE\Documents\Servers.txt
ForEach($Server in ($ServerList))
{
"Collecting server information on $Server, please wait..."
Start-Sleep 3
"Collecting Operating System..."
$OSInfo = gwmi Win32_OperatingSystem -ComputerName $Server | select Caption
"Collecting Storage..."
$StorageInfo = gwmi Win32_LogicalDisk -ComputerName $Server | Format-Table DeviceId, @{n="Size in GB";e={[math]::Round($_.Size/1GB,2)}},@{n="Free Space in GB";e={[math]::Round($_.FreeSpace/1GB,2)}}
"Collecting CPU..."
$CPUInfo = gwmi Win32_Processor -ComputerName $Server | select Name, Manufacturer
$ServerInfo = @($OSInfo, $StorageInfo, $CPUInfo)
Start-Sleep 3
"All done with $Server. Here's what we got:"
$ServerInfo
Start-Sleep 3
"Continuing on to next server in the list..."
Start-Sleep 3
break
}
这是一个输出示例:
Collecting server information on *****, please wait...
Collecting Operating System...
Collecting Storage...
Collecting CPU...
All done with *****. Here's what we got:
Caption
-------
Microsoft Windows Server 2008 R2 Enterprise
DeviceId Size in GB Free Space in GB
-------- ---------- ----------------
A: 0 0
C: 51.9 5.35
D: 15 6.37
E: 0 0
Name Manufacturer
---- ------------
Intel(R) Xeon(R) CPU E7- 2870 @ 2.40GHz GenuineIntel
Intel(R) Xeon(R) CPU E7- 2870 @ 2.40GHz GenuineIntel
Intel(R) Xeon(R) CPU E7- 2870 @ 2.40GHz GenuineIntel
Intel(R) Xeon(R) CPU E7- 2870 @ 2.40GHz GenuineIntel
Continuing on to next server in the list...
我想做一个If语句,如果"Free Space in GB"中提供的数字小于5,那么它将以红色显示以GB为单位的大小,否则以黄色显示,
问:
我不知道如何实际从“GB 中的可用空间”中提取数字以在 If 语句中使用。有什么帮助吗?
编辑:
我使用用户 TheGameiswar 的回答编辑了我的脚本,现在我的输出遇到了一个新问题。
Collecting server information on *****, please wait...
Collecting Operating System...
Collecting Storage...
A: 0 0
C: 50 19.55
D: 35 18.16
E: 0 0
Collecting CPU...
All done with *****. Here's what we got:
Caption
-------
Microsoft Windows Server 2008 R2 Enterprise
Continuing on to next server in the list...
CPU 输出被跳过,它在脚本的那部分运行时输出存储信息,而不是在调用 $ServerInfo 变量时。我不确定发生了什么。
【问题讨论】:
-
Write-Host 有一个
ForegroundColor参数。例如:Write-Host "Red Text" -ForegroundColor 'Red'.
标签: powershell if-statement foreach powershell-3.0