Hello
I have something like this , i want to change portgroup for each VM, note that in screenshot every single row has multiple network adapter and portgroups as well, can i have script?
finally i corrected my self thanks Lucd
With what is 'network1' corresponding?
The name of the vMIC?
That is normally something like 'Network adapter 1'.
Can't you have this as this?
VM,HostName,NetworkAdapter,Portgroup
ABC,ABCHOST,network1,i-VLAN1
ABC,ABCHOST,network2,i-VLAN2
XYZ,XYZHost,network1,i-VLAN2
XYZ,XYZHost,network2,i-VLAN3
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
hi Lucd
Thanks, Yes that name of VMNIC,
regarding the input table, i cannot have like that reason I am creating script to search datastore, register VM with different name and in each VM change network adapter, so this is my requirement, I have import-csv activity in one line so for each server i am doing task, but when VM have multple adapter with multiple portgroup i am stuck. so either I need to proceed above or do you have script to fulfill my requirement ?
Not sure how that can be the name of the vNIC.
Is that the name that is returned when you do
Get-VM -Name XYZ | Get-NetworkAdapter | Select Name
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
it returns to me , sorry for typo
"Parent","Name","NetworkName"
"xyz","Network adapter 1","i-VLAN65"
"xyz","Network adapter 2","i-VLAN65"
Ok, that seems more realistic.
Now for your file, is that a CSV file? Or a TXT file?
If it is a CSV, what is returned when you do
Import-Csv -Path .\yourfile.csv -UseCulture
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Hi Lucd
Sorry for delay response, Here is my script, i left gap for for loop for each NIC, I am blank in changing portgroup, can you suggest ?
for below changing network portgroup, if I have servers.csv then i have to include multiple adapter in one row, or do i need create seperate csv for changing network portgroup
$srv = Import-csv -Path servers.csv -Header VMname,Clustername,Datastorename,ESX -UseCulture
foreach ($entry in $srv)
{
$Server = $entry.VMname
$Cluster = $entry.Clustername
$Datastore = $entry.Datstorename
$NetworkAdapter = $entry.NIC
$Network = $entry.portgroup
$ESXhost = $entry.ESX
# Collect .vmx paths of registered VMs on the datastore
$registered = @{}
Get-VM -Datastore $Datastore | %{$_.Extensiondata.LayoutEx.File | where {$_.Name -like "*.vmx"} | %{$registered.Add($_.Name,$true)}}
# Set up Search for .VMX Files in Datastore
New-PSDrive -Name TgtDS -Location $Datastore -PSProvider VimDatastore -Root '\' | Out-Null
$unregistered = @(Get-ChildItem -Path TgtDS: -Recurse | `
where {$_.FolderPath -notmatch ".snapshot" -and !$registered.ContainsKey($_.Name)})
Remove-PSDrive -Name TgtDS
#Register all .vmx Files as VMs on the datastore
foreach($VMXFile in $unregistered) {
New-VM -Name $Server_dr -VMFilePath $VMXFIle.DatastoreFullPath -VMHost $ESXHost
$NIC = Get-NetworkAdapter -VM $Server_dr
foreach($network in $NIC)
{
# can you correct what I need to put here, if I have same csv servers.csv then i have to include multiple adapter in one row, or do i need create seperate csv for changing network portgroup
}
}
}
Sorry, but you keep utterly confusing me.
That Import-Csv with the Header parameter, doesn't correspond at all with the sample CSV content you gave earlier.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Ok sorry....please ignore all.....i have one requirment ...can u provide me script
My requirement is search given datastore...
Register vm as vm_dr
Change the portgroup for the registered vm
Can u suggest a good script
Regards
Dhananjaya
Try like this
$pgName = 'MyPG'
$registered = @{ }
$ds = Get-Datastore -Name $dsName
Get-VM -Datastore $ds |
ForEach-Object -Process {
$_.Extensiondata.LayoutEx.File | where { $_.Name -like "*.vmx" } |
ForEach-Object -Process {
$registered.Add($_.Name, $true)
}
}
New-PSDrive -Name TgtDS -Location $ds -PSProvider VimDatastore -Root '\' | Out-Null
$unregistered = @(Get-ChildItem -Path TgtDS: -Recurse | `
where { $_.FolderPath -notmatch ".snapshot" -and $_.Name -like "*.vmx" -and !$registered.ContainsKey($_.Name) })
Remove-PSDrive -Name TgtDS
foreach ($VMXFile in $unregistered)
{
$vm = New-VM -VMFilePath $VMXFile.DatastoreFullPath -VMHost $ESXHost -Location $VMFolder -RunAsync
Get-NetworkAdapter -VM $vm | Set-NetworkAdapter -NetworkName $pgName
}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
but my problem is, port group is not same for all 400 servers.
And where would the script find out which portgroup to use for which VM?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
this one
Get-VM | Get-NetworkAdapter | select Parent,Name,Networkname | export-csv network.csv -NoTypeInformation
Sorry, but that makes no sense at all.
You are registering VMs, you want to change the portgroup they are connected to, and that info comes from querying the network cards and the portgroup they are currently connected to???
I give up!
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
sorry thats not the command , i forget to tell you that we are changing port group at DR environment, that means if production port group is PG-ABC then DR port group is PG-ABC_DR so we will create csv file in such way that it should look like below
I have created script now but i get only one error for alternative guest name can you help now
$srv = Import-csv -Path servers.csv -Header VMname,Clustername,Datastore,ESX -UseCulture
foreach ($entry in $srv)
{
$Server = $entry.VMname
$Cluster = $entry.Clustername
$DSname = "SDN_INTRA_VOL6"
$NetworkAdapter = $entry.NIC
#$Network = $entry.portgroup
$ESXhost = $entry.ESX
# Collect .vmx paths of registered VMs on the datastore
$registered = @{}
$Datastore = Get-Datastore -Name $DSname
Get-VM -Datastore $Datastore | %{$_.Extensiondata.LayoutEx.File | where {$_.Name -like "*.vmx"} | %{$registered.Add($_.Name,$true)}}
# Set up Search for .VMX Files in Datastore
New-PSDrive -Name TgtDS -Location $Datastore -PSProvider VimDatastore -Root '\' | Out-Null
$unregistered = @(Get-ChildItem -Path TgtDS: -Recurse | `
where {$_.FolderPath -notmatch ".snapshot" -and $_.Name -like "XYZ.vmx" -and !$registered.ContainsKey($_.Name)})
Remove-PSDrive -Name TgtDS
#Register all .vmx Files as VMs on the datastore
foreach($VMXFile in $unregistered) {
$vm = New-VM -AlternateGuestName "$ServerDR" -VMFilePath $VMXFIle.DatastoreFullPath -VMHost $ESXHost
#$NIC = Get-NetworkAdapter -VM $vm
cmd.exe /c findstr /i $vm network.csv >new.csv
$nw = import-csv -path new.csv -Header VMname,Name,portgroup -UseCulture
foreach($network in $nw)
{
Get-VM -VM $network.VMname |Get-NetworkAdapter |Where {$_.Name -eq $network.Name } | Set-NetworkAdapter -NetworkName $network.portgroup
}
}
}
errorr is
New-VM : Parameter set cannot be resolved using the specified named parameters.
DR-Register.ps1:21 char:7
+ $vm = New-VM -AlternateGuestName "$Server" -VMFilePath $VMXFIle.Datas ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [New-VM], ParameterBindingException
+ FullyQualifiedErrorId : AmbiguousParameterSet,VMware.VimAutomation.ViCore.Cmdlets.Commands.NewVM
finally i corrected my self thanks Lucd
HI
Can you please share me the script and template for that input file as well as i need in same scenario where i need to do this kind of work in DR environment