Skip to content

Commit

Permalink
v.0.1
Browse files Browse the repository at this point in the history
  • Loading branch information
exchange12rocks committed Feb 21, 2021
0 parents commit 67b2791
Show file tree
Hide file tree
Showing 42 changed files with 13,535 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
_Test/
15 changes: 15 additions & 0 deletions Common/Convert-GPONameToID.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
function Convert-GPONameToID {
Param (
[Parameter(Mandatory)]
[string]$Name
)

$Filter = '(&(objectClass=groupPolicyContainer)(displayName={0}))' -f $Name
$DomainDN = ([System.DirectoryServices.DirectoryEntry]::new('LDAP://RootDSE')).defaultNamingContext
$PoliciesCNLDAPPath = 'LDAP://CN=Policies,CN=System,{0}' -f $DomainDN[0] # $DomainDN is a collection of a System.DirectoryServices.ResultPropertyValueCollection type with a single element inside it
$PoliciesDSE = [System.DirectoryServices.DirectoryEntry]::new($PoliciesCNLDAPPath)

$Searcher = [System.DirectoryServices.DirectorySearcher]::new($PoliciesDSE, $Filter, 'cn', 'OneLevel')
$Result = $Searcher.FindOne() # $Result is also a collection of a System.DirectoryServices.ResultPropertyValueCollection type
[guid]$Result.Properties.cn[0]
}
27 changes: 27 additions & 0 deletions Common/Get-GPPSection.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
function Get-GPPSection {
Param (
[Parameter(ParameterSetName = 'ByName', Mandatory)]
[string]$GPOName,
[Parameter(ParameterSetName = 'ById', Mandatory)]
[guid]$GPOId,
[Parameter(ParameterSetName = 'ByName')]
[Parameter(ParameterSetName = 'ById')]
[GPPContext]$Context = $ModuleWideDefaultGPPContext,
[Parameter(ParameterSetName = 'ByName', Mandatory)]
[Parameter(ParameterSetName = 'ById', Mandatory)]
[GPPType]$Type
)

if (-not $GPOId) {
$GPOId = Convert-GPONameToID -Name $GPOName
}

$FilePath = Get-GPPSectionFilePath -GPOId $GPOId -Context $Context -Type $Type

$FilePathExistence = Test-Path -Path $FilePath
if ($FilePathExistence) {
[xml]$XmlDocument = Get-Content -Path $FilePath

Deserialize-GPPSection -InputObject $XmlDocument
}
}
16 changes: 16 additions & 0 deletions Common/Get-GPPSectionFilePath.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
function Get-GPPSectionFilePath {
Param (
[Parameter(Mandatory)]
[guid]$GPOId,
[Parameter(Mandatory)]
[GPPContext]$Context,
[Parameter(Mandatory)]
[GPPType]$Type
)

$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)
Join-Path -Path $ContextPath -ChildPath ('{0}\{0}.xml' -f $Type)
}
30 changes: 30 additions & 0 deletions Common/Set-GPPSection.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
function Set-GPPSection {
Param (
[Parameter(ParameterSetName = 'ByName', Mandatory)]
[Parameter(ParameterSetName = 'ById', Mandatory)]
[GPPSection]$InputObject,
[Parameter(ParameterSetName = 'ByName', Mandatory)]
[string]$GPOName,
[Parameter(ParameterSetName = 'ById', Mandatory)]
[guid]$GPOId,
[Parameter(ParameterSetName = 'ByName')]
[Parameter(ParameterSetName = 'ById')]
[GPPContext]$Context = $ModuleWideDefaultGPPContext,
[Parameter(ParameterSetName = 'ByName', Mandatory)]
[Parameter(ParameterSetName = 'ById', Mandatory)]
[GPPType]$Type
)

if (-not $GPOId) {
$GPOId = Convert-GPONameToID -Name $GPOName
}

$SectionDescription = Serialize-GPPSection -InputObject $InputObject -IncludeType

$XMLDocument = $SectionDescription.XMLDocument
$Type = $SectionDescription.Type

$FilePath = Get-GPPSectionFilePath -GPOId $GPOId -Context $Context -Type $Type

Set-Content -Path $FilePath -Value $XMLDocument.OuterXml
}
Loading

0 comments on commit 67b2791

Please sign in to comment.