my syntax:
Write-Host "Connecting to vCenter"
Write-Host "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
Connect-VIServer "vcenter"
$VMs = Get-Content "C:\Batch\Scripts\poweredoffVMs.txt"
foreach ($VM in $VMs) {
#Days set to -60 to list of VMs that have been shutdown for 2 months and parsing to CSV helps in the sorting.
Get-VIEvent -Start (Get-Date).AddDays(-60) -MaxSamples ([int]::MaxValue) |
where{$_ -is [VMware.Vim.VmPoweredOffEvent] -or
$_ -is [VMware.Vim.VmGuestShutdownEvent]} |
Select CreatedTime,@{N='VM';E={$_.Vm.Name}},UserName,FullFormattedMessage |
Export-CSV -NoTypeInformation C:\Batch\Scripts\poweredoffVMs_info.csv -Append
}
I have seen this error before, it was mostly caused by an incompatibility issue between the PowerCLI version and the vSphere version (6.* or higher).
Against which vSphere version are you running that script?
I suspect not all cmdlets suffer from this incompatibility.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Which PowerCLI version are you using?
Can you do
Get-Module -Name VMware* -ListAvailable
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Hi LucD, this seems due to outdated version?
PS C:\Users\xxxx> Get-Module -Name VMware* -ListAvailable
Directory: C:\Program Files (x86)\VMware\Infrastructure\PowerCLI\Modules
ModuleType Version Name ExportedCommands
---------- ------- ---- ----------------
Binary 6.0.0.0 VMware.DeployAutomation
Binary 6.0.0.0 VMware.ImageBuilder
Binary 6.5.0.4... VMware.VimAutomation.Cis.Core
Binary 6.5.0.4... VMware.VimAutomation.Cloud
Manifest 6.5.0.4... VMware.VimAutomation.Common
Binary 6.5.0.2... VMware.VimAutomation.Core HookGetViewAutoCompleter
Binary 6.0.0.0 VMware.VimAutomation.HA
Binary 7.0.2.4... VMware.VimAutomation.HorizonView
Binary 6.5.0.4... VMware.VimAutomation.License
Binary 6.5.0.4... VMware.VimAutomation.PCloud
Manifest 6.5.0.4... VMware.VimAutomation.Sdk Get-PSVersion
Binary 6.5.0.4... VMware.VimAutomation.Storage
Binary 6.5.0.4... VMware.VimAutomation.Vds
Binary 6.5.0.4... VMware.VimAutomation.vROps
Binary 6.0.0.0 VMware.VumAutomation
I'm afraid that could indeed be the case.
It looks as if you have an older version that was installed with an MSI file.
You will first have to uninstall that package (from Programs and Features) and then install the latest version from the PSGallery.
See Welcome PowerCLI to the PowerShell Gallery – Install Process Updates for more details on the procedure to use.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Noted, thanks @LucD. But is there any reason why this happened only to this new script I was trying to construct but not to other existing script on the same server I executed it from?
I have seen this error before, it was mostly caused by an incompatibility issue between the PowerCLI version and the vSphere version (6.* or higher).
Against which vSphere version are you running that script?
I suspect not all cmdlets suffer from this incompatibility.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
understood, that make sense of certain cmdlet with vsphere.
I will upgrade the powerCLI first as what you recommended.
Thanks LucD for your promptly replies, appreciated it!
Hi LucD,
I managed to run the script, but somehow it run without what I expected.
From the txt file, I have listed VM that I wanted to look for its information who powered it off,
but what I was getting was list of other VMs that was not even listed at all.
For trial run, I just use a VM, but was getting lengthy list of powered off VM in the output report.
Can you please point me where did I do wrong?
You didn't use the Entity parameter on the Get-VIEvent cmdlet, as a result you got back all events.
Try like this
Write-Host "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
Connect-VIServer "vcenter"
$VMs = Get-VM -Name (Get-Content "C:\Batch\Scripts\poweredoffVMs.txt")
#Days set to -60 to list of VMs that have been shutdown for 2 months and parsing to CSV helps in the sorting.
Get-VIEvent -Entity $VMs -Start (Get-Date).AddDays(-60) -MaxSamples ([int]::MaxValue) |
where{$_ -is [VMware.Vim.VmPoweredOffEvent] -or $_ -is [VMware.Vim.VmGuestShutdownEvent]} |
Select CreatedTime,@{N='VM';E={$_.Vm.Name}},UserName,FullFormattedMessage |
Export-CSV -NoTypeInformation C:\Batch\Scripts\poweredoffVMs_info.csv -Append
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference