Question on controller script

This topic contains 4 replies, has 3 voices, and was last updated by  Kevin Greer 2 weeks, 5 days ago.

  • Author
    Posts
  • #76247

    Kevin Greer
    Participant

    I have a function that configures a remote computer. It uses parameter validation for ComputerName, NewComputerName, Office, and Username.

    If a technician runs the function and the mandatory parameters prompts for input, they will be validated and an exception is thrown if a validation method returns false. Then the technician has to run the function again, and re-enter all of the parameters.

    I want to write a controller script to alleviate the hassle of re-entering parameters. The controller script would prompt for input for each parameter, then notify the user to re-enter if it is invalid. Once all parameters are valid the controller script passes them all to the function. The function will do its own validation which makes everything seem redundant, but at least the technician does not have to re-enter all of the values.

    In this case is a pre-validation controller script reasonable? The only thing that I don't like about this is validating input twice.

  • #76250

    Don Jones
    Keymaster

    It is. There's a reason to validate input both in the user interface (the controller) and in the business logic (the function). You could simply have the controller trap for exceptions and re-run the function, of course, but that'd still mean a lot of repeated data entry. The most elegant way might be to move the validation logic to another function, which could then be called by both the controller and the actual command – that way, you're only writing that code once, and using it in multiple places. "Test-XXXX" as a command name for that "validation logic," for example, would work.

  • #76259

    Simon B
    Participant

    or put the function in a form that way you can control some of the data being entered i.e. list boxes, dropdownboxes etc. and validate the rest of the data before the function is called. If there is any invalid data the user will only have to amend this and not re-enter it all again.

  • #76262

    Kevin Greer
    Participant

    @Don

    I would write a test function that uses read-host to request and validate the input. Once all of the input is valid, a custom object could be returned with the parameters I would pass to the computer setup script.

    Then I write the controller script to call the test function and pass the values returned to the computer setup function.

    I then remove parameter validation from the setup function. If I include the test function inside the computer setup function wouldn't the validation still run twice? I'm not sure how to prevent validation from running twice. If I remove the test function call from the Computer Setup function there could be validation issues if someone runs that function directly. I might be overlooking something simple here, but still a little confused. Thank you for your help!

    @Simon
    Good suggestion. I'm not quite there yet. I'm was tempted to jump ahead and look into a GUI form, That is where I would like to be headed, but I want to get text menus down first.

  • #76279

    Kevin Greer
    Participant

    Do you mean have a Test-Function for each parameter? Something like this?

    Parameter Test Functions

    Function Test-Username {
    
    param(
        [ValidateScript({try{get-aduser $_ -ErrorAction stop }catch{write-warning "warning - user does not exist"}})]
        [Parameter(Mandatory = $True)]
        [String]$username
    )
    
    Write-Output $username
    
    }
    
    Function Test-ComputerName {
    
    param(
        #
        #[ValidateScript({try{Test-Connection -ComputerName $_ -Count 1 -ErrorAction stop }catch{write-warning "warning - computer not reachable"}})]
        #
        [Parameter(Mandatory = $True)]
        [String]$computername
    )
    
    $return = $null
        try{
            $test = Test-Connection -ComputerName $computername -Count 1 -ErrorAction stop
            if ($test){ $return = $computername }
        }
        Catch{
            write-warning "Computername is not reachable.  Please try again"
    
        }
    
    Write-Output $return
    
    } # End Function
    

    Computer Setup Function

    Function Setup-Computer{
    
        param(
            [ValidateScript({Test-Username $_})]
            [parameter(Mandatory = $true)]
            [String]$username,
    
            [ValidateScript({Test-ComputerName $_})]
            [parameter(Mandatory = $true)]
            [String]$computername
        )
    
        Write-Output "The Username $username is valid"
        Write-Output "The ComputerName $computername is valid"
        Write-Output "Continue with setup functions...."
    
    } # End Function
    

    Controller Function

    Function Setup_Computer_Controller_Script {
    #Controller Script
    
    $username = $null
    do{
        
        write-host "Enter The User Name to setup" -ForegroundColor Green
        $username = Test-Username 
        if($username){
            Clear-Host
        }
    }
    While(!($username))
    
    clear
    
    $ComputerName = $null
    do{
    
        write-host "Enter The Computer Name to setup" -ForegroundColor Green
        $ComputerName = Test-ComputerName
        if($ComputerName){
            Clear-Host
        }
    }
    While(!($ComputerName))
    
    Setup-Computer -username $username -computername $ComputerName
    
    } # end function
    

You must be logged in to reply to this topic.