$name= read-host -Prompt "please Enter the ComputerName you want to use"
$domain=read-host -Prompt "please Enter the domain you want to add (enter test1 or test2)"
#host file path
$HostsLocation = "$env:windir\System32\drivers\etc\hosts"
if($domain.Equals("test1")){
    $fqdn =$name+".test1.com"
    #往host文件中加入一行
    Add-Content -Path $HostsLocation -Value "10.8.111.56   test1db"
    #修改dns的地址
    $InterfaceDNS = "10.8.150.20" 
    netsh interface ipv4 set dns name="Ethernet 2" source=static address=$InterfaceDNS register=primary
    netsh interface ipv4 set dns name="Local Area Connection 2" source=static address=$InterfaceDNS register=primary
    #加域的操作
    $domain = "test1.com"
    $password = "111111" | ConvertTo-SecureString -asPlainText -Force
    $username = "$domain\admin1" 
    $credential = New-Object System.Management.Automation.PSCredential($username,$password)
    Add-Computer -DomainName $domain -Credential $credential -ErrorAction Stop
}
if($domain.Equals("test2")){
    $fqdn =$name+".test2.com"
    #往host文件中加入一行
    Add-Content -Path $HostsLocation -Value "10.8.150.111   test2db"
    $InterfaceDNS = "10.8.150.59" 
    netsh interface ipv4 set dns name="Ethernet 2" source=static address=$InterfaceDNS register=primary
    netsh interface ipv4 set dns name="Local Area Connection 2" source=static address=$InterfaceDNS register=primary
    $domain = "test2.com"
    $password = "111111" | ConvertTo-SecureString -asPlainText -Force
    $username = "$domain\admin1" 
    $credential = New-Object System.Management.Automation.PSCredential($username,$password)
    Add-Computer -DomainName $domain -Credential $credential -ErrorAction Stop
}
#重命名机器
$computerName = Get-WmiObject Win32_ComputerSystem
$computername.Rename($name)

 

posted on 2013-04-07 09:02  一个土著  阅读(335)  评论(0)    收藏  举报