GinShell.Windows/Public/Remove-GsDomain.ps1
|
function Remove-GsDomain { [CmdletBinding(SupportsShouldProcess)] param ( [Parameter(Mandatory = $true)] [string]$DomainUsername, [Parameter(Mandatory = $true)] [string]$Password, [Parameter()] [string]$WorkgroupName = "WORKGROUP", [Parameter()] [string]$NewComputerName = $env:COMPUTERNAME, [Parameter()] [switch]$Restart ) try { Write-GsLog -Message "Preparing to remove domain and join workgroup '$WorkgroupName'..." -Type Info $securePassword = ConvertTo-SecureString $Password -AsPlainText -Force $credential = New-Object System.Management.Automation.PSCredential ($DomainUsername, $securePassword) if ($PSCmdlet.ShouldProcess("This machine", "Unjoin domain and join $WorkgroupName")) { Remove-Computer -UnjoinDomainCredential $credential -WorkgroupName $WorkgroupName -PassThru -Verbose -Force -Restart:$false Write-GsLog -Message "Successfully removed from domain and joined workgroup '$WorkgroupName'." -Type Success if ($NewComputerName -ne $env:COMPUTERNAME) { Rename-Computer -NewName $NewComputerName -Force -Restart:$false Write-GsLog -Message "Computer renamed to '$NewComputerName'." -Type Success } if ($Restart) { Write-GsLog -Message "Restarting computer..." -Type Info Restart-Computer -Force } } } catch { Write-GsLog -Message "Failed to remove domain: $_" -Type Error throw } } |