【问题标题】:Trying to select a variable from within a ForEach loop, and if that variable is less than a certain number, display another variable as Red尝试从 ForEach 循环中选择一个变量,如果该变量小于某个数字,则将另一个变量显示为红色
【发布时间】: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


【解决方案1】:

你可以像下面这样尝试

gwmi Win32_LogicalDisk  
| select  Deviceid,@{n="SizeinGB";e={[math]::Round($_.Size/1GB,2)}},@{n="FreeSpaceinGB";e={[math]::Round($_.FreeSpace/1GB,2)}}  |
%{ Write-Host  -ForegroundColor $(if ($_.FreeSpaceinGB -le 5 )
 {"red"} else {"Green"}) $_.Deviceid  , $_.SizeinGB , $_.FreeSpaceinGB}

参考资料:
https://technet.microsoft.com/en-us/library/2008.06.windowspowershell.aspx?f=255&MSPPError=-2147217396

https://social.technet.microsoft.com/Forums/scriptcenter/en-US/0797d236-eda4-4b40-b7aa-40f150af7653/how-can-i-color-a-column-in-powershell-output?forum=winserverpowershell

【讨论】:

  • 我将它添加到我的脚本中并且它正在工作,但现在存储信息输出在代码中到达该行时立即发生,它跳过 CPU 信息变量,然后不当调用 Server Info 变量时,不要发布 Storage Info 或 CPU Info。我在上面的问题中添加了完整的输出。
  • Write-host 不适用于变量,输出不存储
  • 有没有办法让 write-host 存储为变量,或者我可以使用类似的命令来达到相同的结果?这也不能解释为什么我的 CPU 变量现在也被跳过了,除非它意外地与写入主机合并。
【解决方案2】:

此脚本列出本地计算机上所有已安装的驱动器。您可以将其集成到当前循环中。

脚本不需要这么长,但是,我想突出显示各个步骤。

$drives = gwmi Win32_LogicalDisk -ComputerName .

foreach ($drive in $drives) {

    $freeSpaceKB = $drive.FreeSpace
    $freeSpaceGB = $freeSpaceKB / 1gb

    if ($freeSpaceGB) {

        Write-Host $drive.DeviceID, $freeSpaceGB
    }
    else {

        Write-Host $drive.DeviceID -ForegroundColor 'Red'
    }        
}

【讨论】:

  • 我已将此添加到我的脚本中,但我遇到了与上述相同的问题。我需要能够将来自 Write-Host 的信息放入一个变量中,以便操作系统信息、存储信息和 CPU 信息同时从 ServerInfo 数组中发布。有没有办法做到这一点?编辑:另外,我遇​​到的问题是它现在跳过了我的 CPU 信息,并且只从阵列中发布操作系统信息。
  • 听起来你的脚本有两个不同的部分。数据收集然后输出。您可以在收集过程中将$drives var 添加到$ServerInfo。然后构建一个循环,一次性输出整个$ServerInfo var。
  • 我想我明白你的意思了。在 ForEach 循环中运行第一部分以收集数据,将所有这些存储在 ServerInfo 变量中,然后将所有这些信息输出到我决定放置的任何位置(如 csv 文件)?我是否需要将第二部分放在 Do 循环中,还是可以将其放在 ForEach 之外就可以了?
猜你喜欢
  • 2013-08-02
  • 1970-01-01
  • 2021-10-22
  • 2021-12-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-01-06
相关资源
最近更新 更多