Permalink
Browse files

Improve run speed

Reduced the number of get-vm cmdlet executions and added some checking so that it won't try to write null data into a field.
  • Loading branch information...
NamedJason committed Oct 20, 2017
1 parent de1201a commit 1df261bf25f5fd8cd55871ea14f7758cbc7f2364
Showing with 3 additions and 2 deletions.
  1. +3 −2 PowerShell/Restore-CustomFields.ps1
@@ -8,13 +8,14 @@ param
#Import the source file that contains a VM Name column as well as columns for all of the meaningful fields
$sourceList = @()
$sourceList += import-csv $source
$allVMs = get-vm
#Go through each line of the source file, checking if the VM has any fields that need to be updated
$i = 1
foreach ($line in $sourceList){
write-progress -Activity "Restoring Custom Field Data" -Status "Working on $($line.name)" -PercentComplete($i++ / $sourceList.count * 100)
if ($vm = get-vm $line.name -erroraction silentlycontinue){
if ($vm = $allVMs | ? {$_.name -eq $line.name}){
foreach ($meaningfulField in $meaningfulFields){
if (($vm | Get-Annotation -CustomAttribute $meaningfulField).value -ne $line.$meaningfulField){
if ($line.$meaningfulField -and ($vm | Get-Annotation -CustomAttribute $meaningfulField).value -ne $line.$meaningfulField){
#if the VM exists and the data for the meaningful field is different on the VM than it is in the file, change the VM data.
$vm | Set-Annotation -CustomAttribute $meaningfulField -Value $line.$meaningfulField
}

0 comments on commit 1df261b

Please sign in to comment.