Common/Get-GPPSectionFilePath.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
function Get-GPPSectionFilePath {
    Param (
        [Parameter(Mandatory)]
        [guid]$GPOId,
        [Parameter(Mandatory)]
        [GPPContext]$Context,
        [Parameter(Mandatory)]
        [GPPType]$Type,
        [switch]$Extended
    )

    $DomainName = (Get-CimInstance -ClassName 'Win32_ComputerSystem').Domain # Computer's domain name, not user's
    $PoliciesFolderPath = '\\{0}\SYSVOL\{0}\Policies' -f $DomainName
    $PolicyPath = Join-Path -Path $PoliciesFolderPath -ChildPath $GPOId.ToString('B') # B - 32 digits separated by hyphens, enclosed in braces: {00000000 - 0000 - 0000 - 0000 - 000000000000}
    $ContextPath = Join-Path -Path $PolicyPath -ChildPath ('{0}\Preferences' -f $Context)
    $FolderPath = Join-Path -Path $ContextPath -ChildPath $Type
    $FilePath = Join-Path -Path $FolderPath -ChildPath ('{0}.xml' -f $Type)

    if ($Extended) {
        @{
            FolderPath = $FolderPath
            FilePath = $FilePath
        }
    }
    else {
        $FilePath
    }
}