COS
Expert
Expert

Change output of a script to include the VM name?........maybe?

Jump to solution

So I'm running this script found here: VMX Raiders Revisited - LucD notes

It runs and does what I need it to do. It outputs the below when finished....

PS C:\Users\user> C:\Users\user\Desktop\ImportVMsFromDatastore.ps1

Name                           State      % Complete Start Time   Finish Time

----                           -----      ---------- ----------   -----------

RegisterVM_Task                Running             0 01:26:59 PM             

RegisterVM_Task                Running             0 01:26:59 PM             

RegisterVM_Task                Running             0 01:27:00 PM             

RegisterVM_Task                Running             0 01:27:00 PM             

My question is, how do you make it output the actual VM name instead of "RegisterVM_Task "?

Or is that just the step the script is at for that specific VM in the list?

Also, how about stamping the finish time as well? Not important but just for completeness for my OCD.......lol

Thanks

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership

Try like this

$Cluster = "MyCluster"

$Datastores = "MyDatastore"

$VMFolder = "MyFolder"


$ESXHost = Get-Cluster $Cluster | Get-VMHost | select -First 1


foreach ($Datastore in Get-Datastore $Datastores) {

   # Collect .vmx paths of registered VMs on the datastore

   $registered = @{}

   Get-VM -Datastore $Datastore | % {$_.Extensiondata.LayoutEx.File | where {$_.Name -like "*.vmx"} | % {$registered.Add($_.Name, $true)}}


   # Set up Search for .VMX Files in Datastore

   New-PSDrive -Name TgtDS -Location $Datastore -PSProvider VimDatastore -Root '\' | Out-Null

   $unregistered = @(Get-ChildItem -Path TgtDS: -Recurse | `

   where {$_.FolderPath -notmatch ".snapshot" -and $_.Name -like "*.vmx" -and !$registered.ContainsKey($_.DatastoreFullPath)})

   Remove-PSDrive -Name TgtDS


   #Register all .vmx Files as VMs on the datastore

   $task = @()

   foreach ($VMXFile in $unregistered) {

   $task += New-VM -VMFilePath $VMXFile.DatastoreFullPath -VMHost $ESXHost -Location $VMFolder -RunAsync

   }

   Wait-Task -Task $task | Out-Null

   $task | select @{N = 'VM'; E = {$_.Result.Name}}, StartTime, FinishTime, PercentComplete, State

}


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

0 Kudos
2 Replies
LucD
Leadership
Leadership

Try like this

$Cluster = "MyCluster"

$Datastores = "MyDatastore"

$VMFolder = "MyFolder"


$ESXHost = Get-Cluster $Cluster | Get-VMHost | select -First 1


foreach ($Datastore in Get-Datastore $Datastores) {

   # Collect .vmx paths of registered VMs on the datastore

   $registered = @{}

   Get-VM -Datastore $Datastore | % {$_.Extensiondata.LayoutEx.File | where {$_.Name -like "*.vmx"} | % {$registered.Add($_.Name, $true)}}


   # Set up Search for .VMX Files in Datastore

   New-PSDrive -Name TgtDS -Location $Datastore -PSProvider VimDatastore -Root '\' | Out-Null

   $unregistered = @(Get-ChildItem -Path TgtDS: -Recurse | `

   where {$_.FolderPath -notmatch ".snapshot" -and $_.Name -like "*.vmx" -and !$registered.ContainsKey($_.DatastoreFullPath)})

   Remove-PSDrive -Name TgtDS


   #Register all .vmx Files as VMs on the datastore

   $task = @()

   foreach ($VMXFile in $unregistered) {

   $task += New-VM -VMFilePath $VMXFile.DatastoreFullPath -VMHost $ESXHost -Location $VMFolder -RunAsync

   }

   Wait-Task -Task $task | Out-Null

   $task | select @{N = 'VM'; E = {$_.Result.Name}}, StartTime, FinishTime, PercentComplete, State

}


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
COS
Expert
Expert

You are the MAN!!!

Thanks dude!

0 Kudos