Morning
I am fairly new to the PoweShell side of things and have been struggling for the last few days to finish off what i thought would be an easy script.
We are running around 9 Virtual Centres at the moment due to the large number of VDI's we have. The support team are having trouble working out with VC to go to when they need console access.
I have created the following script to get the info the team have requested, but i am stuck in two places.
I can get the script to run fine on a single VC but as soon as i try to run it against more it fails. The script shows the single access
When the script runs is there any way of adding the virtual centre server name as an output field ? This has been requested as the teams use this as a filter field as well
Any help on this would stop me tearing my hair out and be greatly appreciated
Cheers
James
$server = "xxxxxx"
$user = "xxxxxx"
$pwd = "xxxxx"
*
Connect-VIServer $server -User $user -Password $pwd
*get-vm | select @{Name="Host"; Expression={$_.Host.Name}}, Name, Memory, NumCpu, Description, PowerState, @{Name="MAC"; expression={foreach($nic in (Get-View* $_.ID).guest.net) {$nic.macAddress}}} | export-csv c:\temp\output.csv*
*
Okay, let's clean it up completely and use objects where possible. That way, it's much easier. Here's what I'd do:
$OutputPath = "c:\temp\output.csv" $VCServers = @() $VCServer = "" | Select Name, User, Password $VCServer.Name = "MyVC1" $VCServer.User = "Admin1" $VCServer.Password = "Pass1" $VCServers += $VCServer $VCServer = "" | Select Name, User, Password $VCServer.Name = "MyVC2" $VCServer.User = "Admin2" $VCServer.Password = "Pass2" $VCServers += $VCServer $VCServer = "" | Select Name, User, Password $VCServer.Name = "MyVC3" $VCServer.User = "Admin3" $VCServer.Password = "Pass3" $VCServers += $VCServer $MyCol = @() ForEach ($VC in $VCServers) { Connect-VIServer $VC.Name -User $VC.User -Password $VC.Password ForEach ($VM in Get-VM) { $myObj = "" | Select VC, Host, VM, Memory, NumCpu, Description, PowerState, MAC $myObj.VC = $VC.Name $myObj.Host = $VM.Host.Name $myObj.VM = $VM.Name $myObj.Memory = $VM.Memory $myObj.NumCpu = $VM.NumCpu $myObj.Description = $VM.Description $myObj.PowerState = $VM.PowerState $myObj.MAC = (($VM | Get-View).Guest.Net | ForEach {$_.MacAddress} $myCol += $myObj } Disconnect-VIServer -Confirm:$False } $myCol | Export-Csv $OutputPath
I think the MAC thing will not work this way, because if there are multiple nics, this will be an array and the csv will show the type instead of the contents. But give it a go.
Hugo
Message was edited by: hugopeeters
Reason: Removed unintended line break in code.
Hugo
Many thanks for the quick response
The script is great runs like a dream appart from not filling anything in the memory colum in the output file, any ideas ?
Oops, I think the property is MemoryMB or something. Created the script off the top of my head. No test-env here, because I'm still in Cannes for VMworld
If you prefer GB, try this:
$myObj.MemoryGB = [math]::Round(($_.MemoryMB * 1MB / 1GB),0)
Cool, huh?
Hugo
Oops doublepost, sorry
Message was edited by: hugopeeters
Hugo
thanks for all the help especially as you were at VMWorld
All running perfectly now and the reports are flowing