Update Win_AD_Join_Computer.ps1
Parameters, error checking with exit codes
.NOTES
Change Log
V1.0 Initial release
+ V1.1 Parameterization; Error Checking with conditionals and exit codes
Reference Links: www.google.com
#>
-$domain = "myDomain"
-$password = "myPassword!" | ConvertTo-SecureString -asPlainText -Force
-$username = "$domain\myUserAccount"
-$credential = New-Object System.Management.Automation.PSCredential($username, $password)
-Add-Computer -DomainName $domain -OUPath "OU=testOU,DC=domain,DC=Domain,DC=com" -Credential $credential -Restart
\ No newline at end of file
+param(
+ $domain,
+ $password,
+ $UserAccount,
+ $OUPath
+)
+
+$username = "$domain\$UserAccount"
+$credential = New-Object System.Management.Automation.PSCredential($username, $secpassword)
+
+if ([string]::IsNullOrEmpty($domain)) {
+ Write-Output "Domain must be defined. Use -domain <value> to pass it."
+ EXIT 1
+}
+
+if ([string]::IsNullOrEmpty($UserAccount)) {
+ Write-Output "User Account must be defined. Use -UserAccount <value> to pass it."
+ EXIT 1
+}
+
+if ([string]::IsNullOrEmpty($password)){
+ Write-Output "Password must be defined. Use -password <value> to pass it."
+ EXIT 1
+}
+
+else{
+ $password = ConvertTo-SecureString -string $password -asPlainText -Force
+}
+
+try{
+
+if ([string]::IsNullOrEmpty($OUPath){
+ Write-Output "OU Path is not defined. Computer object will be created in the default OU."
+ Add-Computer -DomainName $domain -Credential $credential -Restart
+ EXIT 0
+}
+
+else {
+ Add-Computer -DomainName $domain -OUPath $OUPath -Credential $credential -Restart
+ EXIT 0
+}
+}
+
+catch{
+ Write-Output "An error has occured."
+ EXIT 1
+}
+
+Exit $LASTEXITCODE
\ No newline at end of file
