# encoding: ascii
# api: powershell
# title: Advanced1_2011.ps1
# description: posh entry
# version: 0.1
# type: script
# author: Marc Carter
# license: CC0
# function: get-ModuleInfo
# x-poshcode-id: 2598
# x-archived: 2011-04-10T07:06:04
# x-published: 2011-04-04T19:56:00
#
#
#--------------------------------------------------------
# Script  : Advanced1_2011.ps1
# Author  : marc carter (marcadamcarter)
# Date    : 4/4/2011 21:14
# Keywords: PowerShell, Scripting Games
# Comments: 
#--------------------------------------------------------

                                                                     
                                                                     
                                                                     
                                             
function get-ModuleInfo { 
<#   
.SYNOPSIS   
    Retrieves module information for installed components.
.DESCRIPTION 
    Retrieves module information for installed components.
.PARAMETER server
    Name of the system you wish to query.
.PARAMETER UseDefaultCredentials 
    Use the currently authenticated user's credentials   
.NOTES   
    Name: get-ModuleInfo
    Author: Marc Carter
    DateCreated: 4APR2011
.EXAMPLE   
    get-ModuleInfo "computer1"
     
Description 
------------ 
Returns Module properties for a given process. 
#>  
[cmdletbinding( 
    DefaultParameterSetName = 'server', 
    ConfirmImpact = 'low' 
)] 
Param( 
    [Parameter(
        Position = 0, 
        Mandatory = $True, 
        ParameterSetName = 'server', 
        ValueFromPipeline = $True,
        ValueFromPipelineByPropertyName = $True)] 
        [String][ValidatePattern(".{2,}")]$server
) 

    Begin{
        $psBoundParameters.GetEnumerator() | % {
            Write-Verbose "Parameter: $_"
        }
        $server = $server.toUpper()
    }

    Process{
        Write-Host `"Computer`"`,`"ModuleName`"`,`"Size`"`,`"FileName`"`,`"FileVersion`"
        if(($server -ne "localhost") -and ($server -ne $env:computername)){
            Invoke-Command -ComputerName $server -ScriptBlock { notepad }
            $proc = Get-Process -ComputerName $server
            $proc | Where-Object { $_.name -eq 'notepad' } | % {
                $_.Modules | Where-Object {$_.Description -eq "Windows Spooler Driver" } | % {
                    [string]$ModuleName = $_.ModuleName
                    [string]$Size = $_.Size
                    [string]$FileName = $_.FileName
                    [string]$FileVersion = $_.FileVersion
                    Write-Host `"$server`"`,`"$ModuleName`"`,`"$Size`"`,`"$FileName`"`,`"$FileVersion`"
                }
                Stop-Process -Id $_.Id
            }
        }
        else{
            Invoke-Command -ScriptBlock { notepad }
            $proc = Get-Process -ComputerName $server
            $proc | Where-Object { $_.name -eq 'TextPad' } | % {
                $_.Modules | Where-Object {$_.Description -eq "TextPad" } | % {
                    [string]$ModuleName = $_.ModuleName
                    [string]$Size = $_.Size
                    [string]$FileName = $_.FileName
                    [string]$FileVersion = $_.FileVersion
                    Write-Host `"$server`"`,`"$ModuleName`"`,`"$Size`"`,`"$FileName`"`,`"$FileVersion`"
                }
                Stop-Process -Id $_.Id
            }
        }
    }
    End { }
}#End Function get-moduleInfo

function get-listFromAD{
    $strFilter = "(objectCategory=Computer)"
    $props = "cn"
    $dse = [ADSI]"LDAP://rootdse"
    $root = "LDAP://"+$dse.defaultNamingContext
    $ds = New-Object DirectoryServices.DirectorySearcher([ADSI]$root,$strFilter,$props)
    $ds.PageSize = 1000
    $results = $ds.FindAll()
    foreach($obj in $results){
        $obj.cn
    }
}


if($args){ 
    foreach($i in $args){ 
        if($i -eq "ADquery"){ get-ModuleInfo -server get-listFromAD }
        else{ get-ModuleInfo -server $i }
    }
}
else{
    get-ModuleInfo
}
