Quantcast
Channel: PowerShell.org » All Posts
Viewing all articles
Browse latest Browse all 13067

Reply To: Gui development

$
0
0

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.


Viewing all articles
Browse latest Browse all 13067

Trending Articles