I am definitely a PowerCLI novice. However, I have a script that is heavily borrowed from http://www.vstrong.info/2013/10/18/vmware-powercli-script-to-query-virtual-machine-events/
What I would like to do is in addition to the values that are retrieved, also receive the Name property of the Tag attribute.
The script I'm currently using is:
$vcenterserver = "myvcenter"
# Check if VMware Snapin is loaded, if not, load it
if ( (Get-PSSnapin -Name VMware.VimAutomation.Core -ErrorAction SilentlyContinue) -eq $null )
{
Add-PSSnapin VMware.VimAutomation.Core
}
Connect-VIServer -server $vcenterserver
$CDT = Get-Date
Get-VM -Name testvm |`
Get-VIEvent -Types Info -Start $CDT.AddDays(-30) -Finish $CDT |`
Where {`
$_.Gettype().Name -eq "VmBeingDeployedEvent"`
-or $_.Gettype().Name -eq "VmCreatedEvent"`
-or $_.Gettype().Name -eq "VmRegisteredEvent"} |`
Select UserName, CreatedTime, FullFormattedMessage, @{ Name="VM"; Expression={$_.Vm.Name}}, @{ Name="RC Code"; Expression={$_.Vm.Tag}}
It retrieves the correct values except for the Tag for the VM. I've also tried @{ Name="RC Code"; Expression={$_.Vm.Tag.Name}}
I can retrieve the value in a tabular format by using the following command, but can't seem to just pull the Name property:
Get-VM -Name testvm | Get-TagAssignment -Category RCCode | Select-Object -ExpandProperty Tag
Name Category Description
---- -------- -----------
10031 RCCode Technical Serv...
Any help would be appreciated, thanks!
Ok so try this. I haven't tested it yet but it should do it for you:
Select UserName, CreatedTime, FullFormattedMessage, @{ Name="VM"; Expression={$_.Vm.Name}}, @{ Name="RC Code"; Expression={$vm = ($_.VM.Name); Get-TagAssignment | where {$_.Entity -like ("$VM")}}} | Export-Csv c:\Temp\csv.csv -NoTypeInformation -UseCulture
The issue was that since the RC Code expression was running essentially a new query, the $_.VM.Name was not relevant for the entity. Instead I just turned the previous $_.VM.Name into a variable that is referenced in the cmdlet that followed. let me know if that works for you.
Try this @{ Name="RC Code"; Expression={Get-TagAssignment | where {$_.Entity -like "MGMT-VCO"}}}
Where "MGMT-VCO" is the name of your virtual machine
Brian,
Thanks for the response. I am able to pull the Tag along with some other info by using the following:
Select UserName, CreatedTime, FullFormattedMessage, @{ Name="VM"; Expression={$_.Vm.Name}}, @{ Name="RC Code"; Expression={Get-TagAssignment | where {$_.Entity -like "testvm"}}}
Which returns the following output:
UserName : DOMAIN\someuser CreatedTime : 6/2/2014 9:35:56 AM FullFormattedMessage : Deploying testvm on host
myhost in My Datacenter from template mytemplate VM : testvm RC Code : [RCCode/10031] testvm
Which is good. I would prefer just the value of the Tag, 10031, but I can work with the whole value.
The only issue I have is that I'm not running it against a named VM, I am passing it a variable, which is in $_.Vm.Name. I tried the following, but receive blank output for the "RC Code":
Select UserName, CreatedTime, FullFormattedMessage, @{ Name="VM"; Expression={$_.Vm.Name}}, @{ Name="RC Code"; Expression={Get-TagAssignment | where {$_.Entity -like "$_.Vm.Name"}}}
Thanks!
so what's the full script? Shouldn't be hard for us to tweak but the part you pasted above isn't enough info for me to go on
Fair enough, sorry about that . Here is the full script (I just renamed some of the sensitive info):
$vcenterserver = "myvcenter"
# $recipients = "myemail@domain.com", "myemail2@domain.com"
# Check if VMware Snapin is loaded, if not, load it
if ( (Get-PSSnapin -Name VMware.VimAutomation.Core -ErrorAction SilentlyContinue) -eq $null )
{
Add-PSSnapin VMware.VimAutomation.Core
}
Connect-VIServer -server $vcenterserver
$CDT = Get-Date # CDT stands for 'Current Date and Time'
Get-VM |`
Get-VIEvent -Types Info -Start $CDT.AddDays(-180) -Finish $CDT |`
Where {`
$_.Gettype().Name -eq "VmBeingDeployedEvent"`
-or $_.Gettype().Name -eq "VmCreatedEvent"`
-or $_.Gettype().Name -eq "VmRegisteredEvent"} |`
Select UserName, CreatedTime, FullFormattedMessage, @{ Name="VM"; Expression={$_.Vm.Name}}, @{ Name="RC Code"; Expression={Get-TagAssignment | where {$_.Entity -like "$_.Vm.Name"}}} | Export-Csv vms_created_report.csv -NoTypeInformation -UseCulture
# Send-MailMessage -SmtpServer smtp.host.com -To $recipients -From server@domain.com -Subject "VMs Created Past 6 months" -Body "Attached is a list of VMs created in the past 6 months: vms_created_report.csv" -attachment vms_created_report.csv
Thanks again.
Ok so try this. I haven't tested it yet but it should do it for you:
Select UserName, CreatedTime, FullFormattedMessage, @{ Name="VM"; Expression={$_.Vm.Name}}, @{ Name="RC Code"; Expression={$vm = ($_.VM.Name); Get-TagAssignment | where {$_.Entity -like ("$VM")}}} | Export-Csv c:\Temp\csv.csv -NoTypeInformation -UseCulture
The issue was that since the RC Code expression was running essentially a new query, the $_.VM.Name was not relevant for the entity. Instead I just turned the previous $_.VM.Name into a variable that is referenced in the cmdlet that followed. let me know if that works for you.
That did it, thanks! Ideally, we could return just the value of the Tag, but I'll take it. I can easily sort by that to see which Department is responsible.
Here is the full script:
$vcenterserver = "myvcenter"
# $recipients = "myemail@domain.com", "myemail2@domain.com"
# Check if VMware Snapin is loaded, if not, load it
if ( (Get-PSSnapin -Name VMware.VimAutomation.Core -ErrorAction SilentlyContinue) -eq $null )
{
Add-PSSnapin VMware.VimAutomation.Core
}
Connect-VIServer -server $vcenterserver
$CDT = Get-Date # CDT stands for 'Current Date and Time'
Get-VM |`
Get-VIEvent -Types Info -Start $CDT.AddDays(-180) -Finish $CDT |`
Where {`
$_.Gettype().Name -eq "VmBeingDeployedEvent"`
-or $_.Gettype().Name -eq "VmCreatedEvent"`
-or $_.Gettype().Name -eq "VmRegisteredEvent"} |`
Select UserName, CreatedTime, FullFormattedMessage, @{ Name="VM"; Expression={$_.Vm.Name}}, @{ Name="RC Code"; Expression={$vm = ($_.VM.Name); Get-TagAssignment | where {$_.Entity -like ("$VM")}}} | Export-Csv vms_created_report.csv -NoTypeInformation -UseCulture
# Send-MailMessage -SmtpServer smtp.host.com -To $recipients -From server@domain.com -Subject "VMs Created Past 6 months" -Body "Attached is a list of VMs created in the past 6 months: vms_created_report.csv" -attachment vms_created_report.csv
Thanks again for your help!