Hi All,
Im new to powercli, however i would like to create a script that do a of my dvSwitchs,
I would also like to create a script that can import the backup of the dvSwitch.
I would prefer a dump of the dvSwitches into a csv file per dvswitch.
In my setup, im only interested in the portgroup name and the vlan id
Howto do that?
Regards Kim
Try something like this.
It will handle regular VLanIds, VLanId ranges and pvLans
&{foreach($dvSw in Get-VirtualSwitch -Distributed){ Get-VirtualPortGroup -VirtualSwitch $dvSw | Select @{N="dvSw";E={$dvSw.Name}},Name,
@{N="VLanId";E={ if($_.ExtensionData.Config.DefaultPortConfig.Vlan -is [VMware.Vim.VmwareDistributedVirtualSwitchPvlanSpec]){ $_.ExtensionData.Config.DefaultPortConfig.Vlan.PvLanId
} elseif($_.ExtensionData.Config.DefaultPortConfig.Vlan.VLanId -is [VMware.Vim.NumericRange[]]){ [string]::Join(',',($_.ExtensionData.Config.DefaultPortConfig.Vlan.VLanId | %{ [string]$_.Start + "-" + [string]$_.End
})) } else{ $_.ExtensionData.Config.DefaultPortConfig.Vlan.VLanId
} }} }} | Export-Csv C:\report.csv -NoTypeInformation -UseCulture
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Try something like this.
It will handle regular VLanIds, VLanId ranges and pvLans
&{foreach($dvSw in Get-VirtualSwitch -Distributed){ Get-VirtualPortGroup -VirtualSwitch $dvSw | Select @{N="dvSw";E={$dvSw.Name}},Name,
@{N="VLanId";E={ if($_.ExtensionData.Config.DefaultPortConfig.Vlan -is [VMware.Vim.VmwareDistributedVirtualSwitchPvlanSpec]){ $_.ExtensionData.Config.DefaultPortConfig.Vlan.PvLanId
} elseif($_.ExtensionData.Config.DefaultPortConfig.Vlan.VLanId -is [VMware.Vim.NumericRange[]]){ [string]::Join(',',($_.ExtensionData.Config.DefaultPortConfig.Vlan.VLanId | %{ [string]$_.Start + "-" + [string]$_.End
})) } else{ $_.ExtensionData.Config.DefaultPortConfig.Vlan.VLanId
} }} }} | Export-Csv C:\report.csv -NoTypeInformation -UseCulture
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Thanks Lucd,
I just realised that I also need number of ports per portgroup and numbers of ports in use per portgroup
regards kim
No problem, try this
&{foreach($dvSw in Get-VirtualSwitch -Distributed){ Get-VirtualPortGroup -VirtualSwitch $dvSw | Select @{N="dvSw";E={$dvSw.Name}},Name,NumPorts,
@{N="Ports used";E={@($_.ExtensionData.Vm).Count}},
@{N="VLanId";E={
if($_.ExtensionData.Config.DefaultPortConfig.Vlan -is [VMware.Vim.VmwareDistributedVirtualSwitchPvlanSpec]){ $_.ExtensionData.Config.DefaultPortConfig.Vlan.PvLanId
} elseif($_.ExtensionData.Config.DefaultPortConfig.Vlan.VLanId -is [VMware.Vim.NumericRange[]]){ [string]::Join(',',($_.ExtensionData.Config.DefaultPortConfig.Vlan.VLanId | %{ [string]$_.Start + "-" + [string]$_.End
})) } else{ $_.ExtensionData.Config.DefaultPortConfig.Vlan.VLanId
} }} }} | Export-Csv C:\report.csv -NoTypeInformation -UseCulture
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Hi Lucd,
I have created a script to restore the dvs based on your http://www.lucd.info/2009/10/12/dvswitch-scripting-part-2-dvportgroup/
That work fine, however, if i want to import the csv file again with something simple like
dvSw;Name;NumPorts;Ports used;VLanId
------------------------------------
Prod-Switch;Anne Bogholder Net;4;1;200
Prod-Switch;Apokteren Net;4;1;229
That echos absolutly nothing. Whats wrong?
regard kim
Judging from the CSV file extract your regional settings are not default En-US.
That's what the UseCulture parameter takes care of.
Try it like this
$lines = Import-Csv c:\report.csv -UseCulture
foreach ($line in $lines) { $line.Name
}
Note that you do not have to use "echo" to display something on the console in PowerShell.
Just place the value you want to see on the pipeline.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Hi Lucd,
It seems like, when I create a ned port group, then all uplink adaptors is listed under "unused uplinks"
How do I put e.g. dvUplink1 and dvUplink2 in "Active Uplinks"
Regards Kim
That happens when you assign a VMhost to the dvSwitch
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Net really.
I just created a new vcenter version 5.01b.
I have 2 hosts in it. If i create a new port group from my script, then both dvuplinks is listed as unused.
If I create a vm and assign it the new port group, the it will not work, since the dvuplinks is still listed as unused.
any ideas?
\\\Kim
After you create the dvSwitch you need to assign the ESXi hosts to it.
In the vSphere client go to the ESXi server, Configuration, Networking, vSphere Distributed Switch and click Manage Physical Adapters.
Thta's where you link a physcial NIC on the ESXi server to an uplink on a dvSwitch
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Yes, i know, that i have done.
The problem is, when i create a new portgroup with the script, the the dvuplinks are listed as unused.
If i do the same with the vsphere client, then the dvuplinks are listed as Active Uplinks.
\\\ Kim
Can you show the script you used to include the portgroup ?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
You seem to be missing the part where you connect the ESXi hosts to the dvSwitch.
See for example the Add-dvSwHost function in my dvSwitch scripting – Part 1 – Creation post.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference