Automation

 View Only
  • 1.  append domain to this list

    Posted May 09, 2023 05:52 PM

    get-vmhost -name (esxhost.txt)

    $domain = "domain.com"

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



  • 2.  RE: append domain to this list

    Posted May 09, 2023 05:56 PM

    Is esxhost.txt a file with names in it?



  • 3.  RE: append domain to this list

    Posted May 09, 2023 05:58 PM

    yes

     

    host1

    host2

    etc...



  • 4.  RE: append domain to this list

    Posted May 09, 2023 06:22 PM

    ok got it

     

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



  • 5.  RE: append domain to this list

    Posted May 09, 2023 06:32 PM

    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?



  • 6.  RE: append domain to this list

    Posted May 09, 2023 08:06 PM

    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
    }


  • 7.  RE: append domain to this list

    Posted May 09, 2023 08:23 PM

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



  • 8.  RE: append domain to this list

    Posted May 09, 2023 08:54 PM

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



  • 9.  RE: append domain to this list

    Posted May 09, 2023 09:08 PM

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



  • 10.  RE: append domain to this list

    Posted May 09, 2023 09:22 PM

    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
       }
    }