Jump to content

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
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
  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By DannyJ
      $sCommands1 = 'powershell.exe Get-ChildItem' $iPid = run($sCommands1   , @WorkingDir , @SW_SHOW , 0x2) $sOutput = ""  While 1     $sOutput &= StdoutRead($iPID)         If @error Then             ExitLoop         EndIf  WEnd ;~ msgbox(0, '' , $sOutput) ConsoleWrite("$sOutput") ConsoleWrite($sOutput) ConsoleWrite(@CRLF) $aOutput = stringsplit($sOutput ,@LF , 2) For $i=0 To  UBound($aOutput) - 1 Step 1     ConsoleWrite($aOutput[$i]) Next The script above reads the whole directory into a one dimensional array, but I need to work with the array, so I need to split the array into multiple dimensions.
      I have already read some forum answers here, and I have already tried these commands:
       
      Are there any way to use the $aOutput variable like in PowerShell:
      PowerShell:
      $a = Get-ChildItem $a.Mode I imagine this in AutoIt  $aOutput
      ConsoleWrite($aOutput[i].Mode) Or if I split this command into 2 dimension like:
      For $i To UBound($aOutput)-1 Step 1 ConsoleWrite($aOutput[$i][1]) ConsoleWrite($aOutput[$i][2]) Next  
    • By DannyJ
      If I try to run this script with   Get-ChildItem which means dir this script works perfectly, but If I try to run this command Get-RDUserSession, my script has the following error message:
      This command runs perfectly in PowerShell admin and I get back the values
      Get-RDUserSession -ConnectionBroker  broker.local | sort Username Or you can try this command as well
      Get-Command Get-RDUserSession If I run the above mentioned command this runs perfectly in PowerShell but not with AutoIt.
      Here is my script you can test the commands:
      #include<array.au3> $iPid = run('powershell Get-Command Get-RDUserSession'  , @WindowsDir , @SW_HIDE , 0x2) ;; This command not works in AutoIT you can test it in PowerShell but it won't work in Autoit ;$iPid = run('powershell Get-RDUserSession -ConnectionBroker  broker.local | sort Username'  , @WindowsDir , @SW_MAXIMIZE , 0x2) ; This command not works in AutoIT ;$iPid = run('powershell Get-ChildItem | sort Name'  , @WindowsDir , @SW_HIDE , 0x2) ; This runs perfectly $sOutput = ""  While 1     $sOutput &= StdoutRead($iPID)         If @error Then             ExitLoop         EndIf  WEnd ;~ msgbox(0, '' , $sOutput) $aOutput = stringsplit($sOutput , @LF , 2) _ArrayDisplay($aOutput)  
      That could be the solution of the problem if I could run, directly this PowerShell command window and Write to it and save it's values.
    • By DrLarch
      I'm trying to run this powershell command from Autoit and can't figure out how to pull it off:
      Get-ProvisionedAppxPackage -Online | Where-Object { $_.PackageName -match "xbox" } | ForEach-Object { Remove-ProvisionedAppxPackage -Online -AllUsers -PackageName $_.PackageName } I've been trying to run it many different ways including:
      $sCMD = 'Get-ProvisionedAppxPackage -Online | Where-Object { $_.PackageName -match "xbox" } | ForEach-Object { Remove-ProvisionedAppxPackage -Online -AllUsers -PackageName $_.PackageName }' RunWait(@comspec & ' /c powershell.exe -nologo -executionpolicy bypass -noprofile -Command "&' & $sCMD & '"') The problem is that it seems I'm missing something in how to escape or double the quotes. I've tried doubling the quotes in many different ways, but the end result always produces a syntax error in powershell. I could just run powershell first, then paste and run the command, then close the powershell window, but that's clunky. I'm trying to do it either via parameter (as above) or in one line like this:
      RunWait(@comspec & ' /c powershell.exe -nologo -executionpolicy bypass -noprofile -Command "&Get-ProvisionedAppxPackage -Online | Where-Object { $_.PackageName -match "xbox" } | ForEach-Object { Remove-ProvisionedAppxPackage -Online -AllUsers -PackageName $_.PackageName }"')  
    • By antonioj84
      any assistance how to incorporate this powershell command within autoit
       
      powershell.exe -nologo -executionpolicy bypass -WindowStyle hidden -noprofile -command "&Set-WinUserLanguageList -LanguageList fr-CA, en-CA -Force"
    • 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-10-05)
      None Things to come: (last changed: 2020-07-21)
      None
      BTW: If you like this UDF please click the "I like this" button. This tells me where to next put my development effort
×
×
  • Create New...