PowerCLI

 View Only
  • 1.  How to get OU details of a VM

    Posted Oct 29, 2022 07:25 AM

    Hi,

    I would like to get the OU details added in the below script as I have below script to get the OU details but I am not sure how to add in the below script

    Please help!!

     

    Get-ADComputer -Identity "mydb1" -Properties * | select @{N='OU';E={$_.DistinguishedName -replace '^[^,]+,', ''}}

     

    $out = $global:defaultviservers |
    ForEach-Object -Process {
    $vc = $_
    Get-VM -Server $vc | Where-Object {$_.PowerState -eq 'PoweredOn' -and $_.extensiondata.config.createDate -ge (Get-Date).AddDays(-1)} |
    Sort -Property CreatedTime -Descending |
    Select @{N='vCenter';E={([System.Uri]$vc.ServiceUri.AbsoluteUri).Host}},
    Folder,
    @{N="VM Name";E={$_.Name}},
    @{N="IPAddr";E={@($_.guest.IPAddress[0])}}, NumCPU, MemoryGB,
    @{N="Used_HDD(GB)";E={[math]::round($_.UsedSpaceGB)}},
    @{N="OS";E={$_.Guest.OSFullName}},
    @{N="CreateDate";E={$_.extensiondata.config.createDate}}
    }
    $out | ft -auto

     



  • 2.  RE: How to get OU details of a VM

    Posted Oct 29, 2022 08:27 AM

    You could use a calculated property with Invoke-VMScript

     

    $code = @'
    (Get-ADComputer -Identity $($_.Guest.HostName.Split('.')[0])).DistinguishedName -replace '^[^,]+,', ''
    '@
    $yesterday = (Get-Date).AddDays(-1)
    
    Get-VM |
    Where-Object { $_.PowerState -eq 'PoweredOn' -and $_.ExtensionData.Config.CreateDate -ge $yesterday} |
    Sort-Object -Property CreatedTime -Descending |
    Select-Object @{N = 'vCenter'; E = { ([System.Uri]$_.ExtensionData.Client.ServiceUrl).Host } },
    Folder,
    @{N = "VM Name"; E = { $_.Name } },
    @{N = "IPAddr"; E = { @($_.guest.IPAddress[0]) } }, NumCPU, MemoryGB,
    @{N = "Used_HDD(GB)"; E = { [math]::round($_.UsedSpaceGB) } },
    @{N = "OS"; E = { $_.Guest.OSFullName } },
    @{N = "CreateDate"; E = { $_.extensiondata.config.createDate } },
    @{N = 'DistinguishedName'; E = {
        (Invoke-VMScript -VM $_ -ScriptText $ExecutionContext.InvokeCommand.ExpandString($code) -ScriptType Powershell).ScriptOutput
      }
    }

     



  • 3.  RE: How to get OU details of a VM

    Posted Oct 29, 2022 09:26 AM

    Thank you LucD for your help.

    Since we have mix of Windows and Linux and not sure if our VM has AD cmdlet installed or not and Invoke-VMscript was working for Windows but showing blank for Linux. I tried as below using the DNS name. 

    Let me know, if this needs any tweaks from your end.

    $yesterday = (Get-Date).AddDays(-1)

    Get-VM -Name MyDB1 |
    Where-Object {$_.PowerState -eq 'PoweredOn' -and $_.ExtensionData.Config.CreateDate -ge $yesterday} |
    Sort-Object -Property CreatedTime -Descending |
    Select-Object @{N = 'vCenter'; E = { ([System.Uri]$_.ExtensionData.Client.ServiceUrl).Host } },
    Folder,
    @{N = "DNS_Name";E={((($_.Guest.HostName) -split '\.')[0])}},
    @{N = "VM Name";E={$_.Name}},
    @{N = "IPAddr";E={@($_.guest.IPAddress[0])}}, NumCPU, MemoryGB,
    @{N = "Used_HDD(GB)"; E = { [math]::round($_.UsedSpaceGB) } },
    @{N = "OS"; E = { $_.Guest.OSFullName } },
    @{N = "CreateDate"; E = { $_.extensiondata.config.createDate } },
    @{N = 'DistinguishedName';E={@((Get-ADComputer -Identity ((($_.Guest.HostName) -split '\.')[0])).DistinguishedName -replace '^[^,]+,', '')}}



  • 4.  RE: How to get OU details of a VM

    Posted Oct 29, 2022 11:19 AM

    LucD,

    @{N = 'DistinguishedName';E={@(Get-ADComputer -Identity ($_.Guest.HostName.split(".")[0]).DistinguishedName -replace '^[^,]+,', '')}}

    shows blank output



  • 5.  RE: How to get OU details of a VM

    Posted Oct 29, 2022 11:21 AM

    Not sure what you mean.
    Are your Linux boxes added to the AD domain?
    Or do you want to avoid calling Invoke-VMScript when it is a VM with a Linux Guest OS?



  • 6.  RE: How to get OU details of a VM

    Posted Oct 29, 2022 11:34 AM

    LucD,

    Yes, all our Linux VMs are also joined to AD. so Invoke-VMScript might not work for Linux VMs.

    So using the DNS Name, I would like to get the OU detail I tried as below but getting the blank output for OU.

    @{N = 'DistinguishedName';E={@(Get-ADComputer -Identity ($_.Guest.HostName.split(".")[0]).DistinguishedName -replace '^[^,]+,', '')}}



  • 7.  RE: How to get OU details of a VM

    Posted Oct 29, 2022 11:40 AM

    Is PowerShell installed on those Linux VMs?
    If yes, that Get-ADComputer cmdlet should work.
    But since it doesn't return anything, I guess not.

    Another option could be to do the Get-AdComputer cmdlet on the local machine, not the VM.
    Or you could also do an ldpasearch instead of Get-ADComputer on Linux boxes.



  • 8.  RE: How to get OU details of a VM

    Posted Oct 29, 2022 11:47 AM

    LucD,

    Powershell is not installed on any of the VMs. I have installed the Powershell AD cmdlets on my machine where I need to run the script to get the VM and OU details.

    Please help, how can I get the OU details using Get-VM DNS name for below.

    @{N = 'DistinguishedName';E={@(Get-ADComputer -Identity ($_.Guest.HostName.split(".")[0]).DistinguishedName -replace '^[^,]+,', '')}}



  • 9.  RE: How to get OU details of a VM
    Best Answer

    Posted Oct 29, 2022 12:08 PM

    This works for me

    @{N = 'DistinguishedName'; E = { (Get-ADComputer -Identity ($_.Guest.HostName.split(".")[0])).DistinguishedName -replace '^[^,]+,', ''} }
    


  • 10.  RE: How to get OU details of a VM

    Posted Oct 29, 2022 12:11 PM

    Thank you very much. that worked