Quantcast
Channel: The Official Scripting Guys Forum! forum
Viewing all articles
Browse latest Browse all 15028

Speed up the processing of my PowerShell script

$
0
0

How would I speed up the processing of my PowerShell script?

It pulls out information from a apps.csv file (containing 2 apps - a Windows XP and a Windows 7 version) and information from a hosts.csv file.

The app locations are tested across different hostnames (I can tell whether they're Windows XP/Windows 7 machines), to see if they are installed by checking for fingerprints.

At the moment it takes about 5-10 minutes to run 40+ records, really need to speed it up.

clear all

$Applications = @{}
$ComputerObjects = @()

# Gets OS
function Get-OS ([string]$hostname) {
    $os = "offline"

    gwmi win32_operatingsystem -computername $hostname -ea silentlycontinue | ForEach-Object {
        if($_.Version.ToString().StartsWith("6.1")) {
            $os = "Windows 7"
        } elseif ($_.Version.ToString().StartsWith("5.1")){
            $os = "Windows XP"
        } else {
            $os = "N/A"
        }
    } > $null

    return $os
}

# Sorts out the application information, breaking it into OS, LOB and then finally Location
Import-CSV C:\RDC\apps.csv | ForEach-Object {
    $appname = $_.appname.ToLower()
    $os = $_.os.ToLower()
    $lob = $_.lob.ToLower()
    $location = $_.location.ToLower()

    if ($Applications.Keys -notcontains $appname) {
        $WindowsOS=@{}

        # hashtable for windows xp and windows 7 applications
        $WindowsOS["windows xp"]=@{}
        $WindowsOS["windows 7"]=@{}
        $Applications[$appname]=$WindowsOS
    } 

    if ($Applications[$appname][$os].Keys -notcontains $lob) {
        $Applications[$appname][$os][$lob]=@()
    }

    if ($Applications[$appname][$os][$lob].Keys -notcontains $location) {
        $Applications[$appname][$os][$lob]+=$location
    }
}

# Sorts the Hostnames out and tests all Application Locations
Import-CSV C:\RDC\hosts.csv | ForEach-Object {
    $Properties = @{}
    $Properties["hostname"]=$_.hostname.ToLower()
    $Properties["lob"]=$_.type.ToLower()
    $Properties["os"]=Get-OS $Properties["hostname"]

    $Applications.Keys | ForEach-Object {
        $currAppName = $_
        $Properties[$currAppName]=$false;

        if ($Applications[$currAppName].Keys -contains $Properties["os"] -and $Applications[$currAppName][$Properties["os"]].Keys -contains $Properties["lob"]) {
            $Applications[$currAppName][$Properties["os"]][$Properties["lob"]] | ForEach-Object {
                $Properties[$currAppName]=$Properties[$currAppName] -or (Test-Path "\\$($Properties["hostname"])\$($_)")
            }
        }
    }

    $HostObject = New-Object PSObject -Property $Properties
    $ComputerObjects += $HostObject
}

$ComputerObjects | ft

$a = [int][double]::Parse((Get-Date -UFormat %s))

#$ComputerObjects | Export-csv "C:\Working\results_$a.csv" -NoTypeInformation


Viewing all articles
Browse latest Browse all 15028

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>