# 检查并以管理员身份运行
$currentWi = [Security.Principal.WindowsIdentity]::GetCurrent()
$currentWp = [Security.Principal.WindowsPrincipal]$currentWi
if( -not $currentWp.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator))
{
$boundPara = ($MyInvocation.BoundParameters.Keys | foreach{'-{0} {1}' -f $_ ,$MyInvocation.BoundParameters[$_]} ) -join ' '
$currentFile = $MyInvocation.MyCommand.Definition
$fullPara = $boundPara + ' ' + $args -join ' '
Start-Process "$psHome\powershell.exe" -ArgumentList "$currentFile $fullPara" -verb runas
return
}
#access .NET Framework
[System.Reflection.Assembly]::LoadWithPartialName('Microsoft.VisualBasic') | Out-Null
# config Computer Name
$pcname1=[Microsoft.VisualBasic.Interaction]::InputBox("Please enter a computer name. Standard names can contain letters (a-z, a-z), Numbers (0-9), and hyphen (-).", "Computer Name", "$env:computername")
$domain = "域名"
$username = "授权加域账号"
$password = "授权加域账号密码" | ConvertTo-SecureString -AsPlainText -Force
$Credential = New-Object System.Management.Automation.PSCredential($username,$password)
Add-Computer -DomainName $domain -Credential $credential -NewName $pcname1
Read-Host "The computer will restart in 5..."
shutdown /r /t 5
# end of configure Computer Name
另存为ps1格式 实现修改计算机名称并加域的功能!