Gui development

Mr. Jones I have been purchased tool making. I purchased as you had suggested to build GUI, with Powershell Studio 2012. I have created a GUI with a name, SamAcccountName, Phone, and email. I am trying to connect the textbox input to the new-aduser cmdlet. I have read the three chapters in the tool making and still having problems in the event drivers passing the textbox content to the cmdlet. would you have some additional examples that show connecting the textbox to the new-aduser cmdlet. Thank you.

Well, I’m not sure what you mean by “connecting the text box” to the cmdlet. If you can elaborate, maybe I can offer a suggestion. Also, SAPIEN’s forums are often a very good place for GUI advice.

sorry Mr. Jones I have create in the Gui four textboxes, (testing with only two) $Nametextbox1 and $SamAccounttextbox2. I also created a submit button. I fill in the name and Samsaccount and press the submit button.

buttonSubmit_Click={
#TODO: Place custom script here

invoke-command -cn labdc4 -ScriptBlock {new-aduser -name $Nametextbox1 -SamAccountname $SamAccounttextbox2 `
-path ‘cn=users, dc=cosdepot,dc=local’ -enabled $true -accountpassword (convertto-secureString -asplaintext “ZAQ!2wsx” -force)}
error code returned is Cannot validate argument on parameter ‘Name’. The argument is null or empty. Supply an argument that is not null or empty and then try the command again. Thanks you for your assistance.

}

If $samaccounttextbox2 is a text box, then $samaccounttextbox2.text is the contents of the text box.

Also, you’re running into a common “Gotcha” with Invoke-Command. That script block executes on the remote computer, and doesn’t inherit variables in your local scope (in other words, $Nametestbox1 and $SamAccounttextbox2 don’t exist on the remote computer.) You can pass in arguments to the remote script block with the ArgumentList parameter. To enhance readability a bit, I’ve also set up a hashtable for splatting the arguments to Invoke-Command and New-ADUser in this example:

$buttonSubmit_Click = {
    #TODO: Place custom script here
    
    $InvokeCommandParams = @{
        ComputerName = 'labdc4'
        ArgumentList = $Nametextbox1.Text, $SamAccounttextbox2.Text
        
        ScriptBlock = {
            $NewADUserParams = @{
                Name = $args[0]
                SamAccountName = $args[1]
                Path = 'cn=users, dc=cosdepot,dc=local'
                Enabled = $true
                AccountPassword = ConvertTo-SecureString -AsPlainText 'ZAQ!2wsx' -Force
            }

            New-ADUser @NewADUserParams
        }
    }

    Invoke-Command @InvokeCommandParams
}

Notice that in the remote script block, you refer to $args[0] and $args[1], rather than the variable names from the local computer. (If you prefer, you can also define a param block in the remote script block to define named parameters, so long as they can be bound positionally.)

Hard-coding a plain text password in a script like that is a security risk, of course, but not related to the errors you were getting.

thanks to everyone I did get it working see below. Still need to fix try-catch block but it creates a new ad users

$buttonSubmit_Click={
#TODO: Place custom script here
$remoteUsername = “cosdepot\dsfadmin”
$remotePassword = “lab@US24”
$Pass = ConvertTo-SecureString $remotePassword -asPlainText -force
$cred = new-object System.Management.Automation.PSCredential($remoteUserName, $pass)
$activeD = “labdc4”
$remoteFE= “lablync”
$remoteSP = “labsp”

$remoteUsername = “cosdepot\dsfadmin”
$remotePassword = “lab@US24”
$Pass = ConvertTo-SecureString $remotePassword -asPlainText -force
$cred = new-object System.Management.Automation.PSCredential($remoteUserName, $pass)

Write-Host “name” $Nametextbox1.Text
Write-Host “sam” $SamAccounttextbox2.Text
$Name = $Nametextbox1.Text
$SamAccount = $SamAccounttextbox2.Text

try
{
invoke-command -cn labdc4 -ScriptBlock { param ($Name, $SamAccount) new-aduser -name $Name -SamAccountname $SamAccount `
-path ‘cn=users, dc=cosdepot,dc=local’ -enabled $true -accountpassword (convertto-secureString -asplaintext “ZAQ!2wsx” -force)} -ArgumentList $Name, $SamAccount
}
catch {

}

}