Hi,
I am unable to get IP Address and OS details in the below script, please help.
also I dont see any error
Script
$assigned = Get-VM | Get-TagAssignment
$tagCat = $assigned.Tag.Category.Name | Sort-Object -Unique
$report = $assigned | Group-Object -Property { $_.Entity.Name } |
ForEach-Object -Process {
$vm = $_
$obj = [ordered]@{
Folder = $vm.Group[0].Entity.Folder.Name
VM = $vm.Name
IP =$vm.Entity.Guest.IPAddress
OS =$vm.Entity.Guest.OSFullName
}
$tagCat | ForEach-Object -Process {
$cat = $_
$tags = ($vm.Group | Where-Object { $_.Tag.Category.Name -eq $cat }).Tag.Name -join '|'
$obj.Add($cat, $tags)
}
New-Object PSObject -Property $obj
}
$colTab = @{ }
$report | Get-Member -MemberType NoteProperty | Where-Object { 'VM', 'Folder', 'IP', 'OS' -notcontains $_.Name } |
ForEach-Object -Process {
$column = $report."$($_.Name)"
$columnMax = ($column | Where-Object { $_ -ne '' } | ForEach-Object {
$_.Split('|').Count
} | Measure-Object -Maximum).Maximum
$colTab.Add($_.Name, $columnMax)
}
$report | ForEach-Object -Process {
$row = $_
$obj = [ordered]@{
Folder = $_.Folder
VM = $_.VM
IP =$_.IP
OS =$_.OS
}
$colTab.GetEnumerator() | Sort-Object -Property Name | ForEach-Object -Process {
if ($_.Value -gt 1) {
$col = $_
$values = ($row."$($_.Name)").Split('|')
1..($_.Value) | ForEach-Object -Process {
$obj.Add("$($col.Name)-$_", $values[$_ - 1])`
}
} else {
$obj.Add($_.Name, $row."$($_.Name)")
}
}
New-Object PSObject -Property $obj
} | ft -auto
Output
Folder | VM | IP OS Application | Department Owner | ||||||||
------ | -- | -- -- ----------- | ---------- ----- | ||||||||
Testing | HADOOP | Hadoop | Ops | Karl |
Then you could do something like this.
You can add properties at your hearth's desire in the [ordered] block, the tag categories are added in the foreach loop (tag(s) may be present or not)
Get-VM -PipelineVariable vm | ForEach-Object -Process {
$obj = [ordered]@{
VM = $vm.Name
Folder = $vm.Folder.Name
OSGuest = $vm.Guest.OSFullName
IP = $vm.Guest.IPAddress -join '|'
}
$tCat | ForEach-Object -Process {
$obj.Add($_.Name,(Get-TagAssignment -Entity $vm -Category $_).Tag.Name -join '|')
}
New-Object -TypeName PSObject -Property $obj
}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Try like this
$tagCat = $assigned.Tag.Category.Name | Sort-Object -Unique
$report = $assigned | Group-Object -Property { $_.Entity.Name } |
ForEach-Object -Process {
$vm = $_
$obj = [ordered]@{
Folder = $vm.Group[0].Entity.Folder.Name
VM = $vm.Name
IP = $vm.Group[0].Entity.Guest.IPAddress -join '|'
OS = $vm.Group[0].Entity.Guest.OSFullName
}
$tagCat | ForEach-Object -Process {
$cat = $_
$tags = ($vm.Group | Where-Object { $_.Tag.Category.Name -eq $cat }).Tag.Name -join '|'
$obj.Add($cat, $tags)
}
New-Object PSObject -Property $obj
}
$colTab = @{ }
$report | Get-Member -MemberType NoteProperty | Where-Object { 'VM', 'Folder', 'IP', 'OS' -notcontains $_.Name } |
ForEach-Object -Process {
$column = $report."$($_.Name)"
$columnMax = ($column | Where-Object { $_ -ne '' } | ForEach-Object {
$_.Split('|').Count
} | Measure-Object -Maximum).Maximum
$colTab.Add($_.Name, $columnMax)
}
$report | ForEach-Object -Process {
$row = $_
$obj = [ordered]@{
Folder = $_.Folder
VM = $_.VM
IP = $_.IP
OS = $_.OS
}
$colTab.GetEnumerator() | Sort-Object -Property Name | ForEach-Object -Process {
if ($_.Value -gt 1) {
$col = $_
$values = ($row."$($_.Name)").Split('|')
1..($_.Value) | ForEach-Object -Process {
$obj.Add("$($col.Name)-$_", $values[$_ - 1])`
}
} else {
$obj.Add($_.Name, $row."$($_.Name)")
}
}
New-Object PSObject -Property $obj
} | Format-Table -auto
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
LucD,
it worked, but I am unable to get the VM which dont have tags in the output.
That is normal and as deigned.
This script was created to list VMs with tags.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
If you want a report for all VMs, and list the Tags for those VMs that have one, you should like at other scripts in this community.
Like for example Re: Export vm tags and vm inventory details together
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Hi LucD,
Using the above link, I am able to get the output as below, but how can I get the category and tags in columns
Current Output
"vCenter","Folder","Name","IP Address","VM PowerState","OS","TagCategories","Tags"
"192.27.10.10","Testing","HADOOP01",,"PoweredOff",,"Department|Owner|Application","Ops|Karl|Hadoop"
Looking for Output as below
"vCenter","Folder","Name","IP Address","VM PowerState","OS","Department","Owner","Application"
"192.27.10.10","Testing","HADOOP01",,"PoweredOff",,,"Ops","Karl","Hadoop"
Then you could do something like this.
You can add properties at your hearth's desire in the [ordered] block, the tag categories are added in the foreach loop (tag(s) may be present or not)
Get-VM -PipelineVariable vm | ForEach-Object -Process {
$obj = [ordered]@{
VM = $vm.Name
Folder = $vm.Folder.Name
OSGuest = $vm.Guest.OSFullName
IP = $vm.Guest.IPAddress -join '|'
}
$tCat | ForEach-Object -Process {
$obj.Add($_.Name,(Get-TagAssignment -Entity $vm -Category $_).Tag.Name -join '|')
}
New-Object -TypeName PSObject -Property $obj
}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
That worked perfect LucD.
Thank you very very much