【问题标题】:PowerCLI Inventory Script Getting extra Characters获取额外字符的 PowerCLI 清单脚本
【发布时间】:2015-08-20 13:38:42
【问题描述】:

我正在为我的环境编写清单脚本。这样做的主要原因是,我想使用标签将虚拟机分配给系统管理员和企业主。我创建了一个名为 sysadmin 和 BusinessOwner 的类别。类别设置为每个对象一个标签。

我构建的脚本似乎工作正常,但我在标签相关列和正常运行时间中得到了额外的字符。

在 Sysadmin 标签中,数据如下所示:@{Tag=SysAdmin/ITOps: John Smith}

我希望它看起来像:ITOps:John Smith

在正常运行时间中,数据如下所示:“@{Days=9}”

我希望它看起来像:9

我也尝试删除 |从“$row.sysadmin”行中选择标记选项。数据格式不同:[SysAdmin/ITOps: John Smith] SERVERNAME

这个稍微好点,但是里面还是有垃圾字符。

我尝试使用 .trim,但也许我用错了。对此的任何帮助将不胜感激。

*注意我故意禁用 DC/Cluster 部分以针对文件夹进行测试

*注:原脚本来自http://www.vstrong.info/2014/03/25/export-vcenter-virtual-machine-inventory/

$report = @()
#foreach ($DC in Get-Datacenter -Name MyDC){ # Specify Datacenter name if needed
#foreach ($Cluster in Get-Cluster -Location $DC ){ # Specify Cluster name if needed
foreach ($VM in Get-VM -Location Tag_test){

$row = "" | select DC, Cluster, ResourcePool, VMPath, VMhost, PowerState, Name, OS, IPaddress, MacAddress, CPU, Memory, ProvisionedSpaceGB, UsedSpaceGB, Datastore, Notes, Tools, Sysadmin, BusinessOwner, snapshots, portgroup, uptime

  $row.DC = $DC
  $row.Cluster = $Cluster
     ##### If there is no Resource Pool configured in the Cluster, use the Cluster name
     if ($vm.ResourcePool -like "Resources") {$row.ResourcePool = $Cluster}
     else {$row.ResourcePool = $vm.ResourcePool}
        ##### Full VM path in the vCenter VM folder structure - start
        $current = $vm.Folder
        $path = $vm.Name
        do {
        $parent = $current
        if($parent.Name -ne "vm"){$path =  $parent.Name + "\" + $path}
        $current = Get-View $current.Parent
        } while ($current.Parent -ne $null)
        $row.VMPath = $path
        ##### Full VM path in the vCenter VM folder structure - finish
  $row.VMhost = $vm.VMHost
  $row.PowerState = $vm.PowerState
  $row.Name = $vm.Name
  $row.OS = $vm.Guest.OSFullName
  $row.IPaddress = $vm.Guest.IPAddress | Out-String
  $row.MacAddress =($vm | Get-NetworkAdapter).MacAddress -join ", ";  
  $row.CPU = $vm.NumCPU
  $row.Memory = $vm.MemoryGb
  $row.ProvisionedSpaceGB = [math]::round( $vm.ProvisionedSpaceGB , 2 )
  $row.UsedSpaceGB = [math]::round( $vm.UsedSpaceGB , 2 )
  #$row.Datastore = ($vm | Get-Datastore).Name | Out-String
  $row.Datastore =  (Get-Datastore -vm $vm) -split ", " -join ", "; 
  $row.Notes = $vm.Notes
  $row.tools = $vm.ExtensionData.Guest.ToolsVersionStatus
  $row.sysadmin = Get-TagAssignment -Entity $vm -Category SysAdmin| Select Tag 
  $row.Businessowner = Get-TagAssignment -Entity $vm -Category BusinessOwner | Select Tag 
  $row.snapshots = ($vm | get-snapshot).count;
  $row.portgroup = ($vm | Get-NetworkAdapter).NetworkName -join ", ";  
  $uptime = get-stat -entity $vm -stat sys.uptime.latest -RealTime -MaxSamples 1
  $row.uptime = new-timespan -seconds $uptime.value |Select Days 
$report += $row

}

$report | Sort Name | Export-Csv -Path "D:\VMs.csv"

【问题讨论】:

    标签: vmware inventory powercli


    【解决方案1】:

    所以我发现了这一点,它可能不是最优雅的,但我使用占位符变量使额外数据“@{Days=”从报告中删除。

    希望这个脚本对人们有所帮助

    VSphere Inventory Script With VSphere Tag Support.
    
    Connect-VIServer vcenter.server.name
    
    
    $report = @()
    
    foreach ($DC in Get-Datacenter ){ # Specify Datacenter name if needed
    foreach ($Cluster in Get-Cluster -Location $DC ){ # Specify Cluster name if needed
    foreach ($VM in Get-VM -Location $Cluster | where-object {$_.Folder -notmatch "Template Base"}  ){
    
    
    $row = "" | select Name, PowerState, OS, Tools, Notes, Sysadmin, BusinessOwner, Snapshots, Uptime, DC, Cluster, ResourcePool, VMPath, VMhost, IPaddress, MacAddress, PortGroup, CPU, Memory, ProvisionedSpaceGB, UsedSpaceGB, Datastore
    #Null Out Variables
    $ipadd = $null
    $macadd = $null
    $maclist = $null
    $portlist = $null
        $row.Name = $vm.Name
        $row.PowerState = $vm.PowerState
        $row.OS = $vm.Guest.OSFullName
        $row.Tools = $vm.ExtensionData.Guest.ToolsVersionStatus
        $row.Notes = $vm.Notes
        #This Section Queries for the Sysadmin Tag Value
            $sysadmin = Get-TagAssignment -Entity $vm -Category SysAdmin
        $row.Sysadmin = $sysadmin.tag
        #This Section Queries for the BusinessOwner Tag Value
            $Businessowner = Get-TagAssignment -Entity $vm -Category BusinessOwner
        $row.Businessowner = $Businessowner.tag
        $row.Snapshots = ($vm | get-snapshot).count;
        #This Section Calculates System Uptime
            $uptime = get-stat -entity $vm -stat sys.uptime.latest -RealTime -MaxSamples 1 -ErrorAction SilentlyContinue
            $uptimed = new-timespan -seconds $uptime.value
        $row.Uptime = $uptimed.days
        $row.DC = $DC
        $row.Cluster = $Cluster
         ##### If there is no Resource Pool configured in the Cluster, use the Cluster name
         if ($vm.ResourcePool -like "Resources") {$row.ResourcePool = $Cluster}
         else {$row.ResourcePool = $vm.ResourcePool}
            ##### Full VM path in the vCenter VM folder structure - start
            $current = $vm.Folder
            $path = $vm.Name
            do {
            $parent = $current
            if($parent.Name -ne "vm"){$path =  $parent.Name + "\" + $path}
            $current = Get-View $current.Parent
            } while ($current.Parent -ne $null)
        $row.VMPath = $path
            ##### Full VM path in the vCenter VM folder structure - finish
        $row.VMhost = $vm.VMHost
        #$row.IPaddress = $vm.Guest.IPAddress | Out-String
        #Get all IP's and Make a list
            $iplist =$vm.Guest.IPAddress 
            Foreach ($ip in $iplist) { $ipadd += $ip + "; "}
        $row.IPaddress = $ipadd
        #Get all MAC's and Make a list
        #$row.MacAddress =($vm | Get-NetworkAdapter).MacAddress -join ", ";  
            $maclist =($vm | Get-NetworkAdapter)|Select MacAddress
            Foreach ($mac in $maclist) { $macadd += $mac.macaddress + "; "}
        $row.MacAddress = $macadd
        #$row.portgroup = ($vm | Get-NetworkAdapter).NetworkName -join ", ";
        #Get all Port Groups's and Make a list
            $portgroup =$vm | Get-NetworkAdapter
            Foreach ($port in $portgroup) { $portlist += $port.networkname + "; "}
        $row.PortGroup = $portlist
        $row.CPU = $vm.NumCPU
        $row.Memory = $vm.MemoryGb
        $row.ProvisionedSpaceGB = [math]::round( $vm.ProvisionedSpaceGB , 2 )
        $row.UsedSpaceGB = [math]::round( $vm.UsedSpaceGB , 2 )
        #$row.Datastore = ($vm | Get-Datastore).Name | Out-String
        $row.Datastore =  (Get-Datastore -vm $vm) -split ", " -join ", ";
    
    $report += $row
    
    }}}
    
    
    $mailfrom = "from@email.com"
    $mailto = "to@email.com"
    $smtpserver = "smpt.server.com"
    $csvpath = "C:\pcli\Scheduled_Scripts\Production_VMs_ $(get-date -f yyyy-MM-dd).csv"
    $report | Sort Name | Export-Csv -Path $csvpath -NoType
    Send-MailMessage -From $mailfrom -to $mailto -subject "Production VM Inventory" -SmtpServer $smtpserver -body "See Attachment" -Attachments $csvpath
    Attachment" -Attachments $csvpath
    
    
    Remove-Item $csvpath
    

    【讨论】:

      猜你喜欢
      • 2013-02-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-07
      • 2021-07-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多