locked
Powershell -- The argument is null or empty RRS feed

  • Question

  • Hello Microsoft Community,

    I'm very new to Powershell, but I do have a bit of experience coding.

    I'm trying to create a powershell script to onboard users into our AD.

    Here's an example of what I'm attempting.

    function example{
         $user = read-host -prompt "enter username"

         Invoke-Command -ComputerName Server -ScriptBlock {New-ADUser -Name $user}

    }

    Im getting this error message

    Cannot validate argument on parameter 'Name'. The argument is null or empty. Provide an argument that is not null or empty, and then
    try the command again.

    I'm assuming the scriptblock command isn't able to reference the variables in this function. I'm wondering what syntax to use call these variables inside scriptblock.

    A nudge in the right direction would greatly help.

    Thank you

    Sunday, June 28, 2020 1:37 AM

All replies

  • Use the -ArgumentList parameter in the Invoke-Command. Something like this:

    Invoke-Command -ComputerName Server -ScriptBlock {param($u) New-ADUser -Name $u} -ArgumentList $user


    --- Rich Matheisen MCSE&I, Exchange Ex-MVP (16 years)

    Sunday, June 28, 2020 1:54 AM
  • Thank you for the reply.
    I'm interested in learning the fundamentals of this. If I'm attempting to reference a series of variables into this command would I use the following syntax?

    Invoke-Command -ComputerName Server -ScriptBlock {param($u,$g...) New-ADUser -Name $u -GivenName $g} 
    -
    ArgumentList $user,$firstName...

    thanks you again

    Sunday, June 28, 2020 2:10 AM
  • That'll work but using positional parameters gets awkward when there are lots of parameters.

    Try searching the web for something like "pass parameters invoke-command scriptblock" and you'll find there are other ways to do this. You can also use PowerShell's "help about_Remote_Variables".

    Here's another way to use a local variable in a remote command:

      Invoke-Command -ComputerName Server -ScriptBlock {New-ADUser -Name $using:user}

    Keep in mind that the parameter is expanded local and you can't modify the parameter on the local machine from the remote machine.


    --- Rich Matheisen MCSE&I, Exchange Ex-MVP (16 years)

    • Proposed as answer by Vector BCO Wednesday, July 1, 2020 7:17 AM
    Sunday, June 28, 2020 2:51 AM
  • hi W0rmsp17,

    for your case looks like better way would be install RSAT with activedirectory module on a pc/server where you will run your script.

    In this case you will get possibility to use ad cmdlets directly without invoke-command


    The opinion expressed by me is not an official position of Microsoft

    Monday, June 29, 2020 12:01 AM
  • Hi,
    Was your issue resolved? 
    If you resolved it using our solution, please "mark it as answer" to help other community members find the helpful reply quickly.
    If you resolve it using your own solution, please share your experience and solution here. It will be very beneficial for other community members who have similar questions.
    If no, please reply and tell us the current situation in order to provide further help.
    Best Regards,
    Yang Yang

    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    Friday, July 3, 2020 5:47 AM
  • Yang Young, you are a little bit spaming all members from each discussion you added comment

    The opinion expressed by me is not an official position of Microsoft

    Friday, July 3, 2020 6:02 AM
  • I'm sorry, and thank you very much for your reminder.

    I will pay more attention next time.
    Friday, July 3, 2020 6:16 AM