PowerCLI

 View Only
Expand all | Collapse all

Extract operating system and application information from a VM

  • 1.  Extract operating system and application information from a VM

    Posted Jan 18, 2011 03:14 PM

    Hi,

    I have been tasked with documenting the types, versions and roles of our exisiting virtual infrastructure. We currently have around 50 VM's. I would like to know if there is a free utility or part of vsphere that I can run which will give me a report displaying

    VMName

    Host OS

    Installed applications (roles if 2008) and version numbers

    It would really save me a lot of time and effort if there was.


    Thanks

    Andy



  • 2.  RE: Extract operating system and application information from a VM

    Posted Jan 18, 2011 03:32 PM

    I think PowerCLI may be your best option to get this information.

    You can get some with RVTools, but the above may be better.



  • 3.  RE: Extract operating system and application information from a VM

    Posted Jan 19, 2011 09:39 AM

    Hi Troy,

    Thanks for the information, I have downloaded and installed PowerCli and trying to use it, I'm not much of a langauge expert and haven't done much scripting so the commands are all a bit alien to me, do you happen to know what command I would need to run to produce the list I would like

    Server name, OS, Application

    Also how would I then export this information?

    Sorry for all the questions.


    Thanks



  • 4.  RE: Extract operating system and application information from a VM

    Posted Jan 19, 2011 01:26 PM

    I've moved this thread to the PowerCLI Community, for better exposure.



  • 5.  RE: Extract operating system and application information from a VM

    Broadcom Employee
    Posted Jan 19, 2011 01:56 PM

    To report all VMs and their Guest OS you can use:

    Get-VM | Select Name, @{N="OS";E={$_.Guest.OSFullName}}

    To display the Guest OS information, VMware Tools need to be installed on your guests.

    Retrieving installed software from your guests is a bit harder. You have to retrieve this information from inside your guest OS.

    For Windows you could use WMI.

    To run a script inside a VM, use the Invoke-VMScript cmdlet.



  • 6.  RE: Extract operating system and application information from a VM

    Posted Jan 19, 2011 01:57 PM

    Try the folllowing script, it uses the Invoke-VMScript cmdlet. Make sure you provide the correct credentials for the guest OS and for the ESX(i) server that hosts the guest.

    The routine to list the installed programs comes from Andrew Savinykh.

    $progList = {
        if (!([Diagnostics.Process]::GetCurrentProcess().Path -match '\\syswow64\\'))
        {
            $unistallPath = "\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\"
            $unistallWow6432Path = "\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\"        @(         if (Test-Path "HKLM:$unistallWow6432Path" ) { Get-ChildItem "HKLM:$unistallWow6432Path"}         if (Test-Path "HKLM:$unistallPath" ) { Get-ChildItem "HKLM:$unistallPath" }         if (Test-Path "HKCU:$unistallWow6432Path") { Get-ChildItem "HKCU:$unistallWow6432Path"}         if (Test-Path "HKCU:$unistallPath" ) { Get-ChildItem "HKCU:$unistallPath" }         ) |         ForEach-Object {Get-ItemProperty $_.PSPath } |         Where-Object {             $_.DisplayName -and
                !$_.SystemComponent -and
               
    !$_.ReleaseType -and
                !$_.ParentKeyName -and
                (
    $_.UninstallString -or $_.NoRemove) } |         Sort-Object DisplayName | %{$_.DisplayName}     }     else    {         "You are running 32-bit Powershell on 64-bit system. Please run 64 - bit Powershell instead." | Write-Host -ForegroundColor Red    } } foreach($vm in Get-VM){     $guest = Get-VMGuest -VM $vm
       
    $progs = Invoke-VMScript -VM $vm -ScriptText $progList -GuestCredential (Get-Credential -Credential guest) -HostCredential (Get-Credential -Credential host)     $progs.Split("`n") | Select @{N="VM";E={$vm.Name}},@{N="OS";E={$guest.OSFullName}},@{N="Program";E={$_}} }

    If you want to export the results to a CSV file you pipe the output to an Export-Csv cmdlet

    The last line tyhen becomes something like this

    ....
       
    $progs.Split("`n") | Select @{N="VM";E={$vm.Name}},                                 @{N="OS";E={$guest.OSFullName}},                                 @{N="Program";E={$_}} | Export-Csv "C:\report.csv" -NoTypeInformation
    }


  • 7.  RE: Extract operating system and application information from a VM

    Posted Jan 19, 2011 02:08 PM

    Just noticed that the Program values contain an annoying linefeed at the end.

    To suppress that use the following as the last line

        $progs.Split("`n") | Select @{N="VM";E={$vm.Name}},
                                    @{N="OS";E={$guest.OSFullName}},
                                    @{N="Program";E={$_.TrimEnd("`r")}} | 
                            Export-Csv "C:\report.csv" -NoTypeInformation}
    

    The TrimEnd function will get rid of the linefeed.



  • 8.  RE: Extract operating system and application information from a VM

    Posted Mar 28, 2024 08:45 PM

    I am a novice at PowerCLI, but I have set up an ESXi server, and I have done basic patches using an SSH terminal.

    I have been asked to provide a list of the applications and versions running on the ESXi server, so I am trying to run this script. I have saved the script as "C:\vSphere_Scripts\Get_VM_Apps.ps1" installed PowerCLI from the PowerShell Gallery, and I have connected to the server with connect-viserver and root credentials. I'm now trying to run the script above and I'm getting the following error. Can you help?

    At C:\vSphere_Scripts\Get_VM_Apps.ps1:5 char:100
    + ... \SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\" @(
    + ~~
    Unexpected token '@(' in expression or statement.
    At C:\vSphere_Scripts\Get_VM_Apps.ps1:31 char:1
    + }
    + ~
    Unexpected token '}' in expression or statement.
    + CategoryInfo : ParserError: (:) [], ParseException
    + FullyQualifiedErrorId : UnexpectedToken


  • 9.  RE: Extract operating system and application information from a VM

    Posted Mar 28, 2024 08:55 PM

    Seems a NL-CR was dropped in the code above.
    That should be 2 lines

            $unistallWow6432Path = "\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\"
            @(
    


  • 10.  RE: Extract operating system and application information from a VM

    Posted Mar 28, 2024 09:02 PM

    Thank you for the quick reply! After updating the script, I now get:

    At C:\vSphere_Scripts\Get_VM_Apps.ps1:32 char:1
    + }
    + ~
    Unexpected token '}' in expression or statement.
        + CategoryInfo          : ParserError: (:) [], ParseException
        + FullyQualifiedErrorId : UnexpectedToken

    I also tried just pasting the script into the command line, and although I did get errors, I got prompted for the guest and host credentials of each and every VM. I went through that, just clicking enter on the last 2, assuming it was going through the list of VMs in alphabetical order, because the last 2 VMs are Linux. After doing that, I did not get a report.csv file, I just got the following for each VM:


    WARNING: Parameter 'HostCredential' is obsolete. The HostCredential parameter of Invoke-VMScript cmdlet is deprecated
    and is ignored on VC version 4.0 onwards
    Invoke-VMScript : 3/28/2024 4:54:18 PM  Invoke-VMScript         Value cannot be found for the mandatory parameter ScriptText
    At line:3 char:14
    + ...    $progs = Invoke-VMScript -VM $vm -ScriptText $progList -GuestCrede ...
    +                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : NotSpecified: (:) [Invoke-VMScript], VimException
        + FullyQualifiedErrorId : Core_BaseCmdlet_UnknownError,VMware.VimAutomation.ViCore.Cmdlets.Commands.InvokeVmScript
    
    You cannot call a method on a null-valued expression.
    At line:4 char:5
    +     $progs.Split("`n") | Select @{N="VM";E={$vm.Name}},
    +     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
        + FullyQualifiedErrorId : InvokeMethodOnNull

     



  • 11.  RE: Extract operating system and application information from a VM

    Posted Mar 28, 2024 09:16 PM

    Pasting in the pieces separately - entering the $progList first, then the foreach section - I had some success. I got the following output in a report.csv for one VM. I was hoping to get 8 VMs. This seems about right for that one, except there's no app versions. I'm trying to figure out how to add that into the script. I would just need to run this for individual VMs one at a time.

     

     

    "VM","OS","Program"
    "FileStore","Microsoft Windows Server 2019 (64-bit)","FileZilla Server"
    "FileStore","Microsoft Windows Server 2019 (64-bit)","Microsoft Visual C++ 2015-2022 Redistributable (x64) - 14.36.32532"
    "FileStore","Microsoft Windows Server 2019 (64-bit)","Microsoft Visual C++ 2015-2022 Redistributable (x86) - 14.36.32532"
    "FileStore","Microsoft Windows Server 2019 (64-bit)","Notepad++ (32-bit x86)"
    "FileStore","Microsoft Windows Server 2019 (64-bit)","VMware Tools"
    "FileStore","Microsoft Windows Server 2019 (64-bit)",""

     

     

     



  • 12.  RE: Extract operating system and application information from a VM

    Posted Mar 28, 2024 09:42 PM

    That is a rather old thread, the HostCredential parameter is not used anymore.

    I don't have a line 32 in my snippet, not sure what the actual code is that you are running.

    Updated, the code looks something like this

    $progList = {
        if (!([Diagnostics.Process]::GetCurrentProcess().Path -match '\\syswow64\\'))
        {
            $unistallPath = "\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\"
            $unistallWow6432Path = "\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\"
            @(
            if (Test-Path "HKLM:$unistallWow6432Path" ) { Get-ChildItem "HKLM:$unistallWow6432Path"}
            if (Test-Path "HKLM:$unistallPath" ) { Get-ChildItem "HKLM:$unistallPath" }
            if (Test-Path "HKCU:$unistallWow6432Path") { Get-ChildItem "HKCU:$unistallWow6432Path"}
            if (Test-Path "HKCU:$unistallPath" ) { Get-ChildItem "HKCU:$unistallPath" }
            ) |
            ForEach-Object {Get-ItemProperty $_.PSPath } | 
            Where-Object {
                $_.DisplayName -and
                !$_.SystemComponent -and
                !$_.ReleaseType -and
                !$_.ParentKeyName -and
                ($_.UninstallString -or $_.NoRemove) } |
            Sort-Object DisplayName | %{$_.DisplayName}
        }
        else    {
            "You are running 32-bit Powershell on 64-bit system. Please run 64 - bit Powershell instead." | Write-Host -ForegroundColor Red    }
    }
    
    foreach($vm in Get-VM){
        $guest = Get-VMGuest -VM $vm
        $progs = Invoke-VMScript -VM $vm -ScriptText $progList -GuestCredential (Get-Credential -Credential guest)
        $progs.Split("`n") | Select @{N="VM";E={$vm.Name}},@{N="OS";E={$guest.OSFullName}},@{N="Program";E={$_}}
    }





  • 13.  RE: Extract operating system and application information from a VM

    Posted Mar 28, 2024 10:12 PM

    Thank you! Can you tell me how I would add DisplayVersion into this listing? I tried messing with the "Where-Object" section, and got nothing useful.



  • 14.  RE: Extract operating system and application information from a VM

    Posted Mar 29, 2024 12:16 PM

    You could do something like this

    $progList = {
        if (!([Diagnostics.Process]::GetCurrentProcess().Path -match '\\syswow64\\'))
        {
            $unistallPath = "\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\"
            $unistallWow6432Path = "\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\"
            @(
            if (Test-Path "HKLM:$unistallWow6432Path" ) { Get-ChildItem "HKLM:$unistallWow6432Path"}
            if (Test-Path "HKLM:$unistallPath" ) { Get-ChildItem "HKLM:$unistallPath" }
            if (Test-Path "HKCU:$unistallWow6432Path") { Get-ChildItem "HKCU:$unistallWow6432Path"}
            if (Test-Path "HKCU:$unistallPath" ) { Get-ChildItem "HKCU:$unistallPath" }
            ) |
            ForEach-Object {Get-ItemProperty $_.PSPath } |
            Where-Object {
                $_.DisplayName -and
                !$_.SystemComponent -and
                !$_.ReleaseType -and
                !$_.ParentKeyName -and
                ($_.UninstallString -or $_.NoRemove) } |
            Sort-Object DisplayName | %{"$($_.DisplayName)|$($_.DisplayVersion)"}
        }
        else    {
            "You are running 32-bit Powershell on 64-bit system. Please run 64 - bit Powershell instead." | Write-Host -ForegroundColor Red    }
    }
    
    foreach($vm in Get-VM){
        $guest = Get-VMGuest -VM $vm
        $progs = Invoke-VMScript -VM $vm -ScriptText $progList -GuestCredential (Get-Credential -Credential guest)
        $progs.Split("`n") | Select @{N="VM";E={$vm.Name}},
          @{N="OS";E={$guest.OSFullName}},
          @{N="Program";E={$_.Split('|')[0]}},
          @{N="Version";E={$_.Split('|')[1]}}
    }


  • 15.  RE: Extract operating system and application information from a VM

    Posted Mar 29, 2024 04:38 PM

    Thank you! This helped a lot. I got information for most of our Windows VMs. I will need to figure out something else for the Linux ones.