Good day!
One of my Cx is trying to set a limit to vSphere Replication traffic in 6.0 using the below script
Set-ExecutionPolicy RemoteSigned
Connect-VIServer vcsahq1.kembacu.org -User administrator@vsphere.local -Password sdfsdf
<#
vSphere Replication Limits
Scriptable way to toggle your VR bandwidth cap for day, night, weekend, or whatever
Args usage = PSfile "vcsahq1.kembacu.org" "VDS name to configure" "10"
#>
# Variables
$vcenter = $args[0]
$dvsName = $args[1]
$rpName = "vSphere Replication"
# Set to -1 to go back to unlimited
$newLimit = $args[2]
# Connect to vCenter
If (-not $global:DefaultVIServer) {Connect-VIServer -Server $vcenter}
# Check args
if (-not $args[2])
{
throw "Incorrect format. Provide the vCenter name, VDS name, and limit in Mbps. Example: `"script `"vCenterFQDN`" `"VDS1`" 500`" would set VR bandwidth on VDS1 to 500 Mbps"
exit
}
# Get the VDS details
$dvs = Get-VDSwitch -Name $dvsName
# Set the VR network pool to the value provided in args
# The section below was written by Luc Dekens (@LucD22), all credit to him
$rp = $dvs.ExtensionData.NetworkResourcePool | Where {$_.Name -match $rpName}
if($rp){
$spec = New-Object VMware.Vim.DVSNetworkResourcePoolConfigSpec
$spec.AllocationInfo = $rp.AllocationInfo
$spec.AllocationInfo.Limit = [long]$newLimit
$spec.ConfigVersion = $rp.ConfigVersion
$spec.Key = $rp.Key
$dvs.ExtensionData.UpdateNetworkResourcePool(@($spec))
}
# Verify
$rp = $dvs.ExtensionData.NetworkResourcePool | Where {$_.Name -match $rpName}
Write-Host "A limit of" $rp.AllocationInfo.Limit "Mbps has been set on $dvs"
The script executes without any error. However, when I login to web client. Networking > DVS > Manage > Resource Allocation > System Traffic > The value for limit for vSphere Rep VR Traffic is not updated.
Little debug shows that rpname does not match $rp causing the script to jump the if condition. But this is all I know on scripting.
Please have a look and let me know if any changes is required.
PS: I am not familiar with scripting, a newbie I should say, so please bear if my questions sound weird.
Thank you.
The way of changing the limit has indeed changed rather fundamentally.
This script should work for 5.5 and 6.0.
$vdsName = 'dvSwTest'
$limitMbps = 10
$rpName = 'vSphere Replication (VR) Traffic'
$rpKey = 'hbr'
$vds = Get-VDSwitch -Name $vdsName
if($global:DefaultVIServer.Version -eq "6.0"){
$rp = $vds.ExtensionData.Config.InfrastructureTrafficResourceConfig | where{$_.Key -eq $rpKey}
$spec = New-Object -TypeName VMware.Vim.DvsConfigSpec
$spec.configVersion = $vds.ExtensionData.Config.ConfigVersion
$rp.AllocationInfo.Limit = [long]$limitMbps
$spec.InfrastructureTrafficResourceConfig += $rp
$vds.ExtensionData.ReconfigureDvs($spec)
}
else{
$rp = $vds.ExtensionData.NetworkResourcePool | where{$_.Name -eq $rpName}
$spec = New-Object -TypeName VMware.Vim.DVSNetworkResourcePoolConfigSpec
$spec.Name = $rp.Name
$spec.Key = $rp.Key
$spec.ConfigVersion = $rp.ConfigVersion
$spec.Description = $rp.Description
$alloc = New-Object VMware.Vim.DVSNetworkResourcePoolAllocationInfo
$alloc.Limit = $limitMbps
$alloc.Shares = $rp.AllocationInfo.Shares
$alloc.PriorityTag = $rp.PriorityTag
$spec.AllocationInfo = $alloc
$vds.ExtensionData.UpdateNetworkResourcePool(@($spec))
}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Try changing that line into
$rp = Get-View -Id $dvs.ExtensionData.NetworkResourcePool | Where {$_.Name -match $rpName}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Hi Luc,
So I replace the line above the if($rp) by the provided line.
When executing I get the message:
Get-View : Cannot validate argument on parameter 'Id'. The argument is null or empty. Provide an argument that is not null or empty, and then try the command again.
I have attached the screenshot.
Also, the statement
$dvs = Get-VDSwitch -Name $dvsName
$dvsName has an output of Switch1 (Name of DVS)
Then, $dvs has a Value of Switch1 Switch1. Shouldn't $dvs have just Switch1 ?
The $rp still comes up blank on value
Suhas
Does this return more than just the name of the vdSwitch ?
Get-VDSwitch -Name $dvsName | Select *
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Get-VDSwitch -Name $dvsName (Returns two Switch1; Switch1 Switch1)
Get-VDSwitch -Name $dvsName | Select * ($dvs returns empty value)
I have attached the screenshot for both.
Suhas
The fact that it returns 2 objects is probably causing the issues afterwards.
Do you have 2 vdSwitches with a name that starts with "Switch1" ?
Don't assign that line I gave to a variable, just let it display on screen.
Or do you have 2 connections open (display the value of $global:defaultviservers).
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Yes, you were right. There were multiple connections open.
Thanks. So now the line
$dvs = Get-VDSwitch -Name $dvsName
Gives the correct Switch output, Switch1
However, the line:
$rp = Get-View -Id $dvs.ExtensionData.NetworkResourcePool | Where {$_.Name -match $rpName}
Tells:
Get-View : Cannot validate argument on parameter 'Id'. The argument is null, empty, or an element of the argument collection contains a
null value. Supply a collection that does not contain any null values and then try the command again.
Suhas
Great!
On the next issue, are you using a Network Resource Pool for this vdSwitch ?
Can you check with the Web Client ?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Excellent.
So, I have System Traffic tab and Network resource pool.
The vSphere Replication (VR) Traffic is where I am trying to limit.
The network resource pool for vDS is empty.
I am guessing the parameter has a flaw?
Suhas
You can set a limit on VR traffic without using a Network Resource Pool, but that is not done via the code you are using.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Oh, so is there a way we can set a limit on the VR traffic via a script?
Is there a script that you can direct me to.
My Cx is looking a way to limit this VR traffic (Under system traffic), and he provided me the script that he was using.
Any help would be super-great on this
Suhas
I had a 2nd look at the code, and I think it's just a matter of using the correct System Network Resource Pool name.
These are built in.
Try setting the name as
$rpName = 'vSphere Replication (VR) Traffic'
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Yep. It's already set to that. I changed the line once I saw the Traffic type name.
Any idea what's happening on the Get-View -Id line?
$rp = Get-View -Id $dvs.ExtensionData.NetworkResourcePool | Where {$_.Name -match $rpName}
This is the point where I get the error.
Suhas
Also with the corrected name ?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Yep, even when the
$rpName = "vSphere Replication (VR) Traffic"
Suhas
The Get-View -Id is giving out an error.
Get-View : Cannot validate argument on parameter 'Id'. The argument is null, empty, or an element of the argument collection contains a
null value. Supply a collection that does not contain any null values and then try the command again.
At C:\Users\Administrator\AppData\Local\Temp\1\b8b5639e-adf6-4d6f-b629-aab0448b557f.ps1:39 char:20
+ $rp = Get-View -Id $dvs.ExtensionData.NetworkResourcePool | Where {$_.Name -matc ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidData: (:) [Get-View], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,VMware.VimAutomation.ViCore.Cmdlets.Commands.DotNetInterop.GetVIView
Suhas
Can you try this code, it seems to work for me.
$vdsName = 'dvSwTest'
$limitMbps = 10
$rpName = 'vSphere Replication (VR) Traffic'
$vds = Get-VDSwitch -Name $vdsName
$rp = $vds.ExtensionData.NetworkResourcePool | where{$_.Name -eq $rpName}
$spec = New-Object -TypeName VMware.Vim.DVSNetworkResourcePoolConfigSpec
$spec.Name = $rp.Name
$spec.Key = $rp.Key
$spec.ConfigVersion = $rp.ConfigVersion
$spec.Description = $rp.Description
$alloc = New-Object VMware.Vim.DVSNetworkResourcePoolAllocationInfo
$alloc.Limit = $limitMbps
$alloc.Shares = $rp.AllocationInfo.Shares
$alloc.PriorityTag = $rp.PriorityTag
$spec.AllocationInfo = $alloc
$vds.ExtensionData.UpdateNetworkResourcePool(@($spec))
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
No luck :smileycry:
$rp = $vds.ExtensionData.NetworkResourcePool | where{$_.Name -eq $rpName}
Is coming up empty again.
$alloc = New-Object VMware.Vim.DVSNetworkResourcePoolAllocationInfo
Is returning:
Exception calling "UpdateNetworkResourcePool" with "1" argument(s): "The object or item referred to could not be found."
At C:\Users\Administrator\AppData\Local\Temp\1\b8b5639e-adf6-4d6f-b629-aab0448b557f.ps1:35 char:48
+ $vds.ExtensionData.UpdateNetworkResourcePool(@($spec))
+ ~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : VimException
I am pretty sure, I am doing something wrong here.
Can we screen share?
Suhas
I ran my script in 5.5.
There must be a difference with 6.0, let me check that.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
The way of changing the limit has indeed changed rather fundamentally.
This script should work for 5.5 and 6.0.
$vdsName = 'dvSwTest'
$limitMbps = 10
$rpName = 'vSphere Replication (VR) Traffic'
$rpKey = 'hbr'
$vds = Get-VDSwitch -Name $vdsName
if($global:DefaultVIServer.Version -eq "6.0"){
$rp = $vds.ExtensionData.Config.InfrastructureTrafficResourceConfig | where{$_.Key -eq $rpKey}
$spec = New-Object -TypeName VMware.Vim.DvsConfigSpec
$spec.configVersion = $vds.ExtensionData.Config.ConfigVersion
$rp.AllocationInfo.Limit = [long]$limitMbps
$spec.InfrastructureTrafficResourceConfig += $rp
$vds.ExtensionData.ReconfigureDvs($spec)
}
else{
$rp = $vds.ExtensionData.NetworkResourcePool | where{$_.Name -eq $rpName}
$spec = New-Object -TypeName VMware.Vim.DVSNetworkResourcePoolConfigSpec
$spec.Name = $rp.Name
$spec.Key = $rp.Key
$spec.ConfigVersion = $rp.ConfigVersion
$spec.Description = $rp.Description
$alloc = New-Object VMware.Vim.DVSNetworkResourcePoolAllocationInfo
$alloc.Limit = $limitMbps
$alloc.Shares = $rp.AllocationInfo.Shares
$alloc.PriorityTag = $rp.PriorityTag
$spec.AllocationInfo = $alloc
$vds.ExtensionData.UpdateNetworkResourcePool(@($spec))
}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference