【发布时间】: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"
【问题讨论】: