1. 程式人生 > >powershell指令碼實現計算機加入域

powershell指令碼實現計算機加入域

Function Set-Domain 

    param 
    (
        [Parameter(Mandatory = $true, Position = 0)] [string] $DNS,
        [Parameter(Mandatory = $true, Position = 1)] [string] $Domain, 
        [Parameter(Mandatory = $true, Position = 2)] [string] $Password,
        [Parameter(Mandatory = $true, Position = 3)] [string] $UserName
    )
    
    $wmi = Get-WmiObject win32_networkadapterconfiguration -filter "ipenabled = 'true'"
    
    #設定DNS
    $wmi.SetDNSServerSearchOrder($DNS) >$null    
    
    #加入域
    $pwd = $Password | ConvertTo-SecureString -asPlainText -Force 
    $credential = New-Object System.Management.Automation.PSCredential($UserName,$pwd) 
    Add-Computer -DomainName $Domain -Credential $credential 

    write-host success!
    Restart-Computer -Force
}