foreach new-virtualportgroup
I am creating a virtualswitch and exchanges for several hosts, but when I try this below, I only get the vswitches created on the host computers.
I do however find first created so Exchange hosts but not the other hosts
any idea?
SE connect-VIServer vc
$hostarray = @("host1","host2","host3")
{foreach ($vmhost to $hostarray)
New-VirtualSwitch - VMHost $vmhost - name vSwitch4
New-VirtualPortGroup - vSwitch4-name - VLanId 10 - vlan10 VirtualSwitch confirm: $false
New-VirtualPortGroup - vSwitch4-name vlan15 - VLanId 15 - VirtualSwitch confirm: $false
New-VirtualPortGroup - vSwitch4-name vlan17 - VLanId 17 - VirtualSwitch confirm: $false
New-VirtualPortGroup - vSwitch4-name vlan20 - VLanId 20 - VirtualSwitch confirm: $false
New-VirtualPortGroup - vSwitch4-name - VLanId 30 - vlan30 VirtualSwitch confirm: $false
New-VirtualPortGroup - vSwitch4-name vlan40 - VLanId 40 - VirtualSwitch confirm: $false
New-VirtualPortGroup - vSwitch4-name vlan50 - VLanId 50 - VirtualSwitch confirm: $false
New-VirtualPortGroup - vSWitch4-name vlan60 - VLanId 60 - VirtualSwitch confirm: $false
New-VirtualPortGroup - vSwitch4-name vlan160 - VLanId 160 - VirtualSwitch confirm: $false
New-VirtualPortGroup - vSwitch4-name vlan162 - VLanId 162 - VirtualSwitch confirm: $false
New-VirtualPortGroup - vSWitch4-name vlan220 - VLanId 220 - VirtualSwitch confirm: $false
}
Not in front of a PC with access to test at the moment, but from memory, you need to pass a vSwitch object to New-VirtualPortGroup. So, something like this:
SE connect-VIServer vc
$hostarray = @("host1","host2","host3")
{foreach ($vmhost to $hostarray)
$vSwitch = New VirtualSwitch - VMHost $vmhost - name vSwitch4
New-VirtualPortGroup - $vSwitch - name - VLanId 10 - vlan10 VirtualSwitch confirm: $false
New-VirtualPortGroup - $vSwitch - name vlan15 - VLanId 15 - VirtualSwitch confirm: $false
New-VirtualPortGroup - $vSwitch - name vlan17 - VLanId 17 - VirtualSwitch confirm: $false
New-VirtualPortGroup - $vSwitch - name vlan20 - VLanId 20 - VirtualSwitch confirm: $false
New-VirtualPortGroup - $vSwitch - name - VLanId 30 - vlan30 VirtualSwitch confirm: $false
New-VirtualPortGroup - $vSwitch - name vlan40 - VLanId 40 - VirtualSwitch confirm: $false
New-VirtualPortGroup - $vSwitch - vlan50 - VLanId 50 - name VirtualSwitch confirm: $false
New-VirtualPortGroup - $vSwitch - vlan60 - VLanId 60 - name VirtualSwitch confirm: $false
New-VirtualPortGroup - $vSwitch - name vlan160 - VLanId 160 - VirtualSwitch confirm: $false
New-VirtualPortGroup - $vSwitch - name vlan162 - VLanId 162 - VirtualSwitch confirm: $false
New-VirtualPortGroup - $vSwitch - name vlan220 - VLanId 220 - VirtualSwitch confirm: $false
}
Tags: VMware
Similar Questions
-
Addition of new VirtualPortGroup in a folder.
Hello.
I'm trying to figure out if there is a way to create a new virtualportgroup and then add it to a folder under network in a scritp/oneliner.
Today, I use this to add exchanges:
Get-Cluster "your Cluster | Get-VMHost | Get-VirtualSwitch-name "vSwitch0" '
| New-VirtualPortGroup-name ' new VLAN ' VLanId - 123
For security reasons, we set the permissions on the folders for different groups of users
But now I have manually need to move the new portgroup to the right folder, it forge be nice to be able to do this in the script.
We run ESX4.0 u1
Christian
This is possible, but you need to use the SDK method because the Move-File cmdlet does not yet support the folder 'network '.
This is an example of script
$pgName = 'MyPG2' # Name of the new portgroup $esxName = 'esx41.test.local' # ESX host where the new portgroup is created $tgtFolder = 'MyNet' # Child folder in the network folder $esxImpl = Get-VMHost $esxName # Create portgroup $pgImpl = $esxImpl | Get-VirtualSwitch -Name 'vSwitch1' | New-VirtualPortGroup -Name $pgName -VLanId 777 # Get network folder $esx = $esxImpl | Get-View $dc = Get-Datacenter -VMHost $esxImpl | Get-View $netFolder = Get-View $dc.NetworkFolder # Find portgroup $netFolder.ChildEntity | where {$_.Type -eq 'Network'} | %{ $child = Get-View $_ if($child.Name -eq $pgName){ $pgMoRef = $child.MoRef } } # Move portgroup into folder $netFolder.ChildEntity | where {$_.Type -eq 'Folder'} | %{ $child = Get-View $_ if($child.Name -eq $tgtFolder){ $child.MoveIntoFolder($pgMoRef) } }
____________
Blog: LucD notes
Twitter: lucd22
-
How to define the Type of connection using the New-VirtualPortGroup
Using PowerCLI 4.1, is there way to define the port group (VM or VMKernel) connection type when you create the port group?
Thank you
OK, try it like that.
$VMHostObj = Get-VMHost $ESXHost $vsw = get-virtualswitch -name vSwitch0 -VMHost $VMHostObj New-VMhostNetworkAdapter -VMHost $VMHostObj -VirtualSwitch $vsw -PortGroup "VMotion" -IP 10.110.40.43 -SubnetMask 255.255.255.0 -VMotionEnabled:$true
____________
Blog: LucD notes
Twitter: lucd22
-
addinv VLanId to New-VirtualPortgroup
Hello
I wonder if it is possible to obtain VLanId '64', has added to my vswitch (vmkernel vswitch/portgroup). Also how including gateway address.
Sorry for this stupid question, one beginner, looking for help.
Thank you!
To change the VLANid of the VMkernel portgroup you can use
Get-VMHost
| Get-VirtualPortGroup -Name | Set-VirtualPortGroup -VLanId 64 To change the VMkernel gateway you can do
Get-VMHostNetwork -VMHost (Get-VMHost
) | Set-VMHostNetwork -VMKernelGateway -
Create exchanges with table to hash foreach loop
I am a beginner in PowerShell and PowerCLI.
I would like to create a virtual switch with several exchanges and VLANID and then assign a network adapter
This code works for me:
$esxihost = "esx01.
SE connect-VIServer-Server $esxihost
$vSwitch = New VirtualSwitch - VMHost $esxihost - name vSwitch3
$vPortGroup = New-VirtualPortGroup - $vSwitch - name ADM VLanId VirtualSwitch - 1012
$vPortGroup = New-VirtualPortGroup - $vSwitch - name of the VLanId man VirtualSwitch - 1020
$vPortGroup = New-VirtualPortGroup - $vSwitch - name test1 VirtualSwitch - VLanId 1021
$vPortGroup = New-VirtualPortGroup - $vSwitch - name of test2 VirtualSwitch - VLanId 1022
$vPortGroup = New-VirtualPortGroup - $vSwitch - name test3 VirtualSwitch - VLanId 2000
Game-VirtualSwitch - VirtualSwitch $vswitch - Nic vmnic3 - confirm: $false
But I think something like this would be much more elegant:
$esxihost = "esx01.
SE connect-VIServer-Server $esxihost
$vSwitch = New VirtualSwitch - VMHost $esxihost - name vSwitch3
$vlans = @{'}
'adm' = 1012; `
'man' = 1020; `
'test1' = 1021; `"test2" = 1022; `
"test3" = 2000
}{foreach ($vlans items)
$vPortGroup = news-VirtualPortGroup - $vSwitch - name $vlans VirtualSwitch. Key -VLanId $vlans. Value
}
Game-VirtualSwitch - VirtualSwitch $vswitch - Nic vmnic3 - confirm: $false
But I can't get the foreach loop to work. I'm not sure on the three points marked in bold in the foreach loop. Can someone give me a clue...?
Thank you.
Anders
You store data in a hash table.
You cannot use the ForEach loop, as you would for a regular table, but you can use the GetEnumerator method.
Something like that
$esxihost="esx01" Connect-VIServer -Server $esxihost $vSwitch = New-VirtualSwitch -VMHost $esxihost -Name vSwitch3 $vlans = @{ "adm" = 1012; "man" = 1020; "test1" = 1021; "test2" = 1022; "test3" = 2000} $vlans.GetEnumerator() | %{ $vPortGroup = New-VirtualPortGroup -VirtualSwitch $vSwitch -Name $_.Key -VLanId $_.Value } Set-VirtualSwitch -VirtualSwitch $vswitch -Nic vmnic3 -Confirm:$false
-
Add new portgroup for the entire Cluster
Hello
Currently I have the powercli script to add a new portgroup to switch existing on the same host. I'm looking to see how to do this on a whole cluster since the vSwitch I want to add that it is the same vSwitch on all hosts.
Get-VMHost-name ESXI01 | Get-VirtualSwitch-name vSwitch1 | New-VirtualPortGroup-name cpg400 - VLanId 697
This command works, but rather to a host at the same time, what is the proper syntax to apply to the entire cluster?
Have you ever tried something like this?
Get-Cluster-name mycluster. Get-VMHost | Get-VirtualSwitch-name vSwitch1 | New-VirtualPortGroup-name cpg400 - VLanId 697
-
PowerCLI: configuration of the new portgroup / vlan on a standard switch
Hi all
recently, I discovered this useful tool, powercli, to manage a group of 12 Esxi5 hosts.
Well, goes directly to the issue, I do not understand how the Manager something using the cmdlet with the getEsxCli parameter.
I can susccesful login and send fundamentally get-member (I read on using powercli) but I do not understand what I have to use.
Here is an example, I have to run the two commands of esxcli:
esxcli standard vswitch network portgroup add - portgroup-name = VLAN_1 - vswitch-name = vSwitch0esxcli network vswitch portgroup standard set - portgroup-name = VLAN_1 - vlan id = 100Well, after I receive the cmdlet
PowerCLI C:\ > $myEsxCli | get-member
TypeName: VMware.VimAutomation.ViCore.Impl.V1.EsxCli.EsxCliImpl
Name MemberType definition
---- ---------- ----------
Cisco CodeProperty VMware.VimAutomation.Sdk.Util10Ps.ObjectCustomizati...
CodeProperty VMware.VimAutomation.Sdk.Util10Ps.ObjectCustomizati esxcli...
FCoE CodeProperty VMware.VimAutomation.Sdk.Util10Ps.ObjectCustomizati...
material CodeProperty VMware.VimAutomation.Sdk.Util10Ps.ObjectCustomizati...
iSCSI CodeProperty VMware.VimAutomation.Sdk.Util10Ps.ObjectCustomizati...
CodeProperty VMware.VimAutomation.Sdk.Util10Ps.ObjectCustomizati network...
CodeProperty VMware.VimAutomation.Sdk.Util10Ps.ObjectCustomizati software...
CodeProperty VMware.VimAutomation.Sdk.Util10Ps.ObjectCustomizati storage...
CodeProperty VMware.VimAutomation.Sdk.Util10Ps.ObjectCustomizati system...
VM CodeProperty VMware.VimAutomation.Sdk.Util10Ps.ObjectCustomizati...
Equals method Boolean Equals (System.Object, obj)
GetHashCode method int GetHashCode()
Type of the method GetType GetType()
String the ToString ToString() method
Property TypeManager VMware.VimAutomation.ViCore.Types.V1.EsxCli.Dynamic...
UID property System.String Uid {get ;}
Property VMHost VMware.VimAutomation.ViCore.Types.V1.Inventory.VMHo...
PowerCLI C:\ >
Now, I would like to order, but in any field I can do.
Thanks for any help.
You could do somehthing like that
Get-VirtualSwitch -Name vSwitch0 -VMHost MyEsx | New-VirtualPortGroup -Name VLAN_1 -VLanId 100
Take care to put the name of your server ESX (i).
-
Hi all
I'm trying to script on the creation of a cluster of virtual machines. I use the option Import-CSV and when I run the script I get continuous error
"New-VM: could not validate the argument on the 'Name' parameter." The argument is null or
empty. Provide an argument that is not null or empty, and then try the command ag
Ain.
To D:\Pathtoscriptandscriptname.ps1: tank 4:15
+ New-VM-name < < < < $_. Name - VMHost (Get-VMHost GetHosts) - Datastor
(Get-Datastore GetTheDatastores) e - DiskMB $_. OSDisk - MemoryMB $_. MEM - NumCpu $_. NUM
CPU - RunAsync - WhatIf
+ CategoryInfo: InvalidData: (:)) [new-VM], ParameterBindingValid)
ationException
+ FullyQualifiedErrorId: ParameterArgumentValidationError, VMware.VimAutom
ation.ViCore.Cmdlets.Commands.NewVM ".SE connect-VIServer VCServer
Import-CSV D:\Pathtoscriptandscriptname.ps1 | Foreach
(
New-VM-name $_. Name - VMHost ESXHost - MemoryMB $_. MEM - NumCpu $_. NumCPU - RunAsync - WhatIf)
Also when I run the script, I get the following...
ForEach-Object cmdlet in the command pipeline position 2
Provide values for the following parameters:
Process [0]:I ran the Import-CSV to the command window and displays in the command window fiine. I can't understand what is problems...
Thanks in advance.
If you do:
Get-Help New-VM-complete
under the SYNTAX, you will see that there are four sets of parameters of the New-VM cmdlet. DiskMB, MemoryMB, NumCPU and ID settings are only in the first parameter. The setting of the virtual machine is defined only in the second parameter. You must use parameters from a single parameter configured only. Otherwise, you get the error 'parameter set can be solved by using named parameters'.
You can easily see what parameter, define a function using Hal Rottenberg Get-parameter parameter belongs.
Get news-VM parameter. Sort-Object ParameterSet, name | Select-Object Name, Parameterset. Format-Table - AutoSize
gives the following result:
Name ParameterSet
---- ------------
Data store CloneVm
Description CloneVm
DiskStorageFormat CloneVm
DrsAutomationLevel CloneVm
HAIsolationResponse CloneVm
HARestartPriority CloneVm
Location CloneVm
Name CloneVm
OSCustomizationSpec CloneVm
ResourcePool CloneVm
RunAsync CloneVm
Server CloneVm
VApp CloneVm
VM CloneVm
VMHost CloneVm
AlternateGuestName Imagefieldconfig
CD Imagefieldconfig
Data store Imagefieldconfig
Description Imagefieldconfig
DiskMB Imagefieldconfig
DiskPath Imagefieldconfig
DiskStorageFormat Imagefieldconfig
DrsAutomationLevel Imagefieldconfig
Floppy Imagefieldconfig
ID Imagefieldconfig
HAIsolationResponse Imagefieldconfig
HARestartPriority Imagefieldconfig
Location Imagefieldconfig
MemoryMB Imagefieldconfig
Name Imagefieldconfig
NetworkName Imagefieldconfig
NumCpu Imagefieldconfig
ResourcePool Imagefieldconfig
RunAsync Imagefieldconfig
Server Imagefieldconfig
VApp Imagefieldconfig
Version Imagefieldconfig
VMHost Imagefieldconfig
VMSwapfilePolicy Imagefieldconfig
Description RegisterVm
DrsAutomationLevel RegisterVm
HAIsolationResponse RegisterVm
HARestartPriority RegisterVm
Location RegisterVm
Name RegisterVm
ResourcePool RegisterVm
RunAsync RegisterVm
Server RegisterVm
VApp RegisterVm
VMFilePath RegisterVm
VMHost RegisterVm
Model of data store
Model description
Model DiskStorageFormat
Model DrsAutomationLevel
Model HAIsolationResponse
Model HARestartPriority
Model rental
Model name
Model OSCustomizationSpec
ResourcePool model
Model RunAsync
Server model
Model model
VApp model
Model VMHostIf you want to clone a virtual machine, you can only use settings from the value of the CloneVm parameter.
Best regards, Robert
-
Automated configuration of ESX host
Hi all
We automate the process of Provisioning new host and configure the NETWORK/Switch card on the host. Once the host is set up it will be moved to regroup.
Script below is used for the same configuration. Is there a way we can make the generic script for all configuration by the values as variable and from external source or anyone have power-shell script carries it on the same business.
Any help is appreciated.
Trap
{
write-output $_
Output 1
}
$vmhostip = $args [0]
$ravmhostip = $args [1]
$octet = ($vmhostip.split(".")) [-1]
$vmhostuser = «*»
$vmhostpass = «*»
$vmkip1 = "X.X.X.$ ($octet)".
$vmksub1 = "255.255.255.0".
$vmkvlan1 = "17".
$vmkip2 = "X.X.X.10.14.7. $($octet)".
$vmksub2 = "255.255.255.0".
$vmkvlan2 = "16".
$vmkip3 = "X.X.X.$ ($octet)".
$vmksub3 = "255.255.255.0".
$vmkvlan3 = "100".
$vmkvsw0 = "vSwitch0.
$vmkvsw1 = "vSwitch1.
Add-PSSnapin VMware.VimAutomation.Core
to connect-viserver-Server $ravmhostip - the username-password $vmhostpass $vmhostuser
$vmhost = get-vmhost $ravmhostip
$vswitch0 = get-VirtualSwitch - vmhost $ravmhostip
Game-VirtualSwitch - VirtualSwitch $vswitch0 - Nic, vmnic0, vmnic1
$vswitch1 = New-VirtualSwitch - VMHost $vmhost - name vSwitch1 - Nic vmnic2, vmnic3
New-VirtualPortGroup - VirtualSwitch $vswitch1-name 'VM PRD 2' - VLanID 2
New-VirtualPortGroup - VirtualSwitch $vswitch1-name 'VM PRD 9' - VLanID 9
New-VirtualPortGroup - VirtualSwitch $vswitch1-name 'VM PRD 13' - VLanID 13
New-VirtualPortGroup - VirtualSwitch $vswitch1-name 'VM PRD 18' - VLanID 18
New-VirtualPortGroup - VirtualSwitch $vswitch1-name 'VM PRD 21' - VLanID 21
New-VirtualPortGroup - VirtualSwitch $vswitch1-name 'VM PRD 40' - VLanID 40
New-VirtualPortGroup - VirtualSwitch $vswitch1-name "VM PRD 41" - VLanID 41
New-VirtualPortGroup - VirtualSwitch $vswitch1-name "VM PRD 250" - VLanID 250
New-VMHostNetworkAdapter - VMHost $vmhost - PortGroup 'Management' - VirtualSwitch $vmkvsw0 IP - $vmkip1 - subnet $vmksub1 mask
New-VMHostNetworkAdapter - VMHost $vmhost - PortGroup "VMkernel" VirtualSwitch - $vmkvsw0 - IP $vmkip2 SubnetMask - $vmksub2 - VMotionEnabled: $true
New-VMHostNetworkAdapter - VMHost $vmhost - PortGroup 'VMkernel2' VirtualSwitch - $vmkvsw1 - IP $vmkip3 SubnetMask - $vmksub3 - VMotionEnabled: $true
$vmkpg1 = get-VirtualPortgroup-name of the "management".
$vmkpg2 = get-VirtualPortgroup-name "VMkernel".
$vmkpg3 = get-VirtualPortgroup-name 'VMkernel2 '.
Game-VirtualPortGroup - VirtualPortGroup $vmkpg1 - VlanId $vmkvlan1
Game-VirtualPortGroup - VirtualPortGroup $vmkpg2 - VlanId $vmkvlan2
Game-VirtualPortGroup - VirtualPortGroup $vmkpg3 - VlanId $vmkvlan3
Start-VMHostService - HostService ($vmhost |) Get-VMHostService | Where {$_.} Key - eq 'TSM - SSH'})
Disconnect-VIServer-Force - confirm: $false
Thanks and greetings
Riyas Hussain has
You can store variables in a CSV, something like this
VMHostIP, RAVMHostIP, VMHostUser, VMHostPass, Subnet1, Netmask1, VlanID2, Subnet2, Netmask2, VlanID3, Subnet3, Netmask3, VlanID3, vSwitch0, vSwitch1
192.168.1.1,192.168.1.2,root,password,192.168.1,255.255.255.0,17,192.168.2,255.255.255.0,16,192.168.3,255.255.255.0,100,vSwitch0,vSwitch1
192.168.10.1,192.168.10.2,root,password,192.168.10,255.255.255.0,17,192.168.20,255.255.255.0,16,192.168.30,255.255.255.0,100,vSwitch0,vSwitch1
In your original script you must only change the assignment initially.
The script will be executed for each row of the CSV file.
Inside of the ForEach loop (alias %), the variable $_ holds the current line.
Contact the specific values using the property name that matches the name of the column in the CSV file
Trap
{
write-output $_
Output 1
}
Add-PSSnapin VMware.VimAutomation.Core
Import-Csv-path esxihost.csv - UseCulture | %{
$vmhostip = $_. VMHostIP
$ravmhostip = $_. RAVMHostIP
$octet = ($vmhostip.split(".")) [-1]
$vmhostuser = $_. VMHostUser
$vmhostpass = $_. VMHostPass
$vmkip1 = "$($_.)". "Subnet1). $($octet)".
$vmksub1 = $_.Netmask1
$vmkvlan1 = $_. VlanID1
$vmkip2 = "$($_.)". "Subnet2). $($octet)".
$vmksub2 = $_.Netmask2
$vmkvlan2 = $_. VlanID2
$vmkip3 = "$($_.)". "Subnet2). $($octet)".
$vmksub3 = $_.Netmask2
$vmkvlan3 = $_. VlanID2
$vmkvsw0 = $_.vSwitch0
$vmkvsw1 = $_.vSwitch1
to connect-viserver-Server $ravmhostip - the username-password $vmhostpass $vmhostuser
$vmhost = get-vmhost $ravmhostip
# The rest of the original script you
Disconnect-VIServer-Force - confirm: $false
}
-
Need a script to create standard vSwitch with virtual and several computer port group VLAN
I want to create standard vSwitch for all hosts in the cluster for virtual machine port group and add one or more groups of ports VLAN for the same standard vswitch.
Kind regards
Shan
Try something like this
$clusterName = "mycluster.
$nics = "vmnic0", "vmnic1.
$vlans = 123456789
foreach ($esx in (Get-Cluster-name $clusterName |)) Get - VMHost)) {}
$sw = New - VirtualSwitch - name swX - VMHost $esx - Nic $nics - confirm: $false
$vlans | %{
New-VirtualPortGroup-name "PG $($_)" - VLanId $_ - VirtualSwitch $sw - confirm: $false
}
}
-
Slow speeding code with async?
Hello
I have two groups, one of 25 guests and one of 18 guests. For migration to vSphere 5.1 I need from these hosts of dvSwitch in standard vSwitch. I wrote a script that, for each guest, adds 150 virtual exchanges on the vSwitch, but is deadslow. He is now running two hours and only at half way.
Issues related to the:
-Can I speed up the process by writing better code?
-Would adding the async parameter - speed up things and this could potentially overload my vCenter?
Code:
$cluster = "Cluster01".
$ESXHost = get-Cluster | Get-VMHost
$report = import-Csv "c:\csv-files\list-01-standard-ports-voor-nieuwe-omgeving.csv".
foreach ($esxihost to $ESXHost)
{
Write-Host $esxihost. Name
foreach ($row in $Report)
{
Get-VirtualSwitch - VMHost $ESXiHost - name 'vSwitch0 | New-VirtualPortGroup-name $row. PortGroup - VLanId $row. VLAN
}
}
As much as I know there is no RunAsync switch on the New-VirtualPortgroup cmdlet.
The best opportunity to save time, is to do once the VirtualSwitch Get.
Something like that
...
foreach ($esxihost to $ESXHost)
{
$sw = get-VirtualSwitch - VMHost $ESXiHost - name 'vSwitch0.
Write-Output $esxihost. Name
foreach ($row in $Report)
{
VirtualSwitch news-VirtualPortGroup $sw - name $row. PortGroup - VLanId $row. VLAN
}
}
...
BTW, your script would about kill 43 puppies
-
Automation of the network change
I am writing a script to help with a few major network changes that I am in charge of. The script I'm writing should do the following things...
Connect through Mgmt Network2
Remove vmnic2 vSwitch0
Create vSwitch3
Add several exchanges to vSwitch3
To the bottom of the NIC on the network of Mgmt
Restart the network Mgmt
Sleep 60 seconds
Connect to Network2 Mgmt
Fix the vlanid on network Mgmt
Change the ip address of the network Mgmt vmk
So here's what I have so far... I don't think I'm on the right track, but I feel that I can not go about it the right way. I have also the problem create exchanges on vSwitch3. Any help is greatly appreciated!
SE connect-VIServer-Server xx.xx.xx.xx
$VMhost = get-VMHost-name xx.xx.xx.xx
$vmnic0 = "vmnic0".
$vmnic1 = "vmnic1.
$vmnic2 = "vmnic2.
$vmnic3 = "vmnic3".
$VMhostMgmt2 = get-VMHost-name xx.xx.xx.xx
$vSwitch3 = "vSwitch3".
$VMhost | Get-VirtualSwitch | Where-Object {$_.} Name - eq "vSwitch0" - and $_. NIC - eq "vmnic2"} | {Foreach}
If ($VMHost |) Get-VirtualSwitch-Name $_. {Name)
Get-VirtualSwitch-Name $_. Name | Game-VirtualSwitch - Nic $vmnic0, $vmnic1 - confirm: $false
}
}
$VMhost | Get-VirtualSwitch | Where-Object {$_.} Name - not "vSwitch3"} | {Foreach}
If ($VMHost |) Get-VirtualSwitch-Name $_. {Name)
New-VirtualSwitch - VMHost $VMhost - name vSwitch3 - NumPorts 128 - Mtu 1500 - Nic $vmnic2, $vmnic3 - confirm: $false
New-VirtualPortGroup-name xx.xx.xx.xx - VirtualSwitch $vSwitch3 - VLanId 10 - confirm: $false
New-VirtualPortGroup-name xx.xx.xx.xx - VirtualSwitch $vSwitch3 - VLanId 12 - confirm: $false
New-VirtualPortGroup-name xx.xx.xx.xx - VirtualSwitch $vSwitch3 VLanId - 40 - confirm: $false
}
}
Get-EsxCli - vmhost $VMhost
$esxcli.network.nic.down ($vmnic0)
$esxcli.network.nic.down ($vmnic1)
Get-VMHostService - VMHost $VMhost | where {$_.} Key - eq "vpxa"} | Restart-VMHostService-confirm: $false
Start-sleep - s 60
to connect-VIServer-Server xx.xx.xx.xx
Start-sleep - s 60
$VMhostMgmt2 | Get-VirtualSwitch | Where-Object {$_.} Name - eq "vSwitch0"} | {foreach}
If (Get-VirtualSwitch-name $_.) {Name)
Game-VirtualPortGroup-name 'Network management' - VLanId 19 - confirm: $false
}
}
Get-VMHostNetworkAdapter | where {$_.} Name - eq "vmk0"} | {foreach}
If (Get-VMHostNetworkAdapter - VMHost $VMhostMgmt2-Name $_.) {Name)
Get-VMHost $VMhostMgmt2 | Get-VMHostNetworkAdapter-Name $_. Name | Game-VMHostNetworkAdapter - IP xx.xx.xx.xx - netmask 255.255.255.0 - confirm: $false
}
}
Get-VMHostService - VMHost $VMhostMgmt2 | where {$_.} Key - eq "vpxa"} | Restart-VMHostService-confirm: $false
Get-EsxCli - vmhost $VMhost
$esxcli.network.nic.up ($vmnic0)
$esxcli.network.nic.up ($vmnic1)
Get-VMHostService - VMHost $VMhost | where {$_.} Key - eq "vpxa"} | Restart-VMHostService-confirm: $false
It seems that you may have more than one open connection.
Display the contents of
Global $: DefaultVIServers
If there is more than one return value, and if you are working in "multiple" (see Set-PowerCLiConfiguration), which is probably the cause of the error
-
Port group deletion doesn't work not as expected
I have a project where I have to rename all exchanges (create new VM to move, delete old) for our new naming convention. I have a script which covers this. However, he has a problem with exchanges that follow the new standard with the exception of its case. The script removes the exchanges on the hosts where the portgroup is empty! Is there a way to get around this? I even tried to rename the old portgroup but same problem here because she sees 2 exchanges regardless of case and fails to rename.
Example: The z is lower among the ancient and new.
OldPG: VM_169.036.072.000_24_0134_BaSz_Data
NewPG: VM_169.036.072.000_24_0134_BaSZ_Data
$targetClu = "Cluster001".
$fileLocation = "h:\pgChangeInput.csv".
Write-Host ""
$getPG = import-Csv $fileLocation
Write-Host "step 1: create the new portgroup (s) on $targetClu"-ForegroundColor Magenta
{foreach ($line in $getPG)
$Clu = $line. Cluster
$OldPG = $line. OldPG
$NewPG = $line. NewPG
$Vlan = $line. VLanID
$vSwitch = $line.vSwitch
If ($Clu - eq $targetClu) {}
Write-Host "processing = > $Clu, $OldPG, $NewPG, $Vlan, $vSwitch"-ForegroundColor green ""
Write-Host "Creating portgroup $NewPG" - ForegroundColor yellow
foreach ($esx in get-VMhost-location $targetClu | sort name) {}
Write-Host "$esx" - ForegroundColor Cyan
Get-VMHost $esx | Get-VirtualSwitch-name $vSwitch | New-VirtualPortGroup-name $NewPG - vlanid $Vlan | Out-Null
}
Write-Host "step 2: change the portgroup VM"-ForegroundColor Magenta
Get-Cluster $targetClu | Get - VM | Get-NetworkAdapter | Where {$_.NetworkName - eq $OldPG} | Together-NetworkAdapter - NetworkName $NewPG - connected: $true - confirm: $false | Out-Null
Write-Host "step 3: remove the old portgroup"-ForegroundColor Magenta
foreach ($esx in get-VMhost-location $targetClu | sort name) {}
Write-Host "$esx" - ForegroundColor Cyan
Get-VMHost $esx | Get-VirtualSwitch-name $vSwitch | Get-VirtualPortGroup-name $OldPG | Delete-VirtualPortGroup-confirm: $false | Out-Null
}
}
}
Any help is greatly appreciated.
You're right, the vSphere client seems to check the case of an existing portgroup, while the New VirtualPortgroup don't.
The AddPortgroup method also seems to be case sensitive.
I couldn't find anything on this in the Documentation Centre of vSphere, but in the book of vSphere Mastering of Scott Lowe, he also says that the names are case-sensitive.
But it seems that PowerCLI is correctly using the name, which is case-sensitive.
Back to the problem of you, you can use an operator that is case-sensitive in a Where clause to filter the desired portgroup.
Something like that
Get-VMHost $esx | Get-VirtualSwitch -Name $vSwitch | Get-VirtualPortGroup -name $OldPG | where {$_.Name -ceq $OldPG} | Remove-VirtualPortGroup -Confirm:$false | Out-Null
Note that it uses the ceq- instead of the operator -eq .
-
Script to add several vPortGroups on a vSvitch on the mulpitple hosts
Hello
I have a small script to add several vPortGroups on a vSvitch on mulpitple of the hosts.
The Script looks like:
$ObjHosts = get-VMHost host12 * | Sort-Object-property name
Import-Csv "C:\vPortGroups.csv" - UseCulture | %{
{foreach ($objHost to $ObjHosts)
$strVSwitch = get-Virtualswitch - VMHost (Get-VMHost $objHost) | WHERE-object {$_.} {Name - match "$_.strVSwitch"}
Write-Host "Adding virtual port group" $_.strNewVPG "with VLAN Tag' $_.strNewVlanTag 'to' $_.strVSwitch 'on' $objHost
New-VirtualPortGroup-name "$_.strNewVPG"-VirtualSwitch "$_.strVSwitch"-VLanId ' $_.strNewVlanTag '.
}
}The input file looks like:
strNewVPG; strVSwitch; strNewVlanTag
"4.244.151.0 - 24"; «vSwitch1 «;» 120If we use only the line "Write-Host"Adding Virtual Port Group"$_.strNewVPG" with VLAN Tag ' $_.strNewVlanTag 'to' $_.strVSwitch 'on' $objHost' "
the output is "add the port group virtual 4.244.151.0 - 24 with VLAN Tag 120 to vSwitch1 on d100spwesxc3101.d100.intern.
Thus, the input file is correct and legible for the script.
If I use the full script, the output message is:
New-VirtualPortGroup: cannot bind parameter 'VLanId '. Cannot convert value ' @{strNewVPG = 4.244.151.0 - 24;} '. strVSwitch = vSwitch1; "strNewVlanTag = 120} .strNewVlanTag" to type "System.Int32". Error: "I."
nput string was not in a correct format. »
C:\Dokumente und Einstellungen\user1\Eigene Dateien\VMWare PowerShell Scripte\add_VirtualPortGroup_by_inputfile.ps1:7 tank: 83
+ New-VirtualPortGroup-name "$_.strNewVPG"-VirtualSwitch ' $_.strVSwitch ' VLanId - < < < < "$_.strNewVlanTag."
+ CategoryInfo: InvalidArgument: (:)) [new VirtualPortGroup], ParameterBindingException)
+ FullyQualifiedErrorId: CannotConvertArgumentNoMessage, VMware.VimAutomation.ViCore.Cmdlets.Commands.Host.NewVirtualPortGroupWhat's not here?
I made a few changes to your script. You can try the new version?
$ObjHosts = Get-VMHost host12* |Sort-Object -Property Name Import-Csv "C:\vPortGroups.csv" -UseCulture | %{ $vPortGroup = $_ foreach($objHost in $ObjHosts){ $strVSwitch = Get-Virtualswitch -VMHost (Get-VMHost $objHost) | where-object { $_.Name -match $vPortGroup.strVSwitch } Write-Host "Adding Virtual Port Group" $vPortGroup.strNewVPG "with VLAN Tag" $vPortGroup.strNewVlanTag "to" $vPortGroup.strVSwitch "on" $objHost.Name New-VirtualPortGroup -Name $vPortGroup.strNewVPG -VirtualSwitch $strVSwitch -VLanId $vPortGroup.strNewVlanTag } }
One of the main problems was that $_ represents only the object from the pipeline last. So I added the line $vPortGroup = $_ to keep the line from the file entry in the variable $vPortGroup.
Best regards, Robert
-
Check whether there is a portgroup
I would like to add a few exchanges of a .csv. To do this, I use the following script:
$pgs = import-csv portgroups.csv
$swi = get-virtualswitch - vmhost host.domain.local - name vSwitch1
{foreach ($pg in $pgs)
$swi | New-VirtualPortGroup-$pg.name - VLanId $pg name. VLanId
}
How can I change the loop to chek if a portgroup exists already, something like: If $pg does not exist, then new-virtualportgroup.
Daniel
Of course, there are several ways to do this in PS and PowerCLI.
This is one of them.
$pgs = import-csv portgroups.csv $swi = get-virtualswitch -vmhost host.domain.local -name vSwitch1 $pg_present = $swi | Get-VirtualPortGroup | % {$_.Name} foreach ( $pg in $pgs) { if(-not ($pg_present -contains $pg.name)){ $swi | New-VirtualPortGroup -name $pg.name -VLanId $pg.VLanId } }
Maybe you are looking for
-
Satellite Pro S300 - 120 PSSB0E: No 32-bit software Win 7 of fingerprints
Hello. I have a Toshiba Satellite Pro S300-120 part number: PSSB0E-05D00UEN I just loaded Windows 7 32-bit, but as far as I see it, there is no software available for the fingerprint reader. Anyone know how I can get this working, or when it will be
-
Windows 8 will work as a touch screen on my computer to windows 7 touch screen?
I tried to find an answer for "windows 8 will work as a touch screen on my windows 7 HP touchscreen computer.
-
Toshiba 640GB HDD Red Rocket - icon is missing
Hi all I bought a Toshiba 640 GB Portable external hard drive (Red Rocket) - HDDR640E04XRhttp://www.toshibadirect.com/TD/B2C/Adet.to?poid=451941 and even if specifically, I chose the option for NTFS, the installation program there in FAT32. Well I de
-
Hello! I have HP Split 13-m101er x 2 PC and I want to improve memory SSD 128 GB and 8 GB of RAM. Are there ways to do it with this PC. Thank you!
-
Hello I have a duty for a user to use AutoCAD... Is there advice / recommended measures to execute with vWorkspace Session host other applications? Note: These servers don't vGPU map there only the graphics card by default. Thank you