【问题标题】:Add column to CSV file with powershell AzureVM使用 powershell AzureVM 将列添加到 CSV 文件
【发布时间】:2018-03-06 15:19:04
【问题描述】:

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

我想根据可用空间在其中添加一个推荐列,如果可用空间大于 90%,则添加评论“考虑调整大小”,如果小于 15%,则“考虑清理磁盘”。

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

$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
}

这不起作用,然后我尝试添加另一个部分,我得到真或假,这似乎有效..下面是那个,但是我需要在中添加建议..

$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
}

希望这是有道理的。

提前致谢:)

【问题讨论】:

    标签: powershell azure calculated-columns


    【解决方案1】:

    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
    }
    

    【讨论】:

    • 谢谢大家,我明白了我在做什么,我正在寻找上一列中生成的值,而不是从公式中创建值.. 呵呵!有没有办法在其中设置一个范围,例如,如果它在 75-90 之间写“blah”,90 以上写“blah blah”.. 再次感谢您的帮助:)
    • @NorrinRad 见编辑;-)。这是因为第一个 if 已经处理了90+,所以第二个 if 只需要检查75+ - 它不需要上限。您可以根据需要添加任意数量的elseifs。
    • 非常感谢,设法将所有内容归为一列.. 非常感谢您的帮助:)
    猜你喜欢
    • 1970-01-01
    • 2013-06-05
    • 1970-01-01
    • 2018-11-29
    • 1970-01-01
    • 1970-01-01
    • 2012-04-06
    • 1970-01-01
    相关资源
    最近更新 更多