0

I'm just a typical admin trying to make a simple script for some IT assistants in remote offices, to make domain joins easier while minimizing potential errors. The script's end game is to run the one-liner command Add-Computer -DomainName $DomainToJoin -OUPath $LocDN -NewName $WS_NewName -Restart.

But the whole script is supposed to include input validation for the computer's new name as well as for the target OU for the two-letter code for the remote office.

Googling for code snippets for days, esp. from sites like yours, was very helpful. But the problem I have now is I couldn't find the right codes to combine Read-Host , input length validation, and TRAP to work together without losing the value for my variables.

Pardon my coding as obviously I'm no real PS scripter, and I know the wrong portions of it are very basic. I would want to spend more time if I had the luxury, but I would really so much appreciate it if you could point me in the right direction.

Thank you so much in advance.

Please see my code below:

# START: Display name and purpose of invoked script
$path =  $MyInvocation.MyCommand.Definition 
Clear-Host
Write-Host $path
Write-Host " " 
Write-Host "This script will allow you to do the following in a single-step process:"
Write-Host "(1) RENAME this computer"
Write-Host "(2) JOIN it to MYDOMAIN"
Write-Host "(3) MOVE it to a target OU"
Write-Host "(4) REBOOT" 
Write-Host " "
Pause


# Function: PAUSE
Function Pause ($Message = "Press any key to continue . . . ") {
if ((Test-Path variable:psISE) -and $psISE) {
    $Shell = New-Object -ComObject "WScript.Shell"
    $Button = $Shell.Popup("Click OK to continue.", 0, "Script Paused", 0)
}
else {     
    Write-Host -NoNewline $Message
    [void][System.Console]::ReadKey($true)
    Write-Host
}
Write-Host " " 
}

# Function: Define the parameters
Function Define-Parameters {   

# Specify new computer name, with validation and TRAP
$WS_NewName = $null
while ($null -eq $WS_NewName) {
[ValidateLength(8,15)]$WS_NewName = [string](Read-Host -Prompt "NEW NAME of computer (8-15 chars.)" )
TRAP {"" ;continue} 
}
Write-Host " "

# Domain to join.
$DomainToJoin = 'mydomain.net' 

# Specify the target OU, with validation and trap
$baseOU='OU=Offices OU,DC=mydomain,DC=net'
$OU2 = $null
while ($null -eq $OU2) {
[ValidateLength(2,2)]$OU2 = [string](Read-Host -Prompt 'Target OU (TWO-LETTER code for your office)' )
TRAP {"" ;continue} 
}
Write-Host " "
$LocDN = "OU=$OU2,$baseOU"  
}



# Function: Summary and confirmation screen for defined parameters.
Function Confirm-Parameters {
Write-Host "==========================================================================="
Write-Host "Please confirm that you are joining this computer to 
$DomainToJoin (MYDOMAIN)"
Write-Host "with the following parameters:" 
Write-Host ""
Write-Host ""
Write-Host "Computer's NEW NAME:   $WS_NewName" 
# Write-Host "Domain to Join:      $DomainToJoin" 
Write-Host "TARGET mission OU:     $OU2" 
}


# Call Define-Parameters Function
Define-Parameters

# Call Confirm-Parameters Function
Confirm-Parameters

<#
Some more code here
#>


# FINAL COMMAND if all else works: Join the computer to the domain, rename it, and restart it. 
# Add-Computer -DomainName $DomainToJoin -OUPath $LocDN -NewName $WS_NewName -Restart
0

2 Answers 2

2

In your code, you have a lot of things defined very strangely. Your functions create a new scope and the variables you're trying to define therein will disappear after calling them unless you change the variable scope (in this case, to $script: or $global:). Also, to use functions, you need to define them first (your Pause doesn't actually do anything)

Here's something you can do with your Define-Parameters function (I suggest looking at Get-Verb)

# Domain to join.
$DomainToJoin = 'mydomain.net' 

# Function: Define the parameters
Function Get-Parameters {
    do {
        $global:WS_NewName = Read-Host -Prompt 'NEW NAME of computer (8-15 chars)'
    } until ($WS_NewName.Length -gt 7 -and $WS_NewName.Length -lt 16)
    ''

    do {
        $global:OU2 = Read-Host -Prompt 'Target OU (TWO-LETTER code for your office)'
    } until ($OU2 -match '^[a-z]{2}$')
    ''
    $OU2 = "OU=$global:OU2,OU=Offices OU,DC=mydomain,DC=net"
}

I'd strongly recommend moving away from the ISE to do your testing and test in an actual powershell console.

Sign up to request clarification or add additional context in comments.

Comments

0

Perhaps Try/Catch block instead of trap?

Try {
    [ValidatePattern('^\w{8,15}$')]$compname=read-host 'enter comp name' -ErrorAction Stop
    [ValidatePattern('^\w{2}$')]$OU=read-host 'enter OU name' -ErrorAction Stop
}
Catch {
$ErrorMessage = $_.Exception.Message
    $ErrorLineNumber = $_.InvocationInfo.ScriptLineNumber
    $ErrorCommandName = $_.InvocationInfo.InvocationName
    Write-Error -Message "The error message was: <$ErrorMessage>, script line: <$ErrorLineNumber>, command name: <$ErrorCommandName>"
    exit 255
}

1 Comment

The answer above is very interesting. I think I have an idea as to how the specific Catch block is supposed to work, based on the variable names being used, though I'm really not sure how the output should look like, as when I tried running the block on its own, PS just outputted its typical all-red error lines whenever an invalid input is entered, and then exits. I'm sure if I could get the code block to work as it is supposed to, it will be another good option. Thanks to both of you for the quick responses.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.