VMware Cloud Community
FrankG203
Enthusiast
Enthusiast

get-folder get-vm & get-tag

I was just trying to create a simple script and for some reason it is causing me grief.

Basically all I want is a script to go out to vcenter, grab a list of all VM folders, list out all VMs in that folder and a specific tag.

$folderlist = Get-Folder -Type VM -Name "FolderName"

foreach ($Folder in $folderlist){
$Folder.Name
$Folder | Get-VM | Get-TagAssignment -Category "Product" | Select-Object Entity, tag

}

This does the job > lists the folder name > under the folder name are the VMs in that folder with a column to the right with the tag. For some reason I am having an issue "dotting" into tag and removing the category and displaying just the tag. Any help would be appreciated.

0 Kudos
6 Replies
LucD
Leadership
Leadership

Try changing that to a calculated property.

Select-Object @{N='Entity';E={$_.Entity.Name}}, 
 @{N='Tag';E={$_.Tag.Name}}


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
FrankG203
Enthusiast
Enthusiast

Thank you again LucD I knew it had something to do with the expressions, I need serious work on those.

0 Kudos
FrankG203
Enthusiast
Enthusiast

I am outing to a text file, there is a blank line in between the VM folder name and list of VMs, I tried using "trim" but that seems to be removing all the lines, I tried using -replace "`n",''  but I was unsuccessful. Any ideas?  Thanks

0 Kudos
LucD
Leadership
Leadership

You are mixing implicit Write-Host with Select-Object.
You can try suppressing the NewLine on the Write-Host.
Something like this perhaps

$folderlist = Get-Folder -Type VM -Name "FolderName"

foreach ($Folder in $folderlist) {
    Write-Host "$($Folder.Name)" -NoNewline
    $Folder | Get-VM | Get-TagAssignment -Category "Product" | 
    Select-Object @{N='Entity';E={$_.Entity.Name}}, 
        @{N='Tag';E={$_.Tag.Name}}
}


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
FrankG203
Enthusiast
Enthusiast

Thanks for the reply the output to txt does not seem to like that now.  Good on the console not great on output.  Its okay though I would rather not waste my ask on this issue. I'm sure I will have other more important ones at some point. Thanks again.

0 Kudos
LucD
Leadership
Leadership

It depends on how you send the output to that text file.
Just capturing the console output is mostly not the ideal method.
Piping the text you want to Out-File is most of the time better


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos