Skip to content

Commit

Permalink
Get the computer domain instead of defaulting to user domain for cont…
Browse files Browse the repository at this point in the history
…ext.
  • Loading branch information
SamErde committed Jul 3, 2024
1 parent d14c9e5 commit cfa2192
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
7 changes: 5 additions & 2 deletions Active Directory/Rename-GPOsByCSV.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ function Rename-GPOsByCSV {
Write-Log "$StartTime `n"

Import-Module GroupPolicy

[string]$Domain = [System.DirectoryServices.ActiveDirectory.Domain]::GetComputerDomain().Name

} # end begin block

process {
Expand All @@ -90,14 +93,14 @@ function Rename-GPOsByCSV {
foreach ($gpo in $Batch) {
try {
# Get the old GPO and rename it:
$OldGpo = (Get-GPO -Name $gpo.OldName -ErrorAction Stop).DisplayName
$OldGpo = (Get-GPO -Name $gpo.OldName -Domain $Domain -ErrorAction Stop).DisplayName

# Check if -WhatIf parameter is specified
$Target = "$($gpo.OldName)"
$Operation = "Rename to '$($gpo.NewName)'"
if ($PSCmdlet.ShouldProcess($Target, $Operation)) {
# Rename the GPO and suppress the host output
Rename-GPO -Name $OldGpo -TargetName $gpo.NewName | Out-Null
Rename-GPO -Name $OldGpo -TargetName $gpo.NewName -Domain $Domain | Out-Null
Write-Log -LogText "$(Get-Date) [Success] Renamed GPO '$Target' to '$($gpo.NewName)'." -Output Both
} else {
Write-Log -LogText "$(Get-Date) [Skipped] $Target" -Output Both
Expand Down
5 changes: 4 additions & 1 deletion Active Directory/Rename-GroupsByCsv.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,17 @@ function Rename-GroupsByCsv {
}

Import-Module ActiveDirectory

[string]$Domain = [System.DirectoryServices.ActiveDirectory.Domain]::GetComputerDomain().Name

} # end begin block

process {
foreach ($group in $GroupsCsv) {
try {
$OldName = $group.GroupName
$NewName = $group.NewGroupName
Get-ADGroup $OldName | Set-ADGroup -SamAccountName $NewName -DisplayName $NewName -WhatIf
Get-ADGroup $OldName -Server $Domain | Set-ADGroup -SamAccountName $NewName -DisplayName $NewName -Server $Domain -WhatIf
Write-Log -LogText "$(Get-Date) [Renamed] `'$OldName`' renamed to `'$NewName`'." -Output Both
} catch {
Write-Log -LogText "$(Get-Date) [Failed] Failed to rename `'$OldName`'.`n$_" -Output Both
Expand Down

0 comments on commit cfa2192

Please sign in to comment.