【问题标题】:How do I get VM tags added into my output text file如何将 VM 标签添加到输出文本文件中
【发布时间】:2017-01-11 19:08:15
【问题描述】:

我正在获取我的 VMWare 环境中所有快照的列表,我正在格式化列表以获取“VM、名称、已创建、SizeGB”,并且我还想从这些 VM 中获取 vmware 服务器标记信息,但我我不知道该怎么做。当列表自行格式化时,我想获取标签信息并将其放在“SizeGB”下。有没有办法做到这一点?

这是我没有标签信息的脚本:

# Adding PowerCLI Snap-in to use cmdlets to connect to vSphere
Add-PSSnapin VMware.VimAutomation.Core

# Connect to vCenter
Connect-ViServer -server $vCenter -ErrorAction Stop 

# Write header on report
Write-Output "VMWare Snapshot Report for $(get-date -f MM-dd-yyyy)" |
    Out-File $Log -Append

# Get VM Snapshot Information for all Snapshots and send that information to
# c:\automation\AllSnapshots.txt
Get-VM | Get-Snapshot | Where-Object {
    $_.Created -lt (Get-Date).AddDays(-3)
} | Format-List vm, name, created, sizegb | Out-File $Log -Append

# Disconnect from vCenter
Disconnect-VIServer -Server * -Force -Confirm:$false

【问题讨论】:

    标签: powershell powercli


    【解决方案1】:
    # Adding PowerCLI Snap-in to use cmdlets to connect to vSphere
    Add-PSSnapin VMware.VimAutomation.Core
    
    # Connect to vCenter
    Connect-ViServer -server $vCenter -ErrorAction Stop 
    
    # Write header on report
    Write-Output "VMWare Snapshot Report for $(get-date -f MM-dd-yyyy)" |
        Out-File $Log -Append
    
    # Get VM Snapshot Information for all Snapshots and send that information to
    # c:\automation\AllSnapshots.txt
    $output = @()
    foreach ($vm in Get-VM) {
       $tags = ((Get-TagAssignment -Entity $vm | select -ExpandProperty Tag).Name -join ", ")
       foreach ($snapshot in $vm | Get-Snapshot | Where-Object { $_.Created -lt (Get-Date).AddDays(-3) }) {
           $obj = [PSCustomObject]@{VM = $vm.Name; Name = $snapshot.Name; Created = $snapshot.Created; SizeGB = $snapshot.SizeGB; Tags = $tags}
           $output += $obj
       }
    }
    $output | Format-List | Out-File $Log -Append
    
    # Disconnect from vCenter
    Disconnect-VIServer -Server * -Force -Confirm:$false
    

    【讨论】:

    • 我试过用这个,没有输出到文件
    • 如果删除 Out-File cmdlet,您是否在控制台中看到任何输出?这对我来说适用于 vCenter 5.5 和 PowerCLI 6.0。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-07-11
    • 2019-07-16
    • 2016-05-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多