Sure, try like this
$vcenters = 'VC1','VC2','VC3'
$user = 'xyz'
$pswd = 'abc'
$cred = New-Object System.Management.Automation.PSCredential($user, (ConvertTo-SecureString $pswd -AsPlainText -Force))
Connect-VIServer -Server $vcenters -Credential $cred
Get-Cluster -PipelineVariable cluster |
Get-ResourcePool |
Select @{N='vCenter';E={([System.Uri]$cluster.ExtensionData.Client.ServiceUrl).Host}},
@{N='Cluster';E={$cluster.Name}},
@{N='ResourcePool';E={$_.Name}},
@{N='VMCount';E={(Get-VM -Location $_).Count}} |
Export-Csv -Path .\report.csv -NoTypeInformation -UseCulture
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
I like to begin all my scripts with :
Set-PowerCLIConfiguration -Scope Session `
-DefaultVIServerMode Multiple `
-Confirm:$false | Out-NullI need to run my scripts in various locations which may be configured to be single or multi-server for various reasons. This way my scripts work the way I intend them to without altering the environment I run them in.
There are other options too which are sometimes useful too:
Set-PowerCLIConfiguration -Scope Session `
-ParticipateInCeip $false `
-InvalidCertificateAction Ignore `
-DisplayDeprecationWarnings $false `
-DefaultVIServerMode Multiple `
-Confirm:$false | Out-NullThis tends to be the full commandlet near the beginning of my scripts.
Provided the credentials for all your vCenters are the same, you could do
$vcenters = 'VC1','VC2','VC3'
$user = 'xyz'
$pswd = 'abc'
$cred = New-Object System.Management.Automation.PSCredential($user, (ConvertTo-SecureString $pswd -AsPlainText -Force))
Connect-VIServer -Server $vcenters -Credential $cred
Get-ResourcePool |
Select Name,
@{N='VMCount';E={(Get-VM -Location $_).Count}} |
Export-Csv -Path .\report.csv -NoTypeInformation -UseCulture
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Hi LucD,
Thanks for your help.
As I'm running it for multiple VC's, is it possible to add columns for the Cluster and vCenter Name so that I can sort report with VC.
Sure, try like this
$vcenters = 'VC1','VC2','VC3'
$user = 'xyz'
$pswd = 'abc'
$cred = New-Object System.Management.Automation.PSCredential($user, (ConvertTo-SecureString $pswd -AsPlainText -Force))
Connect-VIServer -Server $vcenters -Credential $cred
Get-Cluster -PipelineVariable cluster |
Get-ResourcePool |
Select @{N='vCenter';E={([System.Uri]$cluster.ExtensionData.Client.ServiceUrl).Host}},
@{N='Cluster';E={$cluster.Name}},
@{N='ResourcePool';E={$_.Name}},
@{N='VMCount';E={(Get-VM -Location $_).Count}} |
Export-Csv -Path .\report.csv -NoTypeInformation -UseCulture
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Hi LucD,
Thank you for the update.
I tried the script, however it's capturing the data only for last VC which I enter in the $vcenters.
Anything I'm missing or doing wrong?
You probably have the DefaultVIServerMode set to Single.
Check with Get-PowerCLIConfiguration.
Change to Multiple with Set-PowerCLIConfiguration.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
I like to begin all my scripts with :
Set-PowerCLIConfiguration -Scope Session `
-DefaultVIServerMode Multiple `
-Confirm:$false | Out-NullI need to run my scripts in various locations which may be configured to be single or multi-server for various reasons. This way my scripts work the way I intend them to without altering the environment I run them in.
There are other options too which are sometimes useful too:
Set-PowerCLIConfiguration -Scope Session `
-ParticipateInCeip $false `
-InvalidCertificateAction Ignore `
-DisplayDeprecationWarnings $false `
-DefaultVIServerMode Multiple `
-Confirm:$false | Out-NullThis tends to be the full commandlet near the beginning of my scripts.
Thank you @LucD & @StephenMoll for your inputs.
It's working as expected.
