使用 powershell AzureVM 将列添加到 CSV 文件

Add column to CSV file with powershell AzureVM

不知道如何解释这一点,我们正在对 Azure 上的磁盘使用情况进行评估,以降低成本。我们正在尝试评估每个虚拟机上的 space 并减少磁盘

我想根据免费 space 在其中添加一个推荐栏,如果免费 space 大于 90% 则添加评论 "consider resizing" 如果更少超过 15% 然后 "consider disk clean up".

我的脚本工作正常,除了它没有添加评论,首先我尝试了这个...

$computers = (Get-AdComputer -Filter "name -like 'VM-*'").Name | Sort-Object
foreach ($computer in $Computers) 
{
$vol = gwmi Win32_volume -Computer $Computer -Filter 'DriveType = 3'
#$vol

$info = $vol | select PsComputerName, DriveLetter, Label,
           @{n='Capacity';e={[int]($_.capacity/1GB)}}, 
           @{n='FreeSpace';e={[int]($_.FreeSpace/1GB)}},
           @{n='FreeSpace (%)';e={[int](($_.FreeSpace) / ($_.capacity) * 100.0)}}
           if ('FreeSpace (%)' -gt 85)
           {
           Write-Output "Disk Usage Low, Consider Resizing Options"
           }
           else 
           {
           Write-Output "Disk Usage High"
           }
$info  | Export-Csv "c:\temp\tempfiles\question.csv" -Append
}

那没有用,然后我尝试添加另一个部分,在那里我得到 true 或 false,这似乎有效..那个在下面,但是我需要在..

中添加建议
$computers = (Get-AdComputer -Filter "name -like 'VM-*'").Name | Sort-Object
foreach ($computer in $Computers) 
{
$vol = gwmi Win32_volume -Computer $Computer -Filter 'DriveType = 3'
#$vol

$info = $vol | select PsComputerName, DriveLetter, Label,
           @{n='Capacity';e={[int]($_.capacity/1GB)}}, 
           @{n='FreeSpace';e={[int]($_.FreeSpace/1GB)}},
           @{n='FreeSpace (%)';e={[int](($_.FreeSpace) / ($_.capacity) * 100.0)}},
           @{n='Recommendation';e={[String] ($_.FreeSpace -gt 90)}}

$info  | Export-Csv "c:\temp\tempfiles\question.csv" -Append
}

希望这是有道理的。

提前致谢:)

e代表Expression。所以你应该能够在那里使用表达式,使用原始对象属性(即不是你自定义的 属性 名称,例如 FreeSpace (%)

$computers = (Get-AdComputer -Filter "name -like 'VM-*'").Name | Sort-Object
foreach ($computer in $Computers) 
{
$vol = gwmi Win32_volume -Computer $Computer -Filter 'DriveType = 3'
#$vol

$info = $vol | select PsComputerName, DriveLetter, Label,
           @{n='Capacity';e={[int]($_.capacity/1GB)}}, 
           @{n='FreeSpace';e={[int]($_.FreeSpace/1GB)}},
           @{n='FreeSpace (%)';e={[int](($_.FreeSpace) / ($_.capacity) * 100.0)}},
           @{n='Recommendation';e={
                if((($_.FreeSpace) / ($_.capacity) * 100.0) -gt 90){
                    "Disk Usage Low, Consider Resizing Options"
                }elseif((($_.FreeSpace) / ($_.capacity) * 100.0) -gt 75){
                    "Something else"
                }else{
                    "Disk Usage High"
                }
            }

$info  | Export-Csv "c:\temp\tempfiles\question.csv" -Append
}