Hi,<\/p>\n
I have a script that I am using that I have no error checking.<\/p>\n
do
\n{
\n$ip = Read-Host ‘Enter a valid IP address in the form of 193.62.66.x’
\n} while ($ip -notmatch $ipV4)<\/p>\n
<##Testing<\/span> Variables##> #Function<\/span> to set IP Address\" #Set<\/span> new Configuration $validate = $true Write-Host “Please enter your desired computer name: [Default $env:computername]” Write-Host “If this computer object exists in AD, make sure the computer it is deleted or domain join will fail!” -ForegroundColor Yellow $renamecomputer = $true I would like to make sure that the hostname doesn’t exist and if it does, then delete it.<\/p>\n Also, I would like it to throw an error such as no password, wrong password:<\/p>\n $dompassword = Read-Host “Enter the domain administrator password for the host to join the domain” -AsSecureString<\/p>\n $credentials = New-Object System.Management.Automation.PsCredential(“domain\\administrator”, $dompassword ) What would be best way to error check the following:<\/p>\n Hi,<\/p>\n I have a script that I am using that I have no error checking.<\/p>\n do <##Testing<\/span> Variables##> #Function<\/span> to set IP Address\" #Set<\/span> new Configuration $validate = $true Write-Host “Please enter your desired computer name: [Default $env:computername]” Write-Host “If this computer object exists in AD, make sure the computer it is deleted or domain join will fail!” -ForegroundColor Yellow $renamecomputer = $true I would like to make sure that the hostname doesn’t exist and if it does, then delete it.<\/p>\n Also, I would like it to throw an error such as no password, wrong password:<\/p>\n $dompassword = Read-Host “Enter the domain administrator password for the host to join the domain” -AsSecureString<\/p>\n $credentials = New-Object System.Management.Automation.PsCredential(“domain\\administrator”, $dompassword ) What would be best way to error check the following:<\/p>\n Welcome<\/p>\n If you post code, please use the ‘Insert Code’ button. Please and thank you!<\/p>\n
\n[string]$IPAddress = $ip
\n[string]$Subnet = “255.255.255.0”
\n[string]$Gateway = “”
\n[string]$DNS1 = “”
\n[string]$DNS2 = “”<\/p>\n
\nfunction Set-StaticIPAddress ($IPAddress, $Subnet, $Gateway, $DNS1, $DNS2) {
\n$NewNetworkConfig = Get-WmiObject Win32_NetworkAdapterConfiguration -Filter “IpEnabled = ‘True’”
\n$NewNetworkConfig.EnableStatic($IPAddress, $Subnet)
\n$NewNetworkConfig.SetGateways($Gateway, 1)
\n$NewNetworkConfig.SetDNSServerSearchOrder(@($DNS1, $DNS2))
\n}<\/p>\n
\nSet-StaticIPAddress $IPAddress $Subnet $Gateway $DNS1 $DNS2
\n#Sleep<\/span> to take wait effect
\nsleep -Seconds 10
\n#Test<\/span> Connection
\nIf (Test-Connection -ComputerName $env:COMPUTERNAME -Count 10) {
\n#If<\/span> connection succeded…
\nWrite-Host “IP address changed!” -ForegroundColor Green
\n#Set<\/span> Task seqeunce Variable to False
\n#$TSENV.Value(“NetworkConfig”) = “Static”
\n#Exit<\/span> 0
\n}
\nElse {
\n#If<\/span> connection failed…
\nWrite-Host “Configuration Failed! Setting back to DHCP!” -ForegroundColor Yellow
\n#Set<\/span> Task seqeunce Variable to False
\n#$TSENV.Value(“NetworkConfig”) = “DHCP”
\n#Get<\/span> current IP settings swap to DHCP
\n$CurrentNetworkConfig = Get-WmiObject Win32_NetworkAdapterConfiguration -Filter “IPEnabled = ‘True’”
\n#Set<\/span> DNS Servers check switch
\nIf ($RevDNSAuto -ne $true) {
\n#Set<\/span> DNS to specified variable values
\n$CurrentNetworkConfig.SetDNSServerSearchOrder(@($REVDNS1,$REVDNS2))
\n}
\nElse {
\n#Set<\/span> DNS to Auto
\n$CurrentNetworkConfig.SetDNSServerSearchOrder()
\n}
\n#Enable<\/span> DHCP
\n$CurrentNetworkConfig.EnableDHCP()
\n#Wait<\/span> for configuration to complete
\nSleep -Seconds 10
\n#Exit<\/span> 0
\n}<\/p>\n
\n}<\/p>\n
\n$computername = Read-Host<\/p>\n
\nSleep -Seconds 2<\/p>\n
\nif ($computername -eq “” -or $computername -eq $env:computername) { $computername = $env:computername; $renamecomputer = $false }<\/p>\n
\nWrite-Host “Adding $computername to the domain”
\nAdd-Computer -DomainName “” -Credential $credentials -OUPath $ou
\nif ($renamecomputer -eq $true) { Rename-Computer -NewName $computername -DomainCredential $credentials -Force }<\/p>\n\n
\n{
\n$ip = Read-Host ‘Enter a valid IP address in the form of 193.62.66.x’
\n} while ($ip -notmatch $ipV4)<\/p>\n
\n[string]$IPAddress = $ip
\n[string]$Subnet = “255.255.255.0”
\n[string]$Gateway = “”
\n[string]$DNS1 = “”
\n[string]$DNS2 = “”<\/p>\n
\nfunction Set-StaticIPAddress ($IPAddress, $Subnet, $Gateway, $DNS1, $DNS2) {
\n$NewNetworkConfig = Get-WmiObject Win32_NetworkAdapterConfiguration -Filter “IpEnabled = ‘True’”
\n$NewNetworkConfig.EnableStatic($IPAddress, $Subnet)
\n$NewNetworkConfig.SetGateways($Gateway, 1)
\n$NewNetworkConfig.SetDNSServerSearchOrder(@($DNS1, $DNS2))
\n}<\/p>\n
\nSet-StaticIPAddress $IPAddress $Subnet $Gateway $DNS1 $DNS2
\n#Sleep<\/span> to take wait effect
\nsleep -Seconds 10
\n#Test<\/span> Connection
\nIf (Test-Connection -ComputerName $env:COMPUTERNAME -Count 10) {
\n#If<\/span> connection succeded…
\nWrite-Host “IP address changed!” -ForegroundColor Green
\n#Set<\/span> Task seqeunce Variable to False
\n#$TSENV.Value(“NetworkConfig”) = “Static”
\n#Exit<\/span> 0
\n}
\nElse {
\n#If<\/span> connection failed…
\nWrite-Host “Configuration Failed! Setting back to DHCP!” -ForegroundColor Yellow
\n#Set<\/span> Task seqeunce Variable to False
\n#$TSENV.Value(“NetworkConfig”) = “DHCP”
\n#Get<\/span> current IP settings swap to DHCP
\n$CurrentNetworkConfig = Get-WmiObject Win32_NetworkAdapterConfiguration -Filter “IPEnabled = ‘True’”
\n#Set<\/span> DNS Servers check switch
\nIf ($RevDNSAuto -ne $true) {
\n#Set<\/span> DNS to specified variable values
\n$CurrentNetworkConfig.SetDNSServerSearchOrder(@($REVDNS1,$REVDNS2))
\n}
\nElse {
\n#Set<\/span> DNS to Auto
\n$CurrentNetworkConfig.SetDNSServerSearchOrder()
\n}
\n#Enable<\/span> DHCP
\n$CurrentNetworkConfig.EnableDHCP()
\n#Wait<\/span> for configuration to complete
\nSleep -Seconds 10
\n#Exit<\/span> 0
\n}<\/p>\n
\n}<\/p>\n
\n$computername = Read-Host<\/p>\n
\nSleep -Seconds 2<\/p>\n
\nif ($computername -eq “” -or $computername -eq $env:computername) { $computername = $env:computername; $renamecomputer = $false }<\/p>\n
\nWrite-Host “Adding $computername to the domain”
\nAdd-Computer -DomainName “” -Credential $credentials -OUPath $ou
\nif ($renamecomputer -eq $true) { Rename-Computer -NewName $computername -DomainCredential $credentials -Force }<\/p>\n\n