Public/Set-ComputerIdentification.ps1
<#
.SYNOPSIS Sets a computer and/or its domain/workgroup information. .DESCRIPTION Sets a computer and/or its domain/workgroup information. .NOTES This function is pulled directly from the real Microsoft Windows Admin Center PowerShell scripts use rights (according to Microsoft): We grant you a non-exclusive, royalty-free right to use, modify, reproduce, and distribute the scripts provided herein. ANY SCRIPTS PROVIDED BY MICROSOFT ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS OR A PARTICULAR PURPOSE. .ROLE Administrators .PARAMETER ComputerName This parameter is OPTIONAL. TODO .PARAMETER NewComputerName This parameter is OPTIONAL. TODO .PARAMETER Domain This parameter is OPTIONAL. TODO .PARAMETER NewDomain This parameter is OPTIONAL. TODO .PARAMETER Workgroup This parameter is OPTIONAL. TODO .PARAMETER UserName This parameter is OPTIONAL. TODO .PARAMETER Password This parameter is OPTIONAL. TODO .PARAMETER UserNameNew This parameter is OPTIONAL. TODO .PARAMETER PasswordNew This parameter is OPTIONAL. TODO .PARAMETER Restart This parameter is OPTIONAL. TODO .EXAMPLE # Open an elevated PowerShell Session, import the module, and - PS C:\Users\zeroadmin> Set-ComputerIdentification -ComputerName $env:ComputerName -NewComputerName "SQLServer01" #> function Set-ComputerIdentification { param( [Parameter(Mandatory = $False)] [string] $ComputerName = '', [Parameter(Mandatory = $False)] [string] $NewComputerName = '', [Parameter(Mandatory = $False)] [string] $Domain = '', [Parameter(Mandatory = $False)] [string] $NewDomain = '', [Parameter(Mandatory = $False)] [string] $Workgroup = '', [Parameter(Mandatory = $False)] [string] $UserName = '', [Parameter(Mandatory = $False)] [string] $Password = '', [Parameter(Mandatory = $False)] [string] $UserNameNew = '', [Parameter(Mandatory = $False)] [string] $PasswordNew = '', [Parameter(Mandatory = $False)] [switch] $Restart) function CreateDomainCred($username, $password) { $secureString = ConvertTo-SecureString $password -AsPlainText -Force $domainCreds = New-Object System.Management.Automation.PSCredential($username, $secureString) return $domainCreds } function UnjoinDomain($domain) { If ($domain) { $unjoinCreds = CreateDomainCred $UserName $Password Remove-Computer -UnjoinDomainCredential $unjoinCreds -PassThru -Force } } If ($NewDomain) { $newDomainCreds = $null If ($Domain) { UnjoinDomain $Domain $newDomainCreds = CreateDomainCred $UserNameNew $PasswordNew } else { $newDomainCreds = CreateDomainCred $UserName $Password } If ($NewComputerName) { Add-Computer -ComputerName $ComputerName -DomainName $NewDomain -Credential $newDomainCreds -Force -PassThru -NewName $NewComputerName -Restart:$Restart } Else { Add-Computer -ComputerName $ComputerName -DomainName $NewDomain -Credential $newDomainCreds -Force -PassThru -Restart:$Restart } } ElseIf ($Workgroup) { UnjoinDomain $Domain If ($NewComputerName) { Add-Computer -WorkGroupName $Workgroup -Force -PassThru -NewName $NewComputerName -Restart:$Restart } Else { Add-Computer -WorkGroupName $Workgroup -Force -PassThru -Restart:$Restart } } ElseIf ($NewComputerName) { If ($Domain) { $domainCreds = CreateDomainCred $UserName $Password Rename-Computer -NewName $NewComputerName -DomainCredential $domainCreds -Force -PassThru -Restart:$Restart } Else { Rename-Computer -NewName $NewComputerName -Force -PassThru -Restart:$Restart } } } |