VMware Cloud Community
billyjoel
Enthusiast
Enthusiast
Jump to solution

Script - getting same result/output several times

Hi all,

I have the below script, I just want it to go thru the servers.txt(30 servers) list and print the VM names within it to test that actualli it is doing so.

It's working fine but the thing is that it's goinf thru the list several times so I'm getting the VM names printed several times and I want it to go just once.

For example Instead of getting the 30 server names in the output Im getting 90, 3 times the list of 30.

what I'm missing here?

$vcenters = Get-Content C:\Users\user.name\Desktop\vcenters.txt

## Credentials

$credential = Get-Credential -Message "Enter vCenter Credentials."

foreach ($vcenter in $vcenters){

    Write-Output "connecting to vcenter $vcenter"

    Connect-VIServer $vcenter -Credential $credential

    if (!(($global:DefaultVIServer).name -eq $vcenter)){

        Write-Output "not connected to vcenter $vcenter"

        exit

        } }

$machines = (Get-Content C:\Users\user.name\Desktop\servers.txt)

$vmObj = Get-vm $machines

foreach($active in $vmObj){

if($active.PowerState -eq "PoweredOn"){

echo $vmObj | select name

}

}

Reply
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Can you give this version a try?

$vcenters = Get-Content C:\Users\user.name\Desktop\vcenters.txt

## Credentials

$credential = Get-Credential -Message "Enter vCenter Credentials."

$machines = (Get-Content C:\Users\user.name\Desktop\servers.txt)

foreach ($vcenter in $vcenters) {

    try {

        Connect-VIServer $vcenter -Credential $credential -ErrorAction Stop

    }

    catch {

        Write-Output "not connected to vcenter $vcenter"

        exit

    }


    Get-VM $machines -Server $vcenter |

    Where-Object { $_.PowerState -eq 'PoweredOn' } |

    Select-Object name

}


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

Reply
0 Kudos
4 Replies
LucD
Leadership
Leadership
Jump to solution

Are your vCenters in Linked Mode?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
billyjoel
Enthusiast
Enthusiast
Jump to solution

No they are not.

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Can you give this version a try?

$vcenters = Get-Content C:\Users\user.name\Desktop\vcenters.txt

## Credentials

$credential = Get-Credential -Message "Enter vCenter Credentials."

$machines = (Get-Content C:\Users\user.name\Desktop\servers.txt)

foreach ($vcenter in $vcenters) {

    try {

        Connect-VIServer $vcenter -Credential $credential -ErrorAction Stop

    }

    catch {

        Write-Output "not connected to vcenter $vcenter"

        exit

    }


    Get-VM $machines -Server $vcenter |

    Where-Object { $_.PowerState -eq 'PoweredOn' } |

    Select-Object name

}


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
billyjoel
Enthusiast
Enthusiast
Jump to solution

Thank you Smiley Happy

Reply
0 Kudos