I have been asked to look into providing one of our software teams with details on some PowerCLI commands to do some specific things.
One of these was to ensure that the CpuAffinityList on VMs was empty.
In the WebClient I can clear the "Scheduling Affinity" field, as per the information from the :smileyinfo: help dialogue.
If I then do :
Get-VM -Name $VMName | Get-VMResourceConfiguration | select CpuAffinityList
I get nothing in return as expected, I just get a column header:
CpuAffinityList
-------------------
If I do:
Get-VM -Name $VMName | Get-VMResourceConfiguration | Set-VMResourceConfiguration -CpuAffinityList ''
The Webclient will now show the "Scheduling Affinity" field will now have a "0" in it. Which according to the help information would give the vCPU of the VM affinity to processor 0, whereas what I actual want is to have no specified affinity.
This would to be at odds with the online help.
How do I clear the CPU affinity list using PowerCLI?
I have tried:
get-vm -name $VMName | Get-VMResourceConfiguration | Set-VMResourceConfiguration -CpuAffinityList ([string]::Empty)
That gave the same result.
I see, in that case try with an empty array
Set-VMResourceConfiguration -CpuAffinityList @()
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Isn't the list cleared when you do
Set-VMResourceConfiguration -CpuAffinity NoAffinity
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Yes but we are trying to avoid the obsolete commands.
Which obsolete command?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Sorry I meant parameter, "CpuAffinity"
H:\> get-vm -name $VMName | Get-VMResourceConfiguration | Set-VMResourceConfiguration -CpuAffinity NoAffinity
WARNING: Parameter 'CpuAffinity' is obsolete. Parameter 'CpuAffinity' is obsolete. Please use 'CpuAffinityList' isntead.
WARNING: The 'CpuAffinity' parameter of 'Set-VmResourceConfiguration' cmdlet is deprecated. Use the 'CpuAffinityList' parameter inst
ead.
VM NumCpuShares CpuSharesLevel NumMemShares MemSharesLevel
-- ------------ -------------- ------------ --------------
Dummy04 1000 Normal 320 Normal
I see, in that case try with an empty array
Set-VMResourceConfiguration -CpuAffinityList @()
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Bingo!
Thanks Luc.
Why don't the other methods work, where does the zero come from when specifying an empty string?
I suspect the code behind the cmdlet parameter checks how many elements there are.
Check the difference between
''.Count
But this is my assumption, I would need to see the code behind the cmdlet for that.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
So a string always has at least one character in it? A null?
Something to be wary of none-the-less.
Interesting.
No, an empty string is not a $null object.
The $null indicates the non-existance of an object, while an empty string is still an object, a string.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
I don't think that is what I said. I asked if a string always has at least one character, hence ''.count being 1, and if this one character was a $null?
No, there isn't one character in there.
The Count property should be considered as counting how many elements there are in an array.
For a scalar, which also has the Count property, it should be considered as an array with one element, albeit an empty string.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Yes, I had just come back to say much the same thing after have a tinker with:
''.Count = 1
('').Count = 1
''.Length = 0
('','').Count = 2
($null).Count = 0
Thanks for that.
Don't you sleep?
Sleeping is overrated :smileygrin:
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference