VMware Cloud Community
Sha_roy
Contributor
Contributor
Jump to solution

PowerCLI Script

Hello,

I'm searching for a powerCLI script that can log in to multiple vCenters simultaneously, record the name and number of VMs in each Resource Pool, and export the results in CSV format.

Please, someone lend a hand.

@LucD 

Labels (2)
Reply
0 Kudos
2 Solutions

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

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

View solution in original post

Reply
0 Kudos
StephenMoll
Expert
Expert
Jump to solution

I like to begin all my scripts with :

Set-PowerCLIConfiguration -Scope Session `
        -DefaultVIServerMode Multiple `
        -Confirm:$false | Out-Null

I 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-Null

This tends to be the full commandlet near the beginning of my scripts.

 

View solution in original post

Reply
0 Kudos
8 Replies
scott28tt
VMware Employee
VMware Employee
Jump to solution

I have reported your post to the moderators, asking them to move it to the PowerCLI area.
Once you can see that area, it's probably best to do a search rather than expecting someone to write code for you.

-------------------------------------------------------------------------------------------------------------------------------------------------------------

Although I am a VMware employee I contribute to VMware Communities voluntarily (ie. not in any official capacity)
VMware Training & Certification blog
LucD
Leadership
Leadership
Jump to solution

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

Sha_roy
Contributor
Contributor
Jump to solution

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.

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

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

Reply
0 Kudos
Sha_roy
Contributor
Contributor
Jump to solution

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?

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

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

Reply
0 Kudos
StephenMoll
Expert
Expert
Jump to solution

I like to begin all my scripts with :

Set-PowerCLIConfiguration -Scope Session `
        -DefaultVIServerMode Multiple `
        -Confirm:$false | Out-Null

I 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-Null

This tends to be the full commandlet near the beginning of my scripts.

 

Reply
0 Kudos
Sha_roy
Contributor
Contributor
Jump to solution

Thank you @LucD & @StephenMoll for your inputs.

It's working as expected. 

Reply
0 Kudos