Forum Discussion

bala08yuvaraj's avatar
bala08yuvaraj
Copper Contributor
Oct 14, 2024

Powershell GPO

Need a gpo script to find the value of multiple policy settings

 

For example

Computer Configuration\Windows Settings\Security Settings\Local Policies\User Rights Assignment

Computer Configuration\Windows Settings\Security Settings\Local Policies\User Rights Assignment

Need to get the value of both policy in a single PS script

  • sdtslmn's avatar
    sdtslmn
    Brass Contributor

    bala08yuvaraj 

     

    # Define the policies you want to retrieve
    $policiesToRetrieve = @(
        "SeInteractiveLogonRight",  # Log on locally
        "SeDenyInteractiveLogonRight"  # Deny log on locally
    )
    
    # Temporary file path for secedit export
    $tempFilePath = "$env:TEMP\gpo_security_config.inf"
    
    # Run secedit to export security settings
    secedit /export /cfg $tempFilePath /quiet
    
    # Check if the file was created successfully
    if (Test-Path -Path $tempFilePath) {
        # Initialize a hashtable to store policy values
        $policyValues = @{}
    
        # Read and parse the INF file
        foreach ($line in Get-Content -Path $tempFilePath) {
            foreach ($policy in $policiesToRetrieve) {
                if ($line -match "^$policy\s*=\s*(.+)$") {
                    $policyValues[$policy] = $matches[1].Trim()
                }
            }
        }
    
        # Display the retrieved policy values
        foreach ($policy in $policyValues.Keys) {
            Write-Output "$policy: $($policyValues[$policy])"
        }
    
        # Cleanup - Remove the temporary file
        Remove-Item -Path $tempFilePath -Force
    } else {
        Write-Output "Failed to export security configuration."
    }

Resources