Hello. I'm trying to get this script to populate a csv file with vCenter names, start of Mac address range and end of Mac address range per vCenter server provided in the vclist.csv file. Here's what I have for now. When I run the script, I'm being prompted for an input object. The script appears to just populate the result vclist2.csv file with the length of the character. Can someone please help? Sorry I'm stil in the learning phase of Pwershell/PowerCLI so any help here would be greatly appreciated. Thank you.
Below is the script
*********************************************
$vclist = import-csv .\vclist.csv
Foreach ($vc in $vclist) {
$vCenterSettings = Get-View -Id 'OptionManager-VpxSettings'
$macaddress_begin = ($vCenterSettings.Setting | Where-Object { $_.Key -eq "config.vpxd.macAllocScheme.rangeScheme.range[0].begin"}).Value
$macaddress_end = ($vCenterSettings.Setting | Where-Object { $_.Key -eq "config.vpxd.macAllocScheme.rangeScheme.range[0].end"}).Value
Export-Csv -Path "d:\vclist2.csv"
}
Yes, you can add a Connect-VIServer and a Disconnect-VIServer to the script.
The problem might be the User and Password.
If you don't want to provide that on the Connect-VIServer line in the script, you might want to use the New-VICredentialStoreItem cmdlet.
That allows you to do the connection without specifying a User/Password in the script.
ForEach-Object -Process {
Connect-VIServer -Server $_.VC | Out-Null
$vCenterSettings = Get-View -Id 'OptionManager-VpxSettings' -Server $_.VC
New-Object PSObject -Property @{
'macaddress_begin' = ($vCenterSettings.Setting | Where-Object { $_.Key -eq "config.vpxd.macAllocScheme.rangeScheme.range[0].begin" }).Value
'macaddress_end' = ($vCenterSettings.Setting | Where-Object { $_.Key -eq "config.vpxd.macAllocScheme.rangeScheme.range[0].end" }).Value
}
Disconnect-VIServer -Server $_.VC -Confirm:$false
} |
Export-Csv -Path ".\vclist2.csv" -NoTypeInformation -UseCulture
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
You have to provide some input to the Export-Csv cmdlet.
That can be done through the InputObject parameter (which also accepts pipeline input).
In the following, I use the Foreach-Object cmdlet, since that places objects in the pipeline.
I also assume that you are connected to all vCenters before you run this script?
In that case, you have to provide the Server parameter on the Get-View cmdlet.
If yes, you will have to provide the column name in the code (I assume that is 'Name').
Something like this
ForEach-Object -Process {
$vCenterSettings = Get-View -Id 'OptionManager-VpxSettings' -Server $_.Name
New-Object PSObject -Property @{
'macaddress_begin' = ($vCenterSettings.Setting | Where-Object { $_.Key -eq "config.vpxd.macAllocScheme.rangeScheme.range[0].begin"}).Value
'macaddress_end' = ($vCenterSettings.Setting | Where-Object { $_.Key -eq "config.vpxd.macAllocScheme.rangeScheme.range[0].end"}).Value
}
} |
Export-Csv -Path ".\vclist2.csv" -NoTypeInformation -UseCulture
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
LucD. Thanks so much.
Is there a way to run the script without first connecting to all the vCenters?
Essentially, what I do is log on to a jump host with a userid/password that has admin rights to all the vCenters and want to use that session for authentication.
The vCenter server FQDN names will be specified in the vclist.csv file under a "VC" or "Server" heading.
Is that possible?
Yes, you can add a Connect-VIServer and a Disconnect-VIServer to the script.
The problem might be the User and Password.
If you don't want to provide that on the Connect-VIServer line in the script, you might want to use the New-VICredentialStoreItem cmdlet.
That allows you to do the connection without specifying a User/Password in the script.
ForEach-Object -Process {
Connect-VIServer -Server $_.VC | Out-Null
$vCenterSettings = Get-View -Id 'OptionManager-VpxSettings' -Server $_.VC
New-Object PSObject -Property @{
'macaddress_begin' = ($vCenterSettings.Setting | Where-Object { $_.Key -eq "config.vpxd.macAllocScheme.rangeScheme.range[0].begin" }).Value
'macaddress_end' = ($vCenterSettings.Setting | Where-Object { $_.Key -eq "config.vpxd.macAllocScheme.rangeScheme.range[0].end" }).Value
}
Disconnect-VIServer -Server $_.VC -Confirm:$false
} |
Export-Csv -Path ".\vclist2.csv" -NoTypeInformation -UseCulture
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Hi LucD. My apologies for not responding sooner. I was out of town for a few days. I wanted to thank you and let you know that your solution helped me tremendously. Thank you very much.