VMware Cloud Community
Tat-Man
Enthusiast
Enthusiast

Tag report for All virtual Servers

Good Afternoon.

I have googled and attempted to cut and mix scripts but to no avail.

I am after a TAG report that outputs into to html and emails it to an address.

In my report i am after the below info.

DataCentre, Resource Pool, Virtual Sever, Assigned Tag, Category.  Now there maybe multiple Assigned Tag, Categories to a single Virtual Server but want to capture them all.

foreach ($vc in $viServers){

    Connect-VIServer $vc -ErrorAction SilentlyContinue -WarningAction SilentlyContinue -user $username -Password $password

   

 

$output = $reportlocation

 

$time =  Get-Date -UFormat "%d_%b_%Y"

 

if (!(Test-Path  $output)){New-Item ($output) -ItemType directory}

 

foreach ($vc in $viservers)

 

{

 

$report = @()

 

$vms = Get-VM | where {$_.name -notlike "*-*-*-*-*"}

 

foreach ($vm in $vms){

 

 }

 

 $VMrow = New-Object PSObject -Property @{"Name" = $VM.Name

 

                                          "OwnedByTag" =  (get-tagassignment $vm -Category OwnedBy -ErrorAction SilentlyContinue).tag.Name

 

                                          "ManagedByTag" =  (get-tagassignment $vm -Category ManagedBy -ErrorAction SilentlyContinue).tag.Name

 

                                          "ClientSoldToCodeTag" =  (get-tagassignment $vm -Category ClientSoldToCode -ErrorAction SilentlyContinue).tag.Name

 

                                          "ClientShippedToCodeTag" =  (get-tagassignment $vm -Category ClientShippedToCode -ErrorAction SilentlyContinue).tag.Name

 

                                          "InternalCustomerCodeTag" =  (get-tagassignment $vm -Category InternalCustomerCode -ErrorAction SilentlyContinue).tag.Name

 

                                          "DisasterReceoveryTag" =  (get-tagassignment $vm -Category DisasterReceovery -ErrorAction SilentlyContinue).tag.Name

 

                                          "MemoryGB" = $vm.MemoryGB

 

                                          "vCPU" = $vm.NumCpu

 

                                        }

 

$report += $VMrow

 

}

 

$report | Select-Object Name,OwnedByTag,ManagedByTag,ClinetSoldToCodeTag,ClientShippedToCodeTag,InternalCustomerCodeTag,DisasterRecoveryTag,vCPU,MemoryGB | Sort-Object ManagedByTag | Export-Csv -Path "$output\$vcserver-tag-report_$time.csv" -notypeinformation

 

Send-MailMessage -From $emailsource -to $emaildest -subject "TAG report for $vcserver" -SmtpServer $emailserver -body "Please find attached tag report for $vcserver" -Attachments "$output\$vcserver-tag-report_$time.csv"

 

disconnect-viserver * -Confirm:$false

 

Reply
0 Kudos
2 Replies
jrmunday
Commander
Commander

You've got an extra curly bracket that should be removed. At the moment your script is looping though each VM, but not doing anything as there are no actions within the "foreach VM loop".

 

Change this section ....

foreach ($vm in $vms){

}

 

To this ... 

foreach ($vm in $vms){

# }

... or just delete the line that I've commented out with a #.

 

And you'll need to add an additional closing curly brackets at the end to close the "for each VC loop".

 

Cheers,

Jon

vExpert 2014 - 2022 | VCP6-DCV | http://www.jonmunday.net | @JonMunday77
Tat-Man
Enthusiast
Enthusiast

Thank you.  Made the change and seems to be working.

Reply
0 Kudos