VMware Cloud Community
tdubb123
Expert
Expert

append domain to this list

get-vmhost -name (esxhost.txt)

$domain = "domain.com"

any idea how to add the domain to the list in one line?

Reply
0 Kudos
9 Replies
LucD
Leadership
Leadership

Is esxhost.txt a file with names in it?


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

Reply
0 Kudos
tdubb123
Expert
Expert

yes

 

host1

host2

etc...

Reply
0 Kudos
tdubb123
Expert
Expert

ok got it

 

get-content esxhosts.txt | % { get-vmhost $_* }

Reply
0 Kudos
tdubb123
Expert
Expert

another question,

 

I got a list of VMs that I am getting like this

 

get-vm -name (get-content vms.txt)

some are available while some are not.

How do I log the ones that are not available or not in vcenter inventory?

Reply
0 Kudos
LucD
Leadership
Leadership

Something like this

try {
   Get-VM -Name (Get-Content -Path vms.txt ) -ErrorAction Stop
}
catch {
   $error[0].exception.tostring().Split("'")[1] | Out-File -FilePath .\vm-not-found.txt -Append
}


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

Reply
0 Kudos
tdubb123
Expert
Expert

does the -ErrorAction stop stop the script at first error or should i use silentlycontinue?

Reply
0 Kudos
LucD
Leadership
Leadership

No, that makes sure the Catch block is executed when the VM is not found


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

Reply
0 Kudos
tdubb123
Expert
Expert

i is only logging 1 vm. where there are lots more

Reply
0 Kudos
LucD
Leadership
Leadership

Indeed, the exception happens only once.
Then the only solution I see is to use a loop over the content of the TXT file

Get-Content -Path .\vms.txt |
ForEach-Object -Process {
   try {
      Get-VM -Name $_ -ErrorAction Stop
   } catch {
      $error[0].exception.tostring().Split("'")[1] | Out-File -FilePath .\vm-not-found.txt -Append
   }
}


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

Reply
0 Kudos