none
Powershell Export Softwareliste RRS feed

  • Frage

  • Hallo Zusammen,

    ich bräuchte eure Hilfe!


    ich will mit einem Powershell command eine Liste aller Oracle Produkte herausfahren und das alles in einem String.

    Den command hab ich mal

    Get-WmiObject -Class Win32_Product -ComputerName . -Filter "Vendor='Oracle'"| Select Name,Version

    Output:

    Name                                                        Version
    ----                                                        -------
    Java 7 Update 51                                            7.0.510
    Java 7 Update 51 (64-bit)                                   7.0.510

    Leider ist das alles in Tabellen.

    der Output soll aber so aussehen

    Java 7 Update 51 ;Version 7.0.510

    mit Hilfe von einer for Schleife will ich mir das ausgeben damit es am schluss dann so aussieht.

    Java 7 Update 51 ;Version 7.0.510
    Java 7 Update 51 (64-bit) ;Version 7.0.510


    Das hab ich bis jetzt aber leider gibt er mir nur die erste Zeile aus.

    #Powershell cmd
    $cmdout = Get-WmiObject -Class Win32_Product -ComputerName . -Filter "Vendor='Oracle'"| Select Name,Version
    $update = $a1[0].Name + " ;Version " + $a1[0].Version
    #"Updates expired: "$update
    
    $array = @($update)
    for ($i=0; $i -lt $update.count; $i++) 
    {
        $array[$i]
    }
    


    Output

    Java 7 Update 51 ;Version 7.0.510

    Vielleicht kann mir wer helfen bzw vielleicht hat jemand ein anderes Skript

    Vielen Dank


    Montag, 3. März 2014 14:23

Antworten

  • Hallo,

    das kannst Du einfacher haben.

    (Get-WmiObject -Class Win32_Product -ComputerName . -Filter "Vendor='Oracle'"| Select Name,Version | ConvertTo-Csv -Delimiter ';' -NoTypeInformation) -replace '"', ''

    Hier eine Loesung an Hande deines Versuches

    $cmdout = Get-WmiObject -Class Win32_Product -ComputerName . -Filter "Vendor='Oracle'"| Select Name,Version
    foreach	($Packet in $cmdout) {
    	$Packet.Name + " ;Version " + $Packet.Version
    }

    Dein Code kann nicht funktionieren. Mit der Zeile $array = @($update) baust du ein Array mit einem Element, womit dessen Count 1 ist was erkalert das deine For Scheliefe auch nur 1x ausgefeuhrt wird, zudem verwendest Du die Varaibale $a1, die aber nirgens definiert ist bzw. befeuellt wird ...

    Beste Gruesse
    brima


    • Bearbeitet brima Montag, 3. März 2014 15:31
    • Als Antwort markiert Phil Reichl Montag, 3. März 2014 15:54
    Montag, 3. März 2014 15:19

Alle Antworten

  • Hallo,

    das kannst Du einfacher haben.

    (Get-WmiObject -Class Win32_Product -ComputerName . -Filter "Vendor='Oracle'"| Select Name,Version | ConvertTo-Csv -Delimiter ';' -NoTypeInformation) -replace '"', ''

    Hier eine Loesung an Hande deines Versuches

    $cmdout = Get-WmiObject -Class Win32_Product -ComputerName . -Filter "Vendor='Oracle'"| Select Name,Version
    foreach	($Packet in $cmdout) {
    	$Packet.Name + " ;Version " + $Packet.Version
    }

    Dein Code kann nicht funktionieren. Mit der Zeile $array = @($update) baust du ein Array mit einem Element, womit dessen Count 1 ist was erkalert das deine For Scheliefe auch nur 1x ausgefeuhrt wird, zudem verwendest Du die Varaibale $a1, die aber nirgens definiert ist bzw. befeuellt wird ...

    Beste Gruesse
    brima


    • Bearbeitet brima Montag, 3. März 2014 15:31
    • Als Antwort markiert Phil Reichl Montag, 3. März 2014 15:54
    Montag, 3. März 2014 15:19
  • Vielen Dank für die Hilfe

    Montag, 3. März 2014 15:55
  • Hallo Brima und Phil!

    Es wird nicht empfohlen Win32_Product für so etwas zu benutzen.
    1. ist Win32_Product sehr langsam da es nicht gut filtert
    2. Win32_Product verursacht bei jedem Aufruf einen Software Konsistenz-Check dieser macht Win32_Product ebenfalls sehr langsam und es kann Software geben die sich dann wieder neu Installieren / reparieren will! Das kann zu einem Stop des ganzen vorgangs führen!

    Siehe hier: http://blogs.technet.com/b/askds/archive/2012/04/19/how-to-not-use-win32-product-in-group-policy-filtering.aspx

    http://gregramsey.net/2012/02/20/win32_product-is-evil/

    Ich habe einen Funktion gepostet die die Software mit allen möglichen Attributen aus der Registry ausliest. Die ist auf den Seiten der deutschsprachigen PowerShell Community zu finden:

    http://www.powershell-group.eu/forums/topic/software-auslesen/#post-4383


    PowerShell Artikel, Buchtipps und kostenlose PowerShell Tutorials + E-Books
    auf der deutschsprachigen PowerShell Community

    Mein 21 Teiliger PowerShell Video Grundlehrgang
    Deutsche PowerShell Videos auf Youtube
    Folge mir auf:
    Twitter | Facebook | Google+

    Dienstag, 4. März 2014 06:38
  • Hallo Peter,

    das ist mir bekannt, ich loese es in meinen Scripten auch ueber die Registry, weil mir die Loesung Win32_Product zu langsam ist und auch so nicht unbedingt alle Produkte gefunden werden, und natuerlich auch wegen den anderen von dir genannten Problemen.

    Ich hatte auch uerblegt noch einen entsprechenden Hinweis zu posten habe es dann aber gelassen...

    Bin mir manchmal nicht sicher ob es sinnvoll ist ueber die Frage hinaus Infos zu liefern, hier wegen; mit der Registry soll man vorsichtig sein und mein Eindruck das er erst beginnt mit der Powershell...

    Beste Gruesse
    brima

    Dienstag, 4. März 2014 08:25
  • Hallo Zusammen,

    Vielen Dank für eure tollen Lösungen.

    Das Script funktioniert soweit!

    Eine Frage hab ich noch.

    Kann man in das Script noch iwie Returncodes einbauen?

    Ich würde gerne noch folgendes einfügen

    • Wenn Java vorhanden ist dann 0 (OK)
    • Wenn Java veraltet vorhanden ist dann 2 (Critical)
    • Wenn Java nich vorhanden ist 0 (OK)

    Ob die vorhandene Version drauf ist könnte man doch mit einen file checken oder? oder mit web scraping?

    THX 4 all


    • Bearbeitet Phil Reichl Dienstag, 4. März 2014 09:50 .
    Dienstag, 4. März 2014 08:51
  • @Brima Im grunde hast du recht, man sollte gerade Anfänger nicht mit zuvile Stoff überladen.

    Ich finde Punkt 2. von oben aber so gravierend, dass ich dann doch immer zu der etwas umständlicheren aber besseren Registry lösung rate.

    Nach dem Motto machs gleich richtig.

    Es ist sonnst sehr spassig später den Fehler zu suchen, wenn der Fall auftritt, das eine Software sich lustig neu installieren will, wenn man die Macke von Win32_Product noch nicht kennt.


    PowerShell Artikel, Buchtipps und kostenlose PowerShell Tutorials + E-Books
    auf der deutschsprachigen PowerShell Community

    Mein 21 Teiliger PowerShell Video Grundlehrgang
    Deutsche PowerShell Videos auf Youtube
    Folge mir auf:
    Twitter | Facebook | Google+

    Dienstag, 4. März 2014 09:09
  • Hallo Phil

    dafür kannst du Exit 0 oder Exit 2 oder Exit 3 ... benutzen!

    Function Get-Software { 
    <# 
    .Synopsis 
       Function to get the installed Software from a local or remote Windows System. 
     
    .DESCRIPTION 
       Function to get the installed Software from a local or remote Windows System. 
       This function offers Objects with informations similar to the Installed Software Window-view in the Windows Systemmanagemend. 
       This function will return information about each windows installer-based application out of the Registry 'Uninstall' key (32 and 64 Bit). 
       In order for a registry key to be opened remotely, both the server and client machines must be running the remote registry service, and have remote administration enabled. 
       If the remote registry service is not accessible then you can use this function with PowerShell remoting. 
     
       ADVICE: The WMI/CIM Win32_Product class is considered evil! 
     
           It is not recomende to use WMi/CIM and the class Win32_Product for this task! 
           Unfortunately, Win32_Product uses a provider DLL that validates the consistency of every installed MSI package on the computer. 
           If a Software installation is inconsistent the Sofware starts to try to repair themself with a reinstallation! 
           That makes it very, very slow and in case of a inconsistent Software the task will hang! 
            
           If you use the Computername Parameter of this Funktion please registry service of one Computer connect to (many) other Computers even If you use PowerShell Jobs with this Function. 
           This is very slow! 
           If you like to query many Computers use this Funktion with PowerShell remoting so that it is executed on the target systems! 
           See Invoke-Command Example in section Examples 
     
    .PARAMETER Computername 
        Specifies the target computer for the operation. 
        The local machine registry is opened if Computername is an empty String "". 
        Default is an empty String "". 
     
    .PARAMETER IncludeEmptyDisplaynames 
         Returns even Software Objects with empty Displaynames 
         Default is to NOT return Software Objects with empty Displaynames 
     
    .EXAMPLE 
       Get-Software 
        
       Retrieves the Installed Software from the locale System 
     
    .EXAMPLE 
       Get-Software -IncludeEmptyDisplaynames 
        
       Retrieves the Installed Software from the locale System include Software with empty displaynames 
     
    .EXAMPLE 
       Get-Software -ComputerName 'Server1' 
     
       Retrieves the Installed Software from the remote System 'Server1' out of the registry 
       In order for a registry key to be opened remotely, both the server and client machines must be running the remote registry service, and have remote administration enabled. 
     
    .EXAMPLE 
        Invoke-Command -ComputerName 'Server1','Server2','Server3' -ScriptBlock ${Function:Get-Software} 
     
    .INPUTS 
       You can pipe the ComputerName(s) as input to Get-InstalledSoftware 
     
    .OUTPUTS 
        PSObject with following NoteProperties: 
            ComputerName 
            AuthorizedCDFPrefix 
            Comments 
            Contact 
            DisplayVersion 
            HelpLink 
            HelpTelephone 
            InstallDate 
            InstallLocation 
            InstallSource 
            ModifyPath 
            Publisher 
            Readme 
            Size 
            EstimatedSize 
            UninstallString 
            URLInfoAbout 
            URLUpdateInfo 
            VersionMajor 
            VersionMinor 
            WindowsInstalle 
            Version 
            Language 
            DisplayName 
     
    .NOTES 
       Author: Peter Kriegel 
        
       Version 1.0.0. 03.Dezember.2013 Intial release 
       Version 2.0.0. 05.Jannuary.2014 
            Refactored to an advanced Function and to use the Subfunction in begin{} block. 
        Version: 2.0.2. 13.February.2014 
            Refactored to remove double code and to catch errors with Try{}Catch{} 
             
     
       13.January.2014 
       HTTP://www.Admin-Source.de 
    #> 
      
        [Cmdletbinding()] 
        Param( 
            [Parameter(Position=0,ValueFromPipeline=$True,ValueFromPipelineByPropertyName=$True,ValueFromRemainingArguments=$False)] 
            [ValidateNotNull()] 
            [String[]]$Computername = @(''), 
            [Switch]$IncludeEmptyDisplaynames 
        ) 
      
         Begin { 
      
             # sub function to convert the registry values to an Object 
             Function Convert-RegistryUninstallSubKeyToObject { 
                param ( 
                    [String]$Computername, 
                    [microsoft.win32.registrykey]$SubKey 
                ) 
     
                # create New Object with empty Properties 
                $obj = New-Object PSObject | Select-Object ComputerName,AuthorizedCDFPrefix,Comments,Contact,DisplayVersion,HelpLink,HelpTelephone,InstallDate,InstallLocation,InstallSource,ModifyPath,Publisher,Readme,Size,EstimatedSize,UninstallString,URLInfoAbout,URLUpdateInfo,VersionMajor,VersionMinor,WindowsInstaller,Version,Language,DisplayName 
      
                $obj.ComputerName = $Computername 
                $obj.AuthorizedCDFPrefix = $SubKey.GetValue('AuthorizedCDFPrefix') 
                $obj.Comments = $SubKey.GetValue('Comments') 
                $obj.Contact = $SubKey.GetValue('Contact') 
                $obj.DisplayVersion = $SubKey.GetValue('DisplayVersion') 
                $obj.HelpLink = $SubKey.GetValue('HelpLink') 
                $obj.HelpTelephone = $SubKey.GetValue('HelpTelephone') 
                $obj.InstallDate = $SubKey.GetValue('InstallDate') 
                $obj.InstallLocation = $SubKey.GetValue('InstallLocation') 
                $obj.InstallSource = $SubKey.GetValue('InstallSource') 
                $obj.ModifyPath = $SubKey.GetValue('ModifyPath') 
                $obj.Publisher = $SubKey.GetValue('Publisher') 
                $obj.Readme = $SubKey.GetValue('Readme') 
                $obj.Size = $SubKey.GetValue('Size') 
                $obj.EstimatedSize = $SubKey.GetValue('EstimatedSize') 
                $obj.UninstallString = $SubKey.GetValue('UninstallString') 
                $obj.URLInfoAbout = $SubKey.GetValue('URLInfoAbout') 
                $obj.URLUpdateInfo = $SubKey.GetValue('URLUpdateInfo') 
                $obj.VersionMajor = $SubKey.GetValue('VersionMajor') 
                $obj.VersionMinor = $SubKey.GetValue('VersionMinor') 
                $obj.WindowsInstaller = $SubKey.GetValue('WindowsInstaller') 
                $obj.Version = $SubKey.GetValue('Version') 
                $obj.Language = $SubKey.GetValue('Language') 
                $obj.DisplayName = $SubKey.GetValue('DisplayName') 
                 
                # return Object 
                $obj 
            } 
     
        } # end Begin block        
     
        Process {     
            foreach($pc in $Computername){ 
      
                $UninstallPathes = @("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall","SOFTWARE\\Wow6432node\\Microsoft\\Windows\\CurrentVersion\\Uninstall") 
                 
                ForEach($UninstallKey in $UninstallPathes) { 
                    #Create an instance of the Registry Object and open the HKLM base key 
                    Try { 
                        $reg=[microsoft.win32.registrykey]::OpenRemoteBaseKey('LocalMachine',$pc) 
                    } Catch { 
                        $_ 
                        Continue 
                    } 
      
                    #Drill down into the Uninstall key using the OpenSubKey Method 
                    $regkey=$reg.OpenSubKey($UninstallKey) 
     
                    If(-not $regkey) { 
                        Write-Error "Subkey not found in registry: HKLM:\\$UninstallKey `non Machine: $pc" 
                    } 
     
                    #Retrieve an array of string that contain all the subkey names 
                    $subkeys=$regkey.GetSubKeyNames() 
         
                    #Open each Subkey and use GetValue Method to return the required values for each 
                    foreach($key in $subkeys){ 
      
                        $thisKey=$UninstallKey+"\\"+$key 
      
                        $thisSubKey=$reg.OpenSubKey($thisKey) 
      
                        # prevent Objects with empty DisplayName 
                        if (-not $thisSubKey.getValue("DisplayName") -and (-not $IncludeEmptyDisplaynames)) { continue } 
      
                        # convert registry values to an Object 
                        Convert-RegistryUninstallSubKeyToObject -Computername $PC -SubKey $thisSubKey 
      
                    }  # End ForEach $key 
      
                    $reg.Close() 
                      
                } # End ForEach $UninstallKey 
            } # End ForEach $pc 
        } # end Process block 
     
        End { 
         
        } # end End block 
      
    } #end Function
    
    
    # wir gehen erstmal davon aus das kein Java vorhanden ist
    $Java = $False
    
    # Java suchen in der Registry
    $Java = Get-Software | Where-Object {$_.Publisher -like '*Oracle*' -and $_.DisplayName -like '*java*'} | Select-Object DisplayName,DisplayVersion 
    
    If ($Java) {
        # Java existiert return 0
        Exit 0
    } Else {
        # Java existiert NICHT return 1
        Exit 1  
    }


    PowerShell Artikel, Buchtipps und kostenlose PowerShell Tutorials + E-Books
    auf der deutschsprachigen PowerShell Community

    Mein 21 Teiliger PowerShell Video Grundlehrgang
    Deutsche PowerShell Videos auf Youtube
    Folge mir auf:
    Twitter | Facebook | Google+

    Dienstag, 4. März 2014 12:23