Jump to content
Sign in to follow this  
AdamUL

Using the PowerShell Group Policy Module with AutoIt

Recommended Posts

Below are some functions that I created when I needed to get some Group Policy information via script.  The quickest way I found to get this information was using the Group Policy Module for PowerShell.  This is not a full UDF for the Group Policy Module.  There are a lot more options available with the cmdlets, and this only touches on a few.  To use the functions below, for a Windows client, you will need to install Remote Server Administration Tools (RSAT).  For Windows Server 2012 or later, you will need to install the Group Policy Management Console.  

Remote Server Administration Tools (RSAT)

Installing the Group Policy Management Console via PowerShell for Windows Server 2012 or later.  Run in an Admin PowerShell prompt.

Install-WindowsFeature –Name GPMC

PowerShell Group Policy Module Documentation

For the example below, the AD UDF is only required to query GPO names, but is not required to use the functions.  

#include <AD.au3>
#include <WinAPIFiles.au3>

Global $sLDAPFilter = "(name=*)"
Global $sExcludeFilter = ""
Global $sIncludeFilter = ""
Global $sBaseDN = "DC=ad,DC=university,DC=edu"
Global $sDataToRetrieve = "sAMAccountName"

$sIncludeFilter = "(&(displayName=LIBS-*))" ;GPO Names.
$sLDAPFilter = "(&(objectClass=groupPolicyContainer)" & $sExcludeFilter & $sIncludeFilter & ")"

$sDataToRetrieve = "displayName,name"

_AD_Open()

Global $aGPONames = _AD_GetObjectsInOU($sBaseDN, $sLDAPFilter, 2, $sDataToRetrieve)
If @error Then
    MsgBox(64, "Active Directory Functions", "No objects found")
    Exit _AD_Close()
EndIf

_AD_Close()

;Add column headers to the output array.  
Global $aDataToRetrieve = StringSplit($sDataToRetrieve, ",", 2)
For $i = 0 To UBound($aGPONames, 2) - 1 Step 1
    $aGPONames[0][$i] = $aDataToRetrieve[$i]
Next

_ArraySort($aGPONames)
_ArrayDisplay($aGPONames, "LIBS GPOs") ;For Testing.

Global $aGPOPermissions = _AD_GetGPOPermissionsPS($aGPONames[1][0])
If @error Then Exit 1
_ArrayDisplay($aGPOPermissions, $aGPONames[1][0])


Global $sOU = "OU=libs,OU=active,DC=ad,DC=university,DC=edu"

Global $aGPOLinks =  _AD_GetGPOLinksPS($sOU)
If @error Then Exit 2
_ArrayDisplay($aGPOLinks, $sOU)

Global $aGPOInheritedLinks =  _AD_GetGPOInheritedLinksPS($sOU)
If @error Then Exit 2
_ArrayDisplay($aGPOInheritedLinks, $sOU)

Global $aGPOs = _AD_GetAllGPOsPS()
If @error Then Exit 3
_ArraySort($aGPOs)
_ArrayDisplay($aGPOs, "GPOs")

Global $aGPOName = _AD_GetGPOByNamePS($aGPONames[1][0])
If @error Then Exit 4
_ArrayDisplay($aGPOName, $aGPONames[1][0])

Global $aGPOGuid = _AD_GetGPOByGuidPS($aGPONames[1][1])
If @error Then Exit 5
_ArrayDisplay($aGPOGuid, $aGPONames[1][1])

Global $sReportName = "C:\Users\adamul\Desktop\Group Policy Object (GPO) PowerShell\Reports\" & $aGPONames[1][0] & ".html"
_AD_GetGPOReportByNamePS($aGPONames[1][0], $sReportName)
If @error Then Exit 6

Global $sReportGUID = "C:\Users\adamul\Desktop\Group Policy Object (GPO) PowerShell\Reports\" & $aGPONames[1][1] & ".html"
_AD_GetGPOReportByGuidPS($aGPONames[1][1], $sReportGUID)
If @error Then Exit 6


Func _AD_GetGPOPermissionsPS($sGPOName) ;An array of permission level for one or more security principals on a specified GPO.
    
    Local $sGPOCmd = 'powershell "Import-Module GroupPolicy; Get-GPPermissions -Name ''' & $sGPOName & ''' -All"'
    ConsoleWrite($sGPOCmd & @CRLF) 

    ;Turn off redirection for a 32-bit script on 64-bit system.
    If @OSArch = "X64" And Not @AutoItX64 Then _WinAPI_Wow64EnableWow64FsRedirection(False)
    
    Local $iPIDGPOCmd = Run($sGPOCmd, @SystemDir, @SW_HIDE, $STDERR_MERGED)
    ProcessWaitClose($iPIDGPOCmd)
    
    ;Turn on redirection for a 32-bit script on 64-bit system.
    If @OSArch = "X64" And Not @AutoItX64 Then _WinAPI_Wow64EnableWow64FsRedirection(True)
    
    Local $sGPOCmdOutput = StringStripWS(StdoutRead($iPIDGPOCmd), 3)
;~  ConsoleWrite($sGPOCmdOutput & @CRLF & @CRLF) ;For testing.

    Local $iGPOCmdOutputSS = StringInStr($sGPOCmdOutput, @CRLF & @CRLF)
    If $iGPOCmdOutputSS = 0 Then Return SetError(1, 0, 0)
    
    Local $sGPOCmdOutputSS = StringMid($sGPOCmdOutput, 1, $iGPOCmdOutputSS)
;~  ConsoleWrite(@CRLF & @CRLF & $sGPOCmdOutputSS & @CRLF)
    
    Local $sRegEx = "([^:\r\n]*):.*"
    Local $aProperties = StringRegExp($sGPOCmdOutputSS, $sRegEx, 3)
;~  _ArrayDisplay($aProperties) ;For testing.
    
    If StringInStr($sGPOCmdOutput, "ArgumentException") Then Return SetError(2, 0, 0)
    
    ;Get data on multiple lines to a single line.
    $sGPOCmdOutput = StringRegExpReplace($sGPOCmdOutput, "(\r\n\h{2,})", "")
    
    Local $aGPOCmdOutput =  StringSplit($sGPOCmdOutput, @CRLF & @CRLF, 1)
;~  _ArrayDisplay($aGPOCmdOutput) ;For testing.
    
    ;Convert from a list output to a 2D array.
    Local $aGPOCmdOutput2D[$aGPOCmdOutput[0]][UBound($aProperties)]
    Local $aTemp
    For $i = 1 To $aGPOCmdOutput[0] Step 1
        $aTemp = StringSplit($aGPOCmdOutput[$i], @CRLF, 1)
        For $j = 1 To $aTemp[0] Step 1
            For $k = 0 To UBound($aProperties) - 1 Step 1
                If StringInStr($aTemp[$j], $aProperties[$k]) Then 
                    $aGPOCmdOutput2D[$i - 1][$k] = StringStripWS(StringReplace($aTemp[$j], $aProperties[$k] & ":", ""), 3)
                EndIf
            Next
        Next        
    Next
;~  _ArrayDisplay($aGPOCmdOutput2D) ;For testing.

    For $i = 0 To UBound($aProperties) - 1 Step 1
        $aProperties[$i] = StringStripWS($aProperties[$i], 3)
    Next
    _ArrayTranspose($aProperties)
    _ArrayConcatenate($aProperties, $aGPOCmdOutput2D)

    Return $aProperties
EndFunc   ;==>_AD_GetGPOPermissionsPS

Func _AD_GetGPOLinksPS($sOUName) ;An array of GPOs that are linked directly to the location.
    Local $sGPOCmd = 'powershell "Import-Module GroupPolicy; (Get-GPInheritance -Target ''' & $sOUName & "').GpoLinks"
    ConsoleWrite($sGPOCmd & @CRLF) 
    
    ;Turn off redirection for a 32-bit script on 64-bit system.
    If @OSArch = "X64" And Not @AutoItX64 Then _WinAPI_Wow64EnableWow64FsRedirection(False)
    
    Local $iPIDGPOCmd = Run($sGPOCmd, @SystemDir, @SW_HIDE, $STDERR_MERGED)
    ProcessWaitClose($iPIDGPOCmd)
    
    ;Turn on redirection for a 32-bit script on 64-bit system.
    If @OSArch = "X64" And Not @AutoItX64 Then _WinAPI_Wow64EnableWow64FsRedirection(True)
    
    Local $sGPOCmdOutput = StringStripWS(StdoutRead($iPIDGPOCmd), 3)
;~  ConsoleWrite($sGPOCmdOutput & @CRLF & @CRLF) ;For testing.

    Local $iGPOCmdOutputSS = StringInStr($sGPOCmdOutput, @CRLF & @CRLF)
    If $iGPOCmdOutputSS = 0 Then Return SetError(1, 0, 0)
    
    Local $sGPOCmdOutputSS = StringMid($sGPOCmdOutput, 1, $iGPOCmdOutputSS)
;~  ConsoleWrite(@CRLF & @CRLF & $sGPOCmdOutputSS & @CRLF) ;For testing.
    
    Local $sRegEx = "([^:\r\n]*):.*"
    Local $aProperties = StringRegExp($sGPOCmdOutputSS, $sRegEx, 3)
;~  _ArrayDisplay($aProperties) ;For testing.
    
    If StringInStr($sGPOCmdOutput, "ArgumentException") Then Return SetError(1, 0, 0)
    
    ;Get data on multiple lines to a single line.
    $sGPOCmdOutput = StringRegExpReplace($sGPOCmdOutput, "(\r\n\h{2,})", "")
    
    Local $aGPOCmdOutput =  StringSplit($sGPOCmdOutput, @CRLF & @CRLF, 1)
;~  _ArrayDisplay($aGPOCmdOutput) ;For testing.

    ;Convert from a list output to a 2D array.
    Local $aGPOCmdOutput2D[$aGPOCmdOutput[0]][UBound($aProperties)]
    Local $aTemp
    For $i = 1 To $aGPOCmdOutput[0] Step 1
        $aTemp = StringSplit($aGPOCmdOutput[$i], @CRLF, 1)
        For $j = 1 To $aTemp[0] Step 1
            For $k = 0 To UBound($aProperties) - 1 Step 1
                If StringInStr($aTemp[$j], $aProperties[$k]) Then 
                    $aGPOCmdOutput2D[$i - 1][$k] = StringStripWS(StringReplace($aTemp[$j], $aProperties[$k] & ":", ""), 3)
                EndIf
            Next
        Next        
    Next
;~  _ArrayDisplay($aGPOCmdOutput2D) ;For testing.

    For $i = 0 To UBound($aProperties) - 1 Step 1
        $aProperties[$i] = StringStripWS($aProperties[$i], 3)
    Next
    _ArrayTranspose($aProperties)
    _ArrayConcatenate($aProperties, $aGPOCmdOutput2D)

    Return $aProperties 
EndFunc   ;==>_AD_GetGPOLinksPS

Func _AD_GetGPOInheritedLinksPS($sOUName) ;An array of GPOs that are applied to the location when Group Policy is processed on a client.
    Local $sGPOCmd = 'powershell "Import-Module GroupPolicy; (Get-GPInheritance -Target ''' & $sOUName & "').InheritedGpoLinks"
    ConsoleWrite($sGPOCmd & @CRLF) 
    
    ;Turn off redirection for a 32-bit script on 64-bit system.
    If @OSArch = "X64" And Not @AutoItX64 Then _WinAPI_Wow64EnableWow64FsRedirection(False)
    
    Local $iPIDGPOCmd = Run($sGPOCmd, @SystemDir, @SW_HIDE, $STDERR_MERGED)
    ProcessWaitClose($iPIDGPOCmd)
    
    ;Turn on redirection for a 32-bit script on 64-bit system.
    If @OSArch = "X64" And Not @AutoItX64 Then _WinAPI_Wow64EnableWow64FsRedirection(True)
    
    Local $sGPOCmdOutput = StringStripWS(StdoutRead($iPIDGPOCmd), 3)
;~  ConsoleWrite($sGPOCmdOutput & @CRLF & @CRLF) ;For testing.

    Local $iGPOCmdOutputSS = StringInStr($sGPOCmdOutput, @CRLF & @CRLF)
    If $iGPOCmdOutputSS = 0 Then Return SetError(1, 0, 0)
    
    Local $sGPOCmdOutputSS = StringMid($sGPOCmdOutput, 1, $iGPOCmdOutputSS)
;~  ConsoleWrite(@CRLF & @CRLF & $sGPOCmdOutputSS & @CRLF) ;For testing.
    
    Local $sRegEx = "([^:\r\n]*):.*"
    Local $aProperties = StringRegExp($sGPOCmdOutputSS, $sRegEx, 3)
;~  _ArrayDisplay($aProperties) ;For testing.
    
    If StringInStr($sGPOCmdOutput, "ArgumentException") Then Return SetError(1, 0, 0)
    
    ;Get data on multiple lines to a single line.
    $sGPOCmdOutput = StringRegExpReplace($sGPOCmdOutput, "(\r\n\h{2,})", "")
    
    Local $aGPOCmdOutput =  StringSplit($sGPOCmdOutput, @CRLF & @CRLF, 1)
;~  _ArrayDisplay($aGPOCmdOutput) ;For testing.

    ;Convert from a list output to a 2D array.
    Local $aGPOCmdOutput2D[$aGPOCmdOutput[0]][UBound($aProperties)]
    Local $aTemp
    For $i = 1 To $aGPOCmdOutput[0] Step 1
        $aTemp = StringSplit($aGPOCmdOutput[$i], @CRLF, 1)
        For $j = 1 To $aTemp[0] Step 1
            For $k = 0 To UBound($aProperties) - 1 Step 1
                If StringInStr($aTemp[$j], $aProperties[$k]) Then 
                    $aGPOCmdOutput2D[$i - 1][$k] = StringStripWS(StringReplace($aTemp[$j], $aProperties[$k] & ":", ""), 3)
                EndIf
            Next            
        Next        
    Next
;~  _ArrayDisplay($aGPOCmdOutput2D) ;For testing.

    For $i = 0 To UBound($aProperties) - 1 Step 1
        $aProperties[$i] = StringStripWS($aProperties[$i], 3)
    Next
    _ArrayTranspose($aProperties)
    _ArrayConcatenate($aProperties, $aGPOCmdOutput2D)

    Return $aProperties     
EndFunc   ;==>_AD_GetGPOInheritedLinksPS

Func _AD_GetAllGPOsPS() ;An array of information on all the GPOs in a domain.
    Local $sGPOCmd = 'powershell "Import-Module GroupPolicy; Get-GPO -All"'
    ConsoleWrite($sGPOCmd & @CRLF) 
    
    ;Turn off redirection for a 32-bit script on 64-bit system.
    If @OSArch = "X64" And Not @AutoItX64 Then _WinAPI_Wow64EnableWow64FsRedirection(False)
    
    Local $iPIDGPOCmd = Run($sGPOCmd, @SystemDir, @SW_HIDE, $STDERR_MERGED)
    ProcessWaitClose($iPIDGPOCmd)
    
    ;Turn on redirection for a 32-bit script on 64-bit system.
    If @OSArch = "X64" And Not @AutoItX64 Then _WinAPI_Wow64EnableWow64FsRedirection(True)
    
    Local $sGPOCmdOutput = StringStripWS(StdoutRead($iPIDGPOCmd), 3)
;~  ConsoleWrite($sGPOCmdOutput & @CRLF & @CRLF) ;For testing.
    
    Local $iGPOCmdOutputSS = StringInStr($sGPOCmdOutput, @CRLF & @CRLF)
    If $iGPOCmdOutputSS = 0 Then Return SetError(1, 0, 0)
    
    Local $sGPOCmdOutputSS = StringMid($sGPOCmdOutput, 1, $iGPOCmdOutputSS)
;~  ConsoleWrite(@CRLF & @CRLF & $sGPOCmdOutputSS & @CRLF) ;For testing.
    
    Local $sRegEx = "([^:\r\n]*):.*"
    Local $aProperties = StringRegExp($sGPOCmdOutputSS, $sRegEx, 3)
;~  _ArrayDisplay($aProperties)
    
    If StringInStr($sGPOCmdOutput, "ArgumentException") Then Return SetError(1, 0, 0)
    
    ;Get data on multiple lines to a single line.
    $sGPOCmdOutput = StringRegExpReplace($sGPOCmdOutput, "(\r\n\h{2,})", "")
    
    Local $aGPOCmdOutput =  StringSplit($sGPOCmdOutput, @CRLF & @CRLF, 1)
;~  _ArrayDisplay($aGPOCmdOutput) ;For testing.

    ;Convert from a list output to a 2D array.
    Local $aGPOCmdOutput2D[$aGPOCmdOutput[0]][UBound($aProperties)]

    Local $aTemp
    For $i = 1 To $aGPOCmdOutput[0] Step 1
        $aTemp = StringSplit($aGPOCmdOutput[$i], @CRLF, 1)
        For $j = 1 To $aTemp[0] Step 1
            For $k = 0 To UBound($aProperties) - 1 Step 1
                If StringInStr($aTemp[$j], $aProperties[$k]) Then 
                    $aGPOCmdOutput2D[$i - 1][$k] = StringStripWS(StringReplace($aTemp[$j], $aProperties[$k] & ":", ""), 3)
                EndIf
            Next
        Next        
    Next
;~  _ArrayDisplay($aGPOCmdOutput2D) ;For testing.

    For $i = 0 To UBound($aProperties) - 1 Step 1
        $aProperties[$i] = StringStripWS($aProperties[$i], 3)
    Next
    _ArrayTranspose($aProperties)
    _ArrayConcatenate($aProperties, $aGPOCmdOutput2D)

    Return $aProperties
EndFunc   ;==>_AD_GetAllGPOsPS

Func _AD_GetGPOByNamePS($sGPOName) ;An array of information on one Group Policy Object (GPO) in a domain by Display Name.
    Local $sGPOCmd = 'powershell "Import-Module GroupPolicy; Get-GPO -Name ''' & $sGPOName & '''"'
    ConsoleWrite($sGPOCmd & @CRLF) 
    
    ;Turn off redirection for a 32-bit script on 64-bit system.
    If @OSArch = "X64" And Not @AutoItX64 Then _WinAPI_Wow64EnableWow64FsRedirection(False)
    
    Local $iPIDGPOCmd = Run($sGPOCmd, @SystemDir, @SW_HIDE, $STDERR_MERGED)
    ProcessWaitClose($iPIDGPOCmd)
    
    ;Turn on redirection for a 32-bit script on 64-bit system.
    If @OSArch = "X64" And Not @AutoItX64 Then _WinAPI_Wow64EnableWow64FsRedirection(True)

    Local $sGPOCmdOutput = StringStripWS(StdoutRead($iPIDGPOCmd), 3)
;~  ConsoleWrite($sGPOCmdOutput & @CRLF & @CRLF) ;For testing.
    
    ;Add end of line characters for single return group to be processed.
    $sGPOCmdOutput = $sGPOCmdOutput & @CRLF & @CRLF 
    
    Local $iGPOCmdOutputSS = StringInStr($sGPOCmdOutput, @CRLF & @CRLF)
    If $iGPOCmdOutputSS = 0 Then Return SetError(1, 0, 0)
    
    Local $sGPOCmdOutputSS = StringMid($sGPOCmdOutput, 1, $iGPOCmdOutputSS)
    ConsoleWrite(@CRLF & @CRLF & $sGPOCmdOutputSS & @CRLF)
    
    Local $sRegEx = "([^:\r\n]*):.*"
    Local $aProperties = StringRegExp($sGPOCmdOutputSS, $sRegEx, 3)
;~  _ArrayDisplay($aProperties) ;For testing.
    
    If StringInStr($sGPOCmdOutput, "ArgumentException") Then Return SetError(1, 0, 0)
    
    ;Get data on multiple lines to a single line.
    $sGPOCmdOutput = StringRegExpReplace($sGPOCmdOutput, "(\r\n\h{2,})", "")
    
    ;Remove last @CRLF to prevent blank row in return array. 
    $sGPOCmdOutput = StringTrimRight($sGPOCmdOutput, 2)
    
    Local $aGPOCmdOutput =  StringSplit($sGPOCmdOutput, @CRLF & @CRLF, 1)
;~  _ArrayDisplay($aGPOCmdOutput) ;For testing.

    ;Convert from a list output to a 2D array.
    Local $aGPOCmdOutput2D[$aGPOCmdOutput[0]][UBound($aProperties)]
;~  _ArrayDisplay($aGPOCmdOutput2D) ;For testing.

    Local $aTemp
    For $i = 1 To $aGPOCmdOutput[0] Step 1
        $aTemp = StringSplit($aGPOCmdOutput[$i], @CRLF, 1)
        For $j = 1 To $aTemp[0] Step 1
            For $k = 0 To UBound($aProperties) - 1 Step 1
                If StringInStr($aTemp[$j], $aProperties[$k]) Then 
                    $aGPOCmdOutput2D[$i - 1][$k] = StringStripWS(StringReplace($aTemp[$j], $aProperties[$k] & ":", ""), 3)
                EndIf
            Next
        Next        
    Next
;~  _ArrayDisplay($aGPOCmdOutput2D) ;For testing.

    For $i = 0 To UBound($aProperties) - 1 Step 1
        $aProperties[$i] = StringStripWS($aProperties[$i], 3)
    Next
    _ArrayTranspose($aProperties)
    _ArrayConcatenate($aProperties, $aGPOCmdOutput2D)

    Return $aProperties
EndFunc   ;==>_AD_GetGPOByNamePS

Func _AD_GetGPOByGuidPS($sGPOGuid) ;An array of information on one Group Policy Object (GPO) in a domain by GUID.
    Local $sGPOCmd = 'powershell "Import-Module GroupPolicy; Get-GPO -Guid ''' & $sGPOGuid & '''"'
    ConsoleWrite($sGPOCmd & @CRLF) 
    
    ;Turn off redirection for a 32-bit script on 64-bit system.
    If @OSArch = "X64" And Not @AutoItX64 Then _WinAPI_Wow64EnableWow64FsRedirection(False)
    
    Local $iPIDGPOCmd = Run($sGPOCmd, @SystemDir, @SW_HIDE, $STDERR_MERGED)
    ProcessWaitClose($iPIDGPOCmd)
    
    ;Turn on redirection for a 32-bit script on 64-bit system.
    If @OSArch = "X64" And Not @AutoItX64 Then _WinAPI_Wow64EnableWow64FsRedirection(True)
    
    Local $sGPOCmdOutput = StringStripWS(StdoutRead($iPIDGPOCmd), 3)
;~  ConsoleWrite($sGPOCmdOutput & @CRLF & @CRLF) ;For testing.
    
    ;Add end of line characters for single return group to be processed.
    $sGPOCmdOutput = $sGPOCmdOutput & @CRLF & @CRLF 
    
    Local $iGPOCmdOutputSS = StringInStr($sGPOCmdOutput, @CRLF & @CRLF)
    If $iGPOCmdOutputSS = 0 Then Return SetError(1, 0, 0)
    
    Local $sGPOCmdOutputSS = StringMid($sGPOCmdOutput, 1, $iGPOCmdOutputSS)
;~  ConsoleWrite(@CRLF & @CRLF & $sGPOCmdOutputSS & @CRLF) ;For testing.
    
    Local $sRegEx = "([^:\r\n]*):.*"
    Local $aProperties = StringRegExp($sGPOCmdOutputSS, $sRegEx, 3)
;~  _ArrayDisplay($aProperties) ;For testing.
    
    If StringInStr($sGPOCmdOutput, "ArgumentException") Then Return SetError(1, 0, 0)
    
    ;Get data on multiple lines to a single line.
    $sGPOCmdOutput = StringRegExpReplace($sGPOCmdOutput, "(\r\n\h{2,})", "")
    
    ;Remove last @CRLF to prevent blank row in return array. 
    $sGPOCmdOutput = StringTrimRight($sGPOCmdOutput, 2)
    
    Local $aGPOCmdOutput =  StringSplit($sGPOCmdOutput, @CRLF & @CRLF, 1)
;~  _ArrayDisplay($aGPOCmdOutput) ;For testing.

    ;Convert from a list output to a 2D array.
    Local $aGPOCmdOutput2D[$aGPOCmdOutput[0]][UBound($aProperties)]
;~  _ArrayDisplay($aGPOCmdOutput2D)

    Local $aTemp
    For $i = 1 To $aGPOCmdOutput[0] Step 1
        $aTemp = StringSplit($aGPOCmdOutput[$i], @CRLF, 1)
        For $j = 1 To $aTemp[0] Step 1
            For $k = 0 To UBound($aProperties) - 1 Step 1
                If StringInStr($aTemp[$j], $aProperties[$k]) Then 
                    $aGPOCmdOutput2D[$i - 1][$k] = StringStripWS(StringReplace($aTemp[$j], $aProperties[$k] & ":", ""), 3)
                EndIf
            Next
        Next        
    Next
;~  _ArrayDisplay($aGPOCmdOutput2D) ;For testing.

    For $i = 0 To UBound($aProperties) - 1 Step 1
        $aProperties[$i] = StringStripWS($aProperties[$i], 3)
    Next
    _ArrayTranspose($aProperties)
    _ArrayConcatenate($aProperties, $aGPOCmdOutput2D)

    Return $aProperties
EndFunc   ;==>_AD_GetGPOByGuidPS

Func _AD_GetGPOReportByNamePS($sGPOName, $sReportFullPath, $sReportType = "HTML") ;Generates a report either in XML or HTML format for a specified GPO by name in a domain.
    
    Switch $sReportType
        Case "HTML", "XML"
        Case Else 
            Return SetError(1, 0, False)
    EndSwitch
    
    Local $sPath = StringRegExpReplace($sReportFullPath, "(^.*\\)(.*)", "$1")
;~  ConsoleWrite($sPath & @CRLF) ;For testing.
;~  
    If Not FileExists($sPath) Then Return SetError(2, 0, False)
        
    Local $sGPOCmd = 'powershell "Get-GPOReport -Name ''' & $sGPOName & ''' -ReportType ' & $sReportType & ' -Path ''' & $sReportFullPath & '''"'
    ConsoleWrite($sGPOCmd & @CRLF) 

    ;Turn off redirection for a 32-bit script on 64-bit system.
    If @OSArch = "X64" And Not @AutoItX64 Then _WinAPI_Wow64EnableWow64FsRedirection(False)
    
    Local $iPIDGPOCmd = Run($sGPOCmd, @SystemDir, @SW_HIDE, $STDERR_MERGED)
    ProcessWaitClose($iPIDGPOCmd)
    
    ;Turn on redirection for a 32-bit script on 64-bit system.
    If @OSArch = "X64" And Not @AutoItX64 Then _WinAPI_Wow64EnableWow64FsRedirection(True)
    
    Local $sGPOCmdOutput = StringStripWS(StdoutRead($iPIDGPOCmd), 3)
;~  ConsoleWrite($sGPOCmdOutput & @CRLF & @CRLF) ;For testing.

    If $sGPOCmdOutput <> "" Then SetError(3, 0, False)
    
    Return True 
EndFunc   ;==>_AD_GetGPOReportByNamePS

Func _AD_GetGPOReportByGuidPS($sGPOGuid, $sReportFullPath, $sReportType = "HTML") ;Generates a report either in XML or HTML format for a specified GPO by GUID in a domain.
    
    Switch $sReportType
        Case "HTML", "XML"
        Case Else 
            Return SetError(1, 0, False)
    EndSwitch
    
    Local $sPath = StringRegExpReplace($sReportFullPath, "(^.*\\)(.*)", "$1")
;~  ConsoleWrite($sPath & @CRLF) ;For testing.
;~  
    If Not FileExists($sPath) Then Return SetError(2, 0, False)
        
    Local $sGPOCmd = 'powershell "Get-GPOReport -GUID ''' & $sGPOGuid & ''' -ReportType ' & $sReportType & ' -Path ''' & $sReportFullPath & '''"'
    ConsoleWrite($sGPOCmd & @CRLF) 

    ;Turn off redirection for a 32-bit script on 64-bit system.
    If @OSArch = "X64" And Not @AutoItX64 Then _WinAPI_Wow64EnableWow64FsRedirection(False)
    
    Local $iPIDGPOCmd = Run($sGPOCmd, @SystemDir, @SW_HIDE, $STDERR_MERGED)
    ProcessWaitClose($iPIDGPOCmd)
    
    ;Turn on redirection for a 32-bit script on 64-bit system.
    If @OSArch = "X64" And Not @AutoItX64 Then _WinAPI_Wow64EnableWow64FsRedirection(True)
    
    Local $sGPOCmdOutput = StringStripWS(StdoutRead($iPIDGPOCmd), 3)
;~  ConsoleWrite($sGPOCmdOutput & @CRLF & @CRLF) ;For testing.

    If $sGPOCmdOutput <> "" Then SetError(3, 0, False)
    
    Return True 
EndFunc   ;==>_AD_GetGPOReportByGuidPS

 

Adam

 

Edited by AdamUL

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By water
      Extensive library to control and manipulate Microsoft Active Directory.

      Threads: Development - General Help & Support - Example Scripts - Wiki
      Previous downloads: 30467
       
      Known Bugs: (last changed: 2020-03-26)
      None Things to come: (last changed: 2019-08-19)
      None
      BTW: If you like this UDF please click the "I like this" button. This tells me where to next put my development effort
    • By Exit
      For my next project I would like to send files with "alternate data streams" by email in ZIP format.
      I can not use any external program like 7-Zip or WinRAR. (They would fit😥)
      Who knows how to create a ZIP file with "alternate data streams" included with the Powershell command "Compress-Archive"? 
      Here a test script:  (save as "ADSTester.cmd")
      @rem Try to create a zip file with alternate data streams (ADS) included @rem Housekeeping @cls @del ADSTester.zip >nul: @RD /S /Q Extracted >nul: @del ADSTester.txt >nul: @rem End of Housekeeping echo This is the ADSTester.txt file >ADSTester.txt echo This is the ADSTester.txt:Part1 file >ADSTester.txt:Part1 echo This is the ADSTester.txt:Part2 file >ADSTester.txt:Part2 dir /r ADSTester.txt @rem See the 3 files @rem **************************************************************** @rem **************************************************************** @rem Please alter the next lines to include the alternate data streams. powershell Compress-Archive -Path .\ADSTester.txt -Update -DestinationPath ADSTester.zip powershell Expand-Archive -Path ADSTester.zip -DestinationPath .\Extracted\ dir /r Extracted\ADSTester.txt @rem Only one file left :-( pause  
    • By ur
      Which Powershell command in the PowerCLI module for VMware ESX used to interact with UI apps?
       
      When I launch any exe/any exeutable using powercli on guest VM using powercli command.
      Invoke-VMScript, I am able to run them in the background but not in the foreground.
       
      i.e., UI apps are not launching but showing the background as running in the task manager.
       
      We need our UI Automation scripts to execute in the VM, but it is not working.
       
      We are able to do in virtualbox and hyper-v but not in vmware esx using powercli.
       
      Please suggest.
    • By ambad4u
      Hello and Good Day to All!
      I am trying to install .NET 3.5 on Windows 10 x64bit via autoit (via ShellExecuteWait + PowerShell).
      If I run this line, it will runs without issues:
      ShellExecuteWait('PowerShell.exe', '-executionpolicy Bypass -File "' & @ScriptDir & '\OJP83BU523.ps1' & '"') "OJP83BU523.ps1" contains: DISM /Online /Enable-Feature /FeatureName:NetFX3 /All /Source:D:\Sources\sxs /LimitAccess
      However, since I won't know in advance the drive letter of the "sources" folder, I created a script to generate a PowerShell Script to give a correct path for it.
      With the modified script below, PowerShell only blinks and nothing happens
      ShellExecuteWait('PowerShell.exe', '-executionpolicy Bypass -File "' & @ScriptDir & '\' & $filename & '"') or
      ShellExecuteWait('PowerShell.exe', '-executionpolicy Bypass -File "' & $filename & '"')  
      I wish I know the difference with "$filename" and "\OJP83BU523.ps1" usage, as for me, it should be the same.
      Attached is my entire autoit script.
      any help is appreciated!, many thanks in advance!
      test.au3
    • By JLogan3o13
      There are a number of posts on the forum regarding use of Selenium in AutoIt. I recently had a go at using the PowerShell Selenium module, and was amazed at how easy it is. Thought I would post an example here; if anyone is interested this could probably be incorporated into AutoIt code pretty easily. 
      Pre-Req - The true star of this script is the ChroPath extension, available for Edge, Chrome and FireFox. With it installed, you just click on the element, select Inspect, and then ChroPath generates the XPath to the element for you. Here is an example based on a simple form I created on one of my sites.
      $myForm = Start-SeChrome -StartURL "http://logancomputerser.com/Appointment.html" -Maximized $firstName = Find-SeElement -Driver $myForm -Timeout 30 -XPath "//input[@id='formElement_First']" $lastName = Find-SeElement -Driver $myForm -Timeout 30 -XPath "//input[@id='formElement_Last']" $address = Find-SeElement -Driver $myForm -Timeout 30 -XPath "//input[@id='formElement_Street1']" $city = Find-SeElement -Driver $myForm -Timeout 30 -XPath "//input[@id='formElement_City']" $zip = Find-SeElement -Driver $myForm -Timeout 30 -XPath "//input[@id='formElement_Zip']" $state = Find-SeElement -Driver $myForm -Timeout 30 -XPath "//select[@id='formElement_State']" $phoneDay = Find-SeElement -Driver $myForm -Timeout 30 -XPath "//input[@id='formElement_DaytimePhone']" $phoneNight = Find-SeElement -Driver $myForm -Timeout 30 -XPath "//input[@id='formElement_EveningPhone']" $email = Find-SeElement -Driver $myForm -Timeout 30 -XPath "//input[@id='formElement_liamE']" $user = Find-SeElement -Driver $myForm -Timeout 30 -XPath "//input[@id='formElement_48564']" $pw = Find-SeElement -Driver $myForm -Timeout 30 -XPath "//input[@id='formElement_f403c']" $submit = Find-SeElement -Driver $myForm -Timeout 30 -XPath "//input[@id='wstForm_Contact_Submit']" $reset = Find-SeElement -Driver $myForm -Timeout 30 -XPath "//input[@id='wstForm_Contact_Reset']" Send-SeKeys -Element $firstName -Keys "Joe" Send-SeKeys -Element $lastName -Keys "Blow" Send-SeKeys -Element $address -Keys "111 S. Main St." Send-SeKeys -Element $city -Keys "AnyCity" Send-SeKeys -Element $zip -Keys "90210" Send-SeKeys -Element $state -Keys "CA" Send-SeKeys -Element $phoneDay -Keys "555.867.5309" Send-SeKeys -Element $phoneNight -Keys "555.888.1212" Send-SeKeys -Element $email -Keys "1Adam12@gmail.com" Send-SeKeys -Element $user -Keys "JBlow" Send-SeKeys -Element $pw -Keys "MyPassword" Start-Sleep 1 Invoke-SeClick -Element $submit Stop-SeDriver -Driver $myForm  
      As mentioned, this is just another way to skin the cat, but I found it a pretty fast way to initiate some easy testing in Selenium, and have used it a couple of times in projects now, both straight through PowerShell and wrapped in AutoIt.
×
×
  • Create New...