PowerCLI

 View Only
  • 1.  Query VM for tags from 4 vCenters

    Posted Jul 06, 2023 04:38 PM

    Hi Guys,

    Hope all is great, I need your assistance with a query I am trying to do and unfortunately it doesn't work.

    I have a list of Windows machines that I query from SCCM and get it to my varible (see attach below), the result shows "Windows Computers Name", " "AD Name", "Type of machines"

    Then I go over for each VM and ask for tags + custom attribute, IP and VMNAME, the thing is that if I run this on certain vcenter all great and works get all results as wanted

    But, I must run it parallel for all my 4 vcenters becasue those WIndows machines located on different 4 vcetners, the main purpose is the get "Pending Reboot machines" from SCCM list  and then get their tags and CA.

    The problem is that i get results which are great but no for all VM's some VM are not returning tags and custom attributes even that they have it, even that again if I run it direct query to the VM i got results.

    In SCCM query I got Windows pending reboot machines some of them are physical so I am in "Else" if Windows machine name does not appear on Vcenter just type Windows name and type (the type is VM or Physical server that SCCM provides)

     

    This is my code:

    $rrr

    Screenshot 2023-07-06 193447.png

     

     

    $poweredOnWindowsVMs = Get-VM -Server "vc1.vmware","vc2.vmware","vc3.vmware","vc4.vmware" | Where-Object {$_.PowerState -eq "PoweredOn" -and $_.Guestid -like "*windows*"}
    
    foreach ($sccm in $rrr) {
    
    if ($poweredOnWindowsVMs.Name -contains  $sccm.Name) {
    $Owner = (Get-VM -Name $sccm.Name).CustomFields | Where-Object {$_.Key -eq "Owner Email:"} | Select-Object -ExpandProperty Value
    $VMIP = (Get-VM -Name $sccm.Name).Guest.IPAddress | Where-Object {$_ -notlike "*:*"}
    $Department = Get-VM -Name $sccm.Name | Get-TagAssignment | Where-Object {$_.Tag -like "*department*"} | Select-Object -ExpandProperty Tag | Select-Object -ExpandProperty Name
    $VMname = (Get-VM -Name $sccm.Name).Name
    
    [pscustomobject]@{
    "VMOwner" = $Owner
    "VMName" = $VMname
    "IP" = $VMIP -join ';'
    "Type" = $sccm.Type
    "Department" = $Department
    "ADName" = $sccm.ADname
    }
    } else {
    [pscustomobject]@{
    "ADName" = $sccm.adname
    "Type" = $sccm.type
    }
    }
    }
    )

     



  • 2.  RE: Query VM for tags from 4 vCenters

    Posted Jul 06, 2023 04:52 PM

    Did you run your script from a PS prompt while connected to all vCenters?



  • 3.  RE: Query VM for tags from 4 vCenters

    Posted Jul 06, 2023 05:10 PM

    Yeah sure, I was think that maybe due to autentication issue i dont get results, but again if  I run these tags query per VM, or certain vcetner all is working...

     

    Some is wrong with my if condition 



  • 4.  RE: Query VM for tags from 4 vCenters

    Posted Jul 06, 2023 05:20 PM

    Can you use a Transcipt log in the script, that way you might be able to see what exactly goes wrong.



  • 5.  RE: Query VM for tags from 4 vCenters

    Posted Jul 06, 2023 05:42 PM

    It wont help...

    you will get the if and else result without any error.. 'the things that freaked me out is that those unresult VM has tags and CA I look for 

     

    Merop3004_0-1688665329293.png

     

     



  • 6.  RE: Query VM for tags from 4 vCenters

    Posted Jul 06, 2023 06:22 PM

    No, but you can display the content of the variables you are using