Advanced Windows PowerShell Scripting Video Training

Advanced Windows PowerShell Scripting Video Training
Advanced Windows PowerShell Scripting Video Training

Wednesday, February 20, 2013

List all the scripts my GPOs run.

This is an interesting question that I picked up from my moderator duties on PowerShell.com.  The question was how do I know what scripts are being run by my GPOs?  The function below will enumerate all of your GPOs and then let you know what scripts are being run.

 

 

 

Function Get-GPOScripts

{

    $GPOS = Get-GPO -all

    ForEach ($GPO in $GPOs)

    {

   

        $Obj = New-Object -TypeName PSOBject

        $Obj | Add-Member -MemberType NoteProperty -Name "GPO" -Value $GPO.Displayname

 

        [xml]$xml = Get-GPOReport -Name $GPO.DisplayName -ReportType xml

 

        $User = $xml.documentelement.user.extensiondata.extension.script.command

        $computer = $xml.documentelement.Computer.extensiondata.extension.script.command

 

        $Incriment = 1

        $UserScript = @()

        ForEach ($U in $User)

        {

            $US = New-Object -TypeName PSObject

            $Script = ($U.Split("\"))[-1]

            $US | Add-Member -MemberType NoteProperty -Name "Script" -Value $Script

            $UserScript += $US

        }

 

        $ComputerScript = @()

        ForEach ($C in $Computer)

        {

            $CS = New-Object -TypeName PSObject

            $Script = ($C.Split("\"))[-1]

            $CS | Add-Member -MemberType NoteProperty -Name "Script" -Value $Script

            $ComputerScript += $CS

        }

        $Obj | Add-Member -MemberType NoteProperty -Name "UserScript" -Value $UserScript

        $Obj | Add-Member -MemberType NoteProperty -Name "ComputerScript" -Value $ComputerScript

 

 

        Write-Output $Obj

    }

}

 

Run it like this:

image

 

Since this function outputs an object, we can continue using it in the PowerShell Pipeline if we want to.  In this case, we see that GPO Demo1 has some scripts.

 

image

To list them:

image

No comments: