You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Azure VM磁盘成本优化:PowerShell给CSV添加推荐列失败求助

Hey there! Let's fix that missing "推荐" column in your Azure VM disk evaluation CSV. Since your existing script already pulls valid disk data, we just need to add the conditional recommendation logic correctly — here are two proven approaches depending on what tool you're using (PowerShell or Python, the most common for Azure automation):

PowerShell Implementation

If you're using PowerShell to fetch and export disk data, you can add the recommendation column directly with a calculated property during the selection step:

# Assume $diskData is your existing array of objects with disk metrics (e.g., FreeSpacePercentage, VMName, DiskSize)
$diskDataWithRecommendations = $diskData | Select-Object *, @{
    Name = "推荐"
    Expression = {
        # Adjust the property name to match your actual free space percentage field
        $freePct = $_.FreeSpacePercentage
        if ($freePct -gt 90) {
            "consider resizing"
        }
        elseif ($freePct -lt 15) {
            "consider disk clean up"
        }
        else {
            "" # Leave empty for disks in the healthy 15-90% free range, or add a custom message
        }
    }
}

# Export to CSV (ensure encoding is set correctly for your locale)
$diskDataWithRecommendations | Export-Csv -Path "Azure_VM_Disk_Evaluation.csv" -NoTypeInformation -Encoding UTF8

Key Notes for PowerShell:

  • Double-check that FreeSpacePercentage matches the exact property name in your existing $diskData object. If you only have raw free/total space values, calculate the percentage first: ($_.FreeSpace / $_.TotalSize) * 100
  • If your previous attempts failed, it's likely because you tried adding the column after exporting, or didn't properly cast the free space value to a numeric type (strings can't be compared with -gt/-lt)

Python Implementation

If you're using Python with pandas (common for data processing) or the Azure SDK, here's a clean way to add the recommendation column:

Using Pandas apply() (flexible for complex logic):

import pandas as pd

# Load your existing disk data into a DataFrame (replace with how you fetch data from Azure)
disk_df = pd.read_csv("existing_disk_data.csv") # Or load directly from Azure SDK responses

def generate_recommendation(row):
    free_pct = row["free_space_percent"] # Match your column name
    if free_pct > 90:
        return "consider resizing"
    elif free_pct < 15:
        return "consider disk clean up"
    else:
        return ""

# Add the new column
disk_df["推荐"] = disk_df.apply(generate_recommendation, axis=1)

# Save to CSV
disk_df.to_csv("Azure_VM_Disk_Evaluation.csv", index=False, encoding="utf-8-sig")

Faster Alternative with numpy.where():

For large datasets, use vectorized operations instead of row-by-row apply:

import numpy as np

disk_df["推荐"] = np.where(
    disk_df["free_space_percent"] > 90,
    "consider resizing",
    np.where(
        disk_df["free_space_percent"] < 15,
        "consider disk clean up",
        ""
    )
)

Key Notes for Python:

  • Ensure your free_space_percent column is a numeric type (float/int). If it's stored as a string, convert it first with disk_df["free_space_percent"] = pd.to_numeric(disk_df["free_space_percent"], errors="coerce")
  • If your previous script wrote the CSV before adding the column, you'll need to insert the recommendation logic before the to_csv() call

内容的提问来源于stack exchange,提问作者Norrin Rad

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 08:12:29