加域脚本:
$domain = "域"
$username = "有权限的域账户名"
$password = "域账户的密码" | ConvertTo-SecureString -asPlainText -Force
function JoinDomain
{
$computer = $env:COMPUTERNAME
$DomainName = [System.Net.NetworkInformation.IPGlobalProperties]::GetIPGlobalProperties().DomainName
if ($DomainName.Length -ne 0)
{
Write-Host "[$(Get-Date)] The $computer is already in the domain!"
break
}
else
{
Set-ExecutionPolicy RemoteSigned
$credential = New-Object System.Management.Automation.PSCredential($username,$password)
Add-Computer -ComputerName $computer -DomainName $domain -Credential $credential
Write-Host "[$(Get-Date)] $computer join the $domain ok!"
net localgroup administrators "DX\Domain Admins" /add
Write-Host "[$(Get-Date)] Add DX\Domain Admins to Administrators group"
net localgroup administrators "DX\IT OP" /add
Write-Host "[$(Get-Date)] Add DX\IT OP Administrators group"
}
}
JoinDomain
退域脚本:
$domain = "域"
Write-Host ""
function LevelDomain
{
$computer = $env:COMPUTERNAME
$DomainName = [System.Net.NetworkInformation.IPGlobalProperties]::GetIPGlobalProperties().DomainName
if ($DomainName.length -ne 0)
{
$username = "有权限的域账户"
$password = "域账户密码" | ConvertTo-SecureString -asPlainText -Force
$credential = New-Object System.Management.Automation.PSCredential($username,$password)
remove-computer -credential $credential -PassThru -Force
Write-Host "[$(Get-Date)] The $computer out of the domain: $domainName !"
}
else
{
Write-Host "[$(Get-Date)] The $computer not in the domain: $domain !"
break
}
}
LevelDomain
benmo
没有帐号? 立即注册