Greetings,
I'm attempting to read the running OS from $vm.ExtensionData.Guest.GuestFullName and tag the VM with the running OS. If the tag doesn't exist, I'm trying to create the new tag and assign it to the VM. I'm attempting to write the running OS value to the $NewTag variable, however it fails everytime on two specific VMs. Here is the code:
#Import your list of VMs, then Get-VM on all of them
$vmlist = get-content C:\ece\vCenter\ostest.csv
$vms = Get-VM $vmlist
#Loop through each VM, get the Running OS, assign the Running OS tag
foreach($vm in $vms)
{
$vm |Sort-Object -Property Name |
Get-View -Property @("Name", "Config.GuestFullName", "Guest.GuestFullName") |
Select -Property Name,
@{N="Configured OS";E={$_.Config.GuestFullName}},
@{N="Running OS";E={$_.Guest.GuestFullName}} |
where{$_.Tag.Category -eq 'os'}
$Name = $vm.Name
$Tag = $vm.ExtensionData.Guest.GuestFullName
if($Tag){
Write-Host ".... Assigning $Tag in Category "os" to $Name"
New-TagAssignment -Entity $vm -Tag $Tag -Server (([uri]$vm.ExtensionData.Client.ServiceUrl).Host)
}else {
$NewTag = $vm.ExtensionData.Guest.GuestFullName
New-Tag -Name $NewTag -Category os -Server (([uri]$vm.ExtensionData.Client.ServiceUrl).Host)
Write-Host ".... New "$NewTag" tag in Category "os""
New-TagAssignment -Entity $vm -Tag $NewTag -Server (([uri]$vm.ExtensionData.Client.ServiceUrl).Host)
}
}
Here is the ostest.csv:
roberts2016test |
EFVROPSTESTER |
2k12test |
DPA-FCRTester |
dnrapt007 |
dnrapt10 |
idmtestDOLA |
DOLAP2V01 |
IsilonSimulator |
rhsat_wintest |
OITSFTPP01 |
Testw16sql16 |
OITSFTPA01 |
DPAOACWEB01 |
idmtestDPA |
idmtestHC |
DNR-FCR-Tester |
idmtestDNR |
DNR-Isilon |
DNRWEBIPAWSTEST |
DNRWin10Test02 |
OITvRAWMGT01 |
Dow-Developer-VM06-Shafi |
Dow-Developer-VM08 |
Dow-Developer-VM05 |
DNRNXGENTEST03 |
IDS-Rubika-TST
|
Output:
PS C:\ece\vCenter> .\50.ps1
.... Assigning Microsoft Windows Server 2008 (64-bit) in Category os to DPA-FCRTester
Tag Entity
--- ------
os/Microsoft Windows Server 2008 (64-... DPA-FCRTester
.... Assigning Microsoft Windows Server 2016 or later (64-bit) in Category os to dnrapt007
os/Microsoft Windows Server 2016 or l... dnrapt007
.... Assigning Microsoft Windows Server 2016 or later (64-bit) in Category os to dnrapt10
os/Microsoft Windows Server 2016 or l... dnrapt10
.... Assigning Microsoft Windows Server 2016 or later (64-bit) in Category os to idmtestDOLA
os/Microsoft Windows Server 2016 or l... idmtestDOLA
.... Assigning Microsoft Windows Server 2008 R2 (64-bit) in Category os to DOLAP2V01
os/Microsoft Windows Server 2008 R2 (... DOLAP2V01
.... Assigning FreeBSD (64-bit) in Category os to IsilonSimulator
os/FreeBSD (64-bit) IsilonSimulator
.... Assigning Microsoft Windows Server 2012 (64-bit) in Category os to rhsat_wintest
os/Microsoft Windows Server 2012 (64-... rhsat_wintest
.... Assigning CentOS 6 (64-bit) in Category os to OITSFTPP01
os/CentOS 6 (64-bit) OITSFTPP01
.... Assigning Microsoft Windows Server 2016 or later (64-bit) in Category os to Testw16sql16
os/Microsoft Windows Server 2016 or l... Testw16sql16
.... Assigning CentOS 6 (64-bit) in Category os to OITSFTPA01
os/CentOS 6 (64-bit) OITSFTPA01
.... Assigning Microsoft Windows Server 2008 R2 (64-bit) in Category os to DPAOACWEB01
os/Microsoft Windows Server 2008 R2 (... DPAOACWEB01
.... Assigning Microsoft Windows Server 2016 or later (64-bit) in Category os to idmtestDPA
os/Microsoft Windows Server 2016 or l... idmtestDPA
.... Assigning Microsoft Windows Server 2016 or later (64-bit) in Category os to idmtestHC
os/Microsoft Windows Server 2016 or l... idmtestHC
New-Tag : Cannot validate argument on parameter 'Name'. The argument is null or empty. Provide an argument that is not null or empty, and then try the command again.
At C:\ece\vCenter\50.ps1:36 char:18
+ New-Tag -Name $NewTag -Category os -Server (([uri]$vm.Ext ...
+ ~~~~~~~
+ CategoryInfo : InvalidData: (:) [New-Tag], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,VMware.VimAutomation.ViCore.Cmdlets.Commands.Tagging.NewTag
.... New tag in Category os
New-TagAssignment : Cannot bind argument to parameter 'Tag' because it is null.
At C:\ece\vCenter\50.ps1:40 char:39
+ New-TagAssignment -Entity $vm -Tag $NewTag -Server (([uri ...
+ ~~~~~~~
+ CategoryInfo : InvalidData: (:) [New-TagAssignment], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,VMware.VimAutomation.ViCore.Cmdlets.Commands.Tagging.NewTagAssignment
.... Assigning Microsoft Windows Server 2016 or later (64-bit) in Category os to idmtestDNR
os/Microsoft Windows Server 2016 or l... idmtestDNR
.... Assigning Microsoft Windows Server 2012 (64-bit) in Category os to DNR-Isilon
os/Microsoft Windows Server 2012 (64-... DNR-Isilon
.... Assigning Microsoft Windows Server 2012 (64-bit) in Category os to DNRWEBIPAWSTEST
os/Microsoft Windows Server 2012 (64-... DNRWEBIPAWSTEST
.... Assigning Microsoft Windows 10 (64-bit) in Category os to DNRWin10Test02
os/Microsoft Windows 10 (64-bit) DNRWin10Test02
.... Assigning Microsoft Windows Server 2012 (64-bit) in Category os to OITvRAWMGT01
os/Microsoft Windows Server 2012 (64-... OITvRAWMGT01
.... Assigning Microsoft Windows 7 (64-bit) in Category os to Dow-Developer-VM06-Shafi
os/Microsoft Windows 7 (64-bit) Dow-Developer-VM06-Shafi
.... Assigning Microsoft Windows 7 (64-bit) in Category os to Dow-Developer-VM08
os/Microsoft Windows 7 (64-bit) Dow-Developer-VM08
.... Assigning Microsoft Windows 7 (64-bit) in Category os to Dow-Developer-VM05
os/Microsoft Windows 7 (64-bit) Dow-Developer-VM05
.... Assigning Microsoft Windows Server 2016 or later (64-bit) in Category os to DNRNXGENTEST03
os/Microsoft Windows Server 2016 or l... DNRNXGENTEST03
.... Assigning Microsoft Windows Server 2012 (64-bit) in Category os to IDS-Rubika-TST
os/Microsoft Windows Server 2012 (64-... IDS-Rubika-TST
New-Tag : Cannot validate argument on parameter 'Name'. The argument is null or empty. Provide an argument that is not null or empty, and then try the command again.
At C:\ece\vCenter\50.ps1:36 char:18
+ New-Tag -Name $NewTag -Category os -Server (([uri]$vm.Ext ...
+ ~~~~~~~
+ CategoryInfo : InvalidData: (:) [New-Tag], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,VMware.VimAutomation.ViCore.Cmdlets.Commands.Tagging.NewTag
.... New tag in Category os
New-TagAssignment : Cannot bind argument to parameter 'Tag' because it is null.
At C:\ece\vCenter\50.ps1:40 char:39
+ New-TagAssignment -Entity $vm -Tag $NewTag -Server (([uri ...
+ ~~~~~~~
+ CategoryInfo : InvalidData: (:) [New-TagAssignment], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,VMware.VimAutomation.ViCore.Cmdlets.Commands.Tagging.NewTagAssignment
.... Assigning Microsoft Windows Server 2012 (64-bit) in Category os to EFVROPSTESTER
os/Microsoft Windows Server 2012 (64-... EFVROPSTESTER
.... Assigning Microsoft Windows Server 2012 (64-bit) in Category os to 2K12TEST
os/Microsoft Windows Server 2012 (64-... 2K12TEST
.... Assigning Microsoft Windows Server 2016 or later (64-bit) in Category os to roberts2016test
os/Microsoft Windows Server 2016 or l... roberts2016test
Are you sure that the failing VMs are powered on and have the VMware Tools running?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Thanks LucD! You are correct.
Also, Big thanks for all of your posts and replies! You have helped me a lot!
LucD, quick question: my $NewTag variable after the else statement seems to fail with a null value. How can I take the running OS, create a new tag fromit, and assign it to the VM? I get this error:
New-Tag : Cannot validate argument on parameter 'Name'. The argument is null or empty. Provide an argument that is not null or empty, and then try the command again.
At C:\ece\vCenter\50.ps1:36 char:18
+ New-Tag -Name $NewTag -Category os -Server (([uri]$vm.Ext ...
+ ~~~~~~~
+ CategoryInfo : InvalidData: (:) [New-Tag], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,VMware.VimAutomation.ViCore.Cmdlets.Commands.Tagging.NewTag
Try this alternative version I created.
It uses try-catch to get/create the Category and the Tags.
Note: I notice you do a Get-Content on a CSV file.
Watch out with the 1st line that normally contains the column names.
$vmlist = Get-Content -Path 'C:\ece\vCenter\ostest.csv'
try{
$cat = Get-TagCategory -Name OS -ErrorAction Stop
}
catch{
$cat = New-TagCategory -Name OS -Cardinality Single -EntityType VirtualMachine
}
#Loop through each VM, get the Running OS, assign the Running OS tag
Get-VM -Name $vmlist -PipelineVariable vm | ForEach-Object -Process {
if($vm.ExtensionData.Guest.GuestFullName){
try{
$tag = Get-Tag -Category OS -Name $vm.ExtensionData.Guest.GuestFullName
}
catch{
$tag = New-Tag -Name $vm.ExtensionData.Guest.GuestFullName -Category $cat
}
$present = Get-TagAssignment -Entity $vm -Category $cat | where{$_.Tag.Name -eq $vm.ExtensionData.Guest.GuestFullName}
if(-not $present){
New-TagAssignment -Entity $vm -Tag $tag -Confirm:$false
}
}
else{
Write-Warning -Message "Could not determine guest OS for $($vm.Name)"
}
}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Hi, sorry for the delay in testing. I've had to add a -Server (([uri]$vm.ExtensionData.Client.ServiceUrl).Host) to each of the vCenter operations because I'm working across multiple vCenters:
#Import your list of VMs, then Get-VM on all of them
$vmlist = Get-Content -Path 'C:\ece\vCenter\ostest.csv'
try{
}
catch{
$cat = New-TagCategory -Name OS -Cardinality Single -EntityType VirtualMachine -Server (([uri]$vm.ExtensionData.Client.ServiceUrl).Host)
}
#Loop through each VM, get the Running OS, assign the Running OS tag
Get-VM -Name $vmlist -PipelineVariable vm | ForEach-Object -Process {
if($vm.ExtensionData.Guest.GuestFullName){
try{
$tag = Get-Tag -Category os -Name $vm.ExtensionData.Guest.GuestFullName -Server (([uri]$vm.ExtensionData.Client.ServiceUrl).Host)
}
catch{
$tag = New-Tag -Name $vm.ExtensionData.Guest.GuestFullName -Category $cat -Server (([uri]$vm.ExtensionData.Client.ServiceUrl).Host)
}
$present = Get-TagAssignment -Entity $vm -Category $cat | where{$_.Tag.Name -eq $vm.ExtensionData.Guest.GuestFullName}
if(-not $present){
New-TagAssignment -Entity $vm -Tag $tag -Confirm:$false -Server (([uri]$vm.ExtensionData.Client.ServiceUrl).Host)
}
}
else{
Write-Warning -Message "Could not determine guest OS for $($vm.Name)"
}
}
I'm receiving this error at run time:
PS C:\ece\vCenter\tested> .\lucd1.ps1
Get-Tag : 11/20/2019 8:58:26 AM Get-Tag Tag with name 'Microsoft Windows Server 2003 Standard (32-bit)' was not found using the specified filter(s).
At C:\ece\vCenter\tested\lucd1.ps1:25 char:20
+ ... $tag = Get-Tag -Category os -Name $vm.ExtensionData.Guest.GuestF ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (:) [Get-Tag], VimException
+ FullyQualifiedErrorId : Core_OutputHelper_WriteNotFoundError,VMware.VimAutomation.ViCore.Cmdlets.Commands.Tagging.GetTag
New-TagAssignment : Cannot bind argument to parameter 'Tag' because it is null.
At C:\ece\vCenter\tested\lucd1.ps1:41 char:48
+ New-TagAssignment -Entity $vm -Tag $tag -Confirm:$false - ...
+ ~~~~
+ CategoryInfo : InvalidData: (:) [New-TagAssignment], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,VMware.VimAutomation.ViCore.Cmdlets.Commands.Tagging.NewTagAssignment
I tried re-defining $tag before line 41 with $tag = New-Tag -Name $vm.ExtensionData.Guest.GuestFullName -Category $cat -Server (([uri]$vm.ExtensionData.Client.ServiceUrl).Host) but it failed...
Thanks
Try adding -ErrorAction Stop on the Get-Tag cmdlet.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Thanks, works much better now. Still throws a null 'category' error when no OS is found. I tried adding an -ErrorAction Stop after the Write-Warning but that failed. Thinkiong I need to stop the New-Tag cmdlet somehow if no OS is found...
PS C:\ece\vCenter\tested> .\lucd1.ps1
Tag Entity
--- ------
os/Microsoft Windows Server 2008 (64-... LECS-T2-SQL
WARNING: Could not determine guest OS for DPAOACSQLT1
New-Tag : Cannot bind argument to parameter 'Category' because it is null.
At C:\ece\vCenter\tested\lucd1.ps1:31 char:82
+ ... ag -Name $vm.ExtensionData.Guest.GuestFullName -Category $cat -Server ...
+ ~~~~
+ CategoryInfo : InvalidData: (:) [New-Tag], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,VMware.VimAutomation.ViCore.Cmdlets.Commands.Tagging.NewTag
os/Microsoft Windows Server 2008 R2 (... SOITORTST0
WARNING: Could not determine guest OS for test-shu
New-Tag : Cannot bind argument to parameter 'Category' because it is null.
At C:\ece\vCenter\tested\lucd1.ps1:31 char:82
+ ... ag -Name $vm.ExtensionData.Guest.GuestFullName -Category $cat -Server ...
+ ~~~~
+ CategoryInfo : InvalidData: (:) [New-Tag], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,VMware.VimAutomation.ViCore.Cmdlets.Commands.Tagging.NewTag
The warning and the New-Tag error are afaik not coming from the same VM.
Try replacing the Category value on the New-Tag cmdlet
$tag = Get-Tag -Category $cat -Name $vm.ExtensionData.Guest.GuestFullName -Server (([uri]$vm.ExtensionData.Client.ServiceUrl).Host)
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
I'm getting this error when I hit a VM whose running OS tag does not yet exist:
PS C:\ece\vcenter\tested> .\lucd1.ps1
Get-Tag : 11/21/2019 4:27:37 PM Get-Tag Tag with name 'Microsoft Windows Server 2003 Standard (32-bit)' was not found
using the specified filter(s).
At C:\ece\vcenter\tested\lucd1.ps1:25 char:20
+ ... $tag = Get-Tag -Category $cat -Name $vm.ExtensionData.Guest.Gues ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (:) [Get-Tag], VimException
+ FullyQualifiedErrorId : Core_OutputHelper_WriteNotFoundError,VMware.VimAutomation.ViCore.Cmdlets.Commands.Tagging.G
etTag
New-TagAssignment : Cannot bind argument to parameter 'Tag' because it is null.
At C:\ece\vcenter\tested\lucd1.ps1:40 char:48
+ New-TagAssignment -Entity $vm -Tag $tag -Confirm:$false - ...
+ ~~~~
+ CategoryInfo : InvalidData: (:) [New-TagAssignment], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,VMware.VimAutomation.ViCore.Cmdlets.Commands
.Tagging.NewTagAssignment
The null value error occurs at this line:New-TagAssignment -Entity $vm -Tag $tag -Confirm:$false -Server (([uri]$vm.ExtensionData.Client.ServiceUrl).Host)
Do I need to re-declare the $tag variable before the line? Why is it returning a null value? There seems to be a value at "Tag with name 'Microsoft Windows Server 2003 Standard (32-bit)' was not found". Somehow the New-TagAssignment is failing...
Thanks!
Did you add -ErrorAction Stop on the Get-Tag cmdlet?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference