PowerCLI清单脚本获取额外字符

时间:2015-08-20 13:38:42

标签: vmware inventory powercli

我正在为我的环境制作一个清单脚本。主要的驱动原因是,我想使用标签将vm分配给系统管理员和业务所有者。我创建了一个类别调用sysadmin和BusinessOwner。 “类别”设置为每个对象一个标记。

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

系统管理员标签中的

数据如下:@ {Tag = SysAdmin / ITOps:John Smith}

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

在Uptime中数据看起来像是:“@ {Days = 9}”

我希望它看起来像:9

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

这有点好,但它里面有垃圾字符。

我试图使用.trim,但也许我错了。任何有关这方面的帮助将不胜感激。

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

*注意:原始脚本是从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"

1 个答案:

答案 0 :(得分:0)

所以我想出来了,它可能不是最优雅的,但我使用占位符变量来制作额外的数据" @ {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