ScanForSoftware.txt

(5 KB) Pobierz
# =============================================================================
# 
# NAME:ScanForSoftware.ps1
# 
# AUTHOR: Ed Wilson , microsoft
# DATE  : 11/10/2007
# 
# COMMENT: 
# Uses Params to allow modification of script at runtime
# Uses funHelp function to display help
# Uses funLine function to underline output
# On Windows Server 2008 the MSI Installer Provider must
# be installed ... This is not installed by default. On 
# Windows Vista, the MSI Installer Provider is installed by
# default.
#
# =============================================================================

param(
      $computer="localhost",
      $name,
      $vendor,
      [switch]$all,
      [switch]$whatif,
      [switch]$help,
      [switch]$examples,
      [switch]$min,
      [switch]$full
      ) #end param

# Begin Functions

function funHelp()
{
 $descriptionText= `
@"
 NAME: ScanForSoftware.ps1
 DESCRIPTION:
 Scans for the existence of a specific piece of software. 
 This can be a partial name. Locates software that has been
 installed by using MSI installer. It will also produce a
 list of all MSI installed packages on the machine.This script
 works on local or remote machines.

 PARAMETERS: 
 -computer computer upon which to run the command. Local host
  by default
 -name the name of the software to locate
 -vendor software created by specific vendor
 -all list all MSI installed software
 -whatif prototypes the command
 -help prints help description and parameters file
 -examples prints only help examples of syntax
 -full prints complete help information
 -min prints minimal help. Modifies -help

"@ #end descriptionText

$examplesText= `
@"

 SYNTAX:
 ScanForSoftware.ps1 

 Displays an error missing parameter, and calls help

 ScanForSoftware.ps1  -name excel,word

 Queries for installed software on a local computer that
 matches word and excel. Returns the product name of the 
 match

 ScanForSoftware.ps1  -vendor Microsoft

 Queries for installed software on a local computer that
 matches a vendor named Microsoft. Returns the product 
 names of the match

 ScanForSoftware.ps1  -computer "Berlin" -all

 Queries for installed software on a remote computer named Berlin 

 ScanForSoftware.ps1 -whatif -all
 
 Displays what if: Perform operation query all installed software
 on localhost

 ScanForSoftware.ps1 -help

 Prints the help topic for the script

 ScanForSoftware.ps1 -help -full

 Prints full help topic for the script

 ScanForSoftware.ps1 -help -examples

 Prints only the examples for the script

 ScanForSoftware.ps1 -examples

 Prints only the examples for the script
"@ #end examplesText

$remarks = `
"
REMARKS
     For more information, type: $($MyInvocation.ScriptName) -help -full
" #end remarks

  if($examples) { $examplesText ; $remarks ; exit }
  if($full)     { $descriptionText; $examplesText ; exit } 
  if($min)      { $descriptionText ; exit }
  $descriptionText; $remarks 
  exit
} #end funHelp function

function funline (
                  $strIN,
                  $char = "=",
                  $sColor = "Yellow",
                  $uColor = "darkYellow",
                  [switch]$help
                 )
{
 if($help)
  {
    $local:helpText = `
@"
     Funline accepts inputs: -strIN for input string and -char for seperator
     -sColor for the string color, and -uColor for the underline color. Only 
     the -strIn is required. The others have the following default values:
     -char: =, -sColor: Yellow, -uColor: darkYellow
     Example:
     funline -strIN "Hello world"
     funline -strIn "Morgen welt" -char "-" -sColor "blue" -uColor "yellow"
     funline -help
"@
   $local:helpText
   break
  } #end funline help
  
 $strLine= $char * $strIn.length
 Write-Host -ForegroundColor $sColor $strIN 
 Write-Host -ForegroundColor $uColor $strLine
} #end funLine function

Function funWhatIf()
{
 if($all)
  {
   "what if: Perform operation query all installed software on $computer"
  }
 if($name)
  {
   "what if: Perform operation query for $name on $computer"
  }
   exit
} #end funWhatIf

Function funCheckMSI()
{
 $msi=Get-WmiObject -Class __provider -filter "name like '%msi%'"
 $count = ($msi | measure-object).count
 if($count -eq 0 ) 
   { 
    "Unable to locate MSI installer provider. This feature may not
     be enabled."
    exit
   }
} #end funCheckMSI

Function funAllSoftware()
{
 Get-wmiobject -class win32_product -computername $computer |
 format-list -property name, version, vendor
 exit
} #end funAllSoftware

Function funSpecificSoftware()
{
 $products = Get-wmiobject -class win32_product -computername $computer 
 foreach($sname in $name)
  {
   foreach($product in $products)
    {
     if($product.name -match $sname)
      {
       "$($product.name) identified" 
      } #end if
    } #end foreach product
  } #end foreach name
 exit
} #end funSpecificSoftware

Function funVendor()
{
 $products = Get-wmiobject -class win32_product -computername $computer 
   foreach($product in $products)
    {
     if($product.vendor -match $vendor)
      {
       "$($product.name) identified from $vendor" 
      } #end if
    } #end foreach product
  
 exit
} #end funSpecificSoftware

# Entry Point

if($help)            { funhelp }
if($examples)        { funhelp }
if($full)            { funhelp }
if($whatif)          { funWhatIf }
if($all)             { funcheckMSI ; funAllSoftware }
if($name)            { funcheckMSI ; funSpecificSoftware }
if($vendor)        { funcheckMSI ; funVendor }
if(!$name -or !$all -or !$vendor) { funhelp }
Zgłoś jeśli naruszono regulamin