我正在编写一个脚本来自动配置我的虚拟机。我们的想法是,VM自动配置其netadapter并通过默认凭据加入域。它应该适用于2012r2,因此PoShv4正在使用中。
在使用Add-Computer
$vmName = (Get-ItemProperty –path 'HKLM:\SOFTWARE\Microsoft\Virtual Machine\Guest\Parameters').VirtualMachineName
$vmName = $vmName -replace '\w+\s+(.+)' , '$1'
$ip = $vmName -replace '\D+(\d+)' , '$1'
switch -Regex ($vmName) {
'dc' {$Ip = '10.18.0.{0}0' -f $ip}
'srv' {$Ip = '10.18.1.{0}0' -f $ip}
'CL' {$Ip = '10.18.2.{0}0' -f $ip}
}
#Configure IP-Settings
$netadapter = Get-NetAdapter -Name Ethernet
$netadapter | Set-NetIPInterface -DHCP Disabled | Out-Null
$netadapter | New-NetIPAddress -IPAddress $ip -PrefixLength 16 -DefaultGateway 10.18.0.10 | Out-Null
Set-DnsClientServerAddress -InterfaceAlias Ethernet -ServerAddresses 10.18.0.10 | Out-Null
#Create Credentials for Domainjoin
$username = 'domain\administrator'
$password = ConvertTo-SecureString 'Passw0rd' -AsPlainText -Force
$cred = new-object -typename System.Management.Automation.PSCredential -argumentlist $username, $password
Start-Sleep -Seconds 5
#Check if Domainjoin possible
if ($vmName -eq 'DC1')
{
Install-WindowsFeature AD-Domain-services -IncludeAllSubFeature -IncludeManagementTools
Rename-Computer $vmName -Restart
}
elseif ($vmName -match 'DC')
{
Install-WindowsFeature AD-Domain-services -IncludeAllSubFeature -IncludeManagementTools
-DomainCredential $cred -NewName $vmName -Restart
}
else
{
$null = [net.dns]::Resolve('domain.tld')
Add-Computer -DomainName 'domain.tld' -DomainCredential $cred -NewName $vmName -Restart -Server 'dc1.domain.tld'
}
Add-Computer
因以下错误而失败:
Add-Computer : Der angegebene Servername "dc1.domain.tld" kann nicht aufgelöst werden.
In C:\VMAutoConfig.ps1:42 Zeichen:5
+ Add-Computer -DomainName 'domain.tld' -DomainCredential $cred -NewName $ ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (dc1.domain.tld:String) [Add-Computer], InvalidOperationException
+ FullyQualifiedErrorId : AddressResolutionException,Microsoft.PowerShell.Commands.AddComputerCommand
有任何已知问题吗?
netadapter配置和Add-Computer
之间长达5秒的延迟时间根本无济于事。
如果我第二次运行脚本,那么它就可以正常运行。