This is my second question in my quest to get this script finished (Restore VM Swap Script)....
One of the data points I'm collecting is what tags are on the production server that will be decommissioned once the restored version of it is up and running. I want to know what tags to add to the restored version of the server.
I have this below:
$srvr = Read-Host -Prompt 'Enter server name'
$vmnn = get-vm $srvr | Get-NetworkAdapter | Select-Object NetworkName
$vmds = get-vm $srvr | get-datastore | select Name
$vmfldr = get-vm $srvr | select Folder
$vmtags = Get-TagAssignment -entity $srvr | select -ExpandProperty Tag
Write-Host -ForegroundColor Yellow "Production server, $srvr, is in folder $($vmfldr.Folder) and on datastore $($vmds.Name) and on VLAN $($vmnn.NetworkName) with tags: $($vmtags)"
And when the script is run, it grabs everything without issue:
Production server, HELPER, is in folder ABC and on datastore CLONES and on VLAN 733-ABC with tags: Backup/OptOut DR/No Owner/ABC OS/Win2016
I don't know the best way to do what I'm thinking of doing, but I'm curious how I can get the tags assigned to a variable (?) so that I can then later on in the script say, "assign $backuptag, $DRtag, $Ownertag, $OStag to $restoredserver"
I'm assuming running the tags through a foreach loop, but can't seem to find how to assign a variable to each tag (if even possible).
As always, I appreciate the input and help!
You can use the New-Variable cmdlet for that.
Something like
Get-TagAssignment -entity $srvr |
ForEach-Object -Process {
New-Variable -Name "$($_.Tag.Category.Name)tag" -Value $_.Tag.Name
}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
You can use the New-Variable cmdlet for that.
Something like
Get-TagAssignment -entity $srvr |
ForEach-Object -Process {
New-Variable -Name "$($_.Tag.Category.Name)tag" -Value $_.Tag.Name
}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Awesome, thanks again. 🙂
Edit: I'm in the process of adding the tags to the restored server and curious how I can do this more efficiently. Not all VMs have the same amount of tags (they all should have an Owner, DR, Backup, OS tags, but unfortunately they don't....)...so with my code right now...
$myappTag = Get-Tag -Category "Application" -Name "$Applicationtag" -ErrorAction SilentlyContinue
$myownerTag = Get-Tag -Category "Owner" -Name "$Ownertag" -ErrorAction SilentlyContinue
$myosTag = Get-Tag -Category "OS" -Name "$OStag" -ErrorAction SilentlyContinue
$mybackupTag = Get-Tag -Category "Backup" -Name "$Backuptag" -ErrorAction SilentlyContinue
$mydrTag = Get-Tag -Category "DR" -Name "$DRtag" -ErrorAction SilentlyContinue
Get-VM -Name "$srvr" | New-TagAssignment -Tag $myappTag -ErrorAction SilentlyContinue | Out-Null
Get-VM -Name "$srvr" | New-TagAssignment -Tag $myownerTag -ErrorAction SilentlyContinue | Out-Null
Get-VM -Name "$srvr" | New-TagAssignment -Tag $myosTag -ErrorAction SilentlyContinue | Out-Null
Get-VM -Name "$srvr" | New-TagAssignment -Tag $mybackupTag -ErrorAction SilentlyContinue | Out-Null
Get-VM -Name "$srvr" | New-TagAssignment -Tag $mydrTag -ErrorAction SilentlyContinue | Out-Null
I get errors because the [prod] server that I was grabbing the tags from didn't have some of the tag [categories] on it, so obviously can't assign a null tag. I tried to do the -ErrorAction silentlycontinue but I still see the errors (so that's a 2nd question as to why I'm seeing them if I have the SilentlyContinue erroraction).
Would I need to put those new-variables into an array and then do a count on it and then based on the count, apply only those tags? Even saying that I'm getting confused....need to take a break for a bit lol.
You could test the variables after the Get-Tag, to check if they are $null or not.
Which lines give errors even with an ErrorAction?
Can you show the errors you are getting?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Ah okay, I think I might know what is needed to be done with that information...
I did add a check for the variables if they're null or not:
Get-TagAssignment -entity $srvr |
ForEach-Object -Process {
New-Variable -Name "$($_.Tag.Category.Name)tag" -Value $_.Tag.Name
}
$DRtag -eq $null
$Backuptag -eq $null
$OStag -eq $null
$Ownertag -eq $null
$Carbonitetag -eq $null
$Applicationtag -eq $null
Write-Host -ForegroundColor Yellow "Production server, $srvr, is in folder $($vmfldr.Folder) and on datastore $($vmds.Name) and on VLAN $($vmnn.NetworkName) with tags: $($vmtags)"
When the script was run, output was:
True
True
True
False
True
True
Production server, HELPER, is in folder ABC and on datastore CLONES and on VLAN 733-ABC with tags: Owner/ABC
Which is true because since I'm testing this, I only put 1 tag (the Owner tag) on the HELPER server.
The Owner tag did get applied to the restored server, but then I received the following errors (code will be below the errors):
Adding all appropriate tags on HELPER...
Get-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 D:\Todd\powercli_scripts\1restore-swap.ps1:176 char:51
+ ... Tag = Get-Tag -Category "Application" -Name "$Applicationtag" -ErrorA ...
+ ~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidData: (:) [Get-Tag], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,VMware.VimAutomation.ViCore.Cmdlets.Commands.Tagging.GetTag
Get-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 D:\Todd\powercli_scripts\1restore-swap.ps1:177 char:49
+ ... ycarTag = Get-Tag -Category "Carbonite" -Name "$Carbonitetag" -ErrorA ...
+ ~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidData: (:) [Get-Tag], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,VMware.VimAutomation.ViCore.Cmdlets.Commands.Tagging.GetTag
Get-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 D:\Todd\powercli_scripts\1restore-swap.ps1:179 char:41
+ $myosTag = Get-Tag -Category "OS" -Name "$OStag" -ErrorAction Silentl ...
+ ~~~~~~~~
+ CategoryInfo : InvalidData: (:) [Get-Tag], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,VMware.VimAutomation.ViCore.Cmdlets.Commands.Tagging.GetTag
Get-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 D:\Todd\powercli_scripts\1restore-swap.ps1:180 char:49
+ $mybackupTag = Get-Tag -Category "Backup" -Name "$Backuptag" -ErrorAc ...
+ ~~~~~~~~~~~~
+ CategoryInfo : InvalidData: (:) [Get-Tag], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,VMware.VimAutomation.ViCore.Cmdlets.Commands.Tagging.GetTag
Get-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 D:\Todd\powercli_scripts\1restore-swap.ps1:181 char:41
+ $mydrTag = Get-Tag -Category "DR" -Name "$DRtag" -ErrorAction Silentl ...
+ ~~~~~~~~
+ CategoryInfo : InvalidData: (:) [Get-Tag], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,VMware.VimAutomation.ViCore.Cmdlets.Commands.Tagging.GetTag
New-TagAssignment : Cannot bind argument to parameter 'Tag' because it is null.
At D:\Todd\powercli_scripts\1restore-swap.ps1:183 char:47
+ Get-VM -Name "$srvr" | New-TagAssignment -Tag $myappTag -ErrorAction ...
+ ~~~~~~~~~
+ CategoryInfo : InvalidData: (:) [New-TagAssignment], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,VMware.VimAutomation.ViCore.Cmdlets.Commands.Tagging.NewTagAssignment
New-TagAssignment : Cannot bind argument to parameter 'Tag' because it is null.
At D:\Todd\powercli_scripts\1restore-swap.ps1:184 char:47
+ Get-VM -Name "$srvr" | New-TagAssignment -Tag $mycarTag -ErrorAction ...
+ ~~~~~~~~~
+ CategoryInfo : InvalidData: (:) [New-TagAssignment], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,VMware.VimAutomation.ViCore.Cmdlets.Commands.Tagging.NewTagAssignment
New-TagAssignment : Cannot bind argument to parameter 'Tag' because it is null.
At D:\Todd\powercli_scripts\1restore-swap.ps1:186 char:47
+ Get-VM -Name "$srvr" | New-TagAssignment -Tag $myosTag -ErrorAction S ...
+ ~~~~~~~~
+ CategoryInfo : InvalidData: (:) [New-TagAssignment], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,VMware.VimAutomation.ViCore.Cmdlets.Commands.Tagging.NewTagAssignment
New-TagAssignment : Cannot bind argument to parameter 'Tag' because it is null.
At D:\Todd\powercli_scripts\1restore-swap.ps1:187 char:47
+ Get-VM -Name "$srvr" | New-TagAssignment -Tag $mybackupTag -ErrorActi ...
+ ~~~~~~~~~~~~
+ CategoryInfo : InvalidData: (:) [New-TagAssignment], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,VMware.VimAutomation.ViCore.Cmdlets.Commands.Tagging.NewTagAssignment
New-TagAssignment : Cannot bind argument to parameter 'Tag' because it is null.
At D:\Todd\powercli_scripts\1restore-swap.ps1:188 char:47
+ Get-VM -Name "$srvr" | New-TagAssignment -Tag $mydrTag -ErrorAction S ...
+ ~~~~~~~~
+ CategoryInfo : InvalidData: (:) [New-TagAssignment], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,VMware.VimAutomation.ViCore.Cmdlets.Commands.Tagging.NewTagAssignment
Code:
$myappTag = Get-Tag -Category "Application" -Name "$Applicationtag" -ErrorAction SilentlyContinue
$mycarTag = Get-Tag -Category "Carbonite" -Name "$Carbonitetag" -ErrorAction SilentlyContinue
$myownerTag = Get-Tag -Category "Owner" -Name "$Ownertag" -ErrorAction SilentlyContinue
$myosTag = Get-Tag -Category "OS" -Name "$OStag" -ErrorAction SilentlyContinue
$mybackupTag = Get-Tag -Category "Backup" -Name "$Backuptag" -ErrorAction SilentlyContinue
$mydrTag = Get-Tag -Category "DR" -Name "$DRtag" -ErrorAction SilentlyContinue
Get-VM -Name "$srvr" | New-TagAssignment -Tag $myappTag -ErrorAction SilentlyContinue | Out-Null
Get-VM -Name "$srvr" | New-TagAssignment -Tag $mycarTag -ErrorAction SilentlyContinue | Out-Null
Get-VM -Name "$srvr" | New-TagAssignment -Tag $myownerTag -ErrorAction SilentlyContinue | Out-Null
Get-VM -Name "$srvr" | New-TagAssignment -Tag $myosTag -ErrorAction SilentlyContinue | Out-Null
Get-VM -Name "$srvr" | New-TagAssignment -Tag $mybackupTag -ErrorAction SilentlyContinue | Out-Null
Get-VM -Name "$srvr" | New-TagAssignment -Tag $mydrTag -ErrorAction SilentlyContinue | Out-Null
I'm guessing I could do some if statements saying, "if $Ownertag -eq $null, skip it, else tag the restored server with $Ownertag" ?
Correct.
You only seem to be getting that error for all the Tags that are not in the Owner Category.
So yes, adding a test if the variable exists (with Get-Variable) avoids those errors.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Okay, thanks LucD. 🙂
And this question may be the wrong spot, but when I tried to post that reply on Friday, I thought it posted fine; however, when I checked a few minutes later, it wasn't there. I copied and pasted it back in and posted it again. Everything seemed fine again, but it still didn't post.
I received an email from a moderator saying that the first post was marked as spam and that's why it didn't post. How would I know it was marked as spam as there was no indication of that? 😕
Afaik, if a post from a user is marked as spam (automatically or by another user), the originator of that post should get a message stating that.
If you did not receive such a message, it might have been lost on the VMTN side or somewhere in between.
Such questions should probably go to Technical Community Resources and Help Discussions - VMware Technology Network VMTN
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference