new-aduser error

This topic contains 12 replies, has 6 voices, and was last updated by Profile photo of Istvan Szarka Istvan Szarka 2 years, 6 months ago.

  • Author
    Posts
  • #16081
    Profile photo of yaron wainberg
    yaron wainberg
    Participant

    Hi

    i wanted to create a script that will help me save time when i create a new user
    the script is a one-liner and goes like that :
    New-ADUser -Name (Read-Host "Name") -GivenName (Read-Host "GivenName") -Surname (Read-Host "SureName") -SamAccountName (Read-Host "SamAccountName") -Path (Read-Host "Path") -Enabled $true -AccountExpirationDate $null -AccountPassword (Read-Host -AsSecureString "AccountPassword")

    when i run it and put all the parameters i get an error : "New-ADUser : The object name has bad syntax"

    what am i doing wrong?

    Thanks

  • #16082
    Profile photo of Richard Siddaway
    Richard Siddaway
    Moderator

    Short answer is using Read-host in the way that you do.

    I would either just type in the values, or turn your code into a simple function or create a csv file with the data and pass that to New-AdUser

    • #16085
      Profile photo of yaron wainberg
      yaron wainberg
      Participant

      can you give me an example to a simple function?
      why shouldn't i use "read-host"

      Thanks

  • #16083
    Profile photo of Dave Wyatt
    Dave Wyatt
    Moderator

    Depends on what you typed in for the "Name" prompt, I think. (Path might be involved as well.)

  • #16084
    Profile photo of Dave Wyatt
    Dave Wyatt
    Moderator

    By the way, this is the preferred way to prompt for input if a parameter hasn't been passed in to your script. When you mark everything as Mandatory, the PowerShell engine takes care of that for you (including masking password input). It's probably still using Read-Host or something similar to that under the hood, but you don't need to know that. More importantly, this gives you the ability to run the script without all the prompts, if you want. (eg, .\NewADUser.ps1 'Bob Jones' Bob Jones bjones 'OU=Users,DC=Contoso,DC=com' ('p@ssw0rd' | ConvertTo-SecureString -AsPlainText -Force) )

    param (
        [Parameter(Mandatory = $true)]
        [string]
        $Name,
    
        [Parameter(Mandatory = $true)]
        [string]
        $GivenName,
    
        [Parameter(Mandatory = $true)]
        [string]
        $Surname,
    
        [Parameter(Mandatory = $true)]
        [string]
        $SamAccountName,
    
        [Parameter(Mandatory = $true)]
        [string]
        $Path,
    
        [Parameter(Mandatory = $true)]
        [System.Security.Securestring]
        $AccountPassword
    )
    
    New-ADUser @PSBoundParameters -Enabled $true -AccountExpirationDate $null
    
  • #16088
    Profile photo of H Man
    H Man
    Participant

    $FileToImport = "C:\NewHires.csv"
    import-csv $FileToImport |ForEach-Object { New-QADUser -name $_.name -FirstName $_.FirstName -LastName $_.LastName -SamAccountName $_.Samaccountname -Email $_.email`
    -userpassword $_.password -HomeDirectory $_.homedirectory -HomeDrive "U:" -Displayname $_.displayname -title $_.title -Department $_.department -office $_.office -streetaddress $_.streetaddress -city $_.city `
    -description 'Class_06-02' -stateorprovince $_.stateorprovince -postalcode $_.postalcode -UserPrincipalName $_.UserPrincipalName -ParentContainer your.domain/ou/users}
    # requires quest snap in

  • #16093
    Profile photo of Joakim
    Joakim
    Participant

    I don't get what am I doing wrong with code formatting?
    I use the code tags and the formatting just gets ugly.
    On wordpress.com you can use [code language="powershell"][/code] and it works beautifully, even with color coding.

    You are not alone to be annyoed by this. I think it is an issue with the BBPress plugin choosen.

    Do you know which code formatting plugin WordPress.com are using?

  • #16094
    Profile photo of Istvan Szarka
    Istvan Szarka
    Participant

    I don't know. But Dave's script is formatted alright, I wonder how does he do that.

  • #16107
    Profile photo of Istvan Szarka
    Istvan Szarka
    Participant

    I see we've got very nice code formatting, thanks 🙂

  • #16090
    Profile photo of Istvan Szarka
    Istvan Szarka
    Participant

    Read-Host is very inflexible. It just prompts you to enter something and that's it.
    However, parameterizing your one-liner, as Dave did, has many advantages. It makes your script look and act like a real PowerShell cmdlet, especially if you do the following. Amend Dave's script like this:


    Function New-CustomUser {
    param (
    [Parameter(Mandatory = $true)]
    [string]
    $Name,

    [Parameter(Mandatory = $true)]
    [string]
    $GivenName,

    [Parameter(Mandatory = $true)]
    [string]
    $Surname,

    [Parameter(Mandatory = $true)]
    [string]
    $SamAccountName,

    [Parameter(Mandatory = $true)]
    [string]
    $Path,

    [Parameter(Mandatory = $true)]
    [System.Security.Securestring]
    $AccountPassword
    )

    New-ADUser @PSBoundParameters -Enabled $true -AccountExpirationDate $null
    }

    In file explorer, go to "C:\Program Files\WindowsPowerShell\Modules" and create a folder called "New-CustomUser".
    Save the script in this folder as New-CustomUser.psm1. It's very important that you save it with a .psm1 extension!
    When it's done, close PowerShell and open it again. In PowerShell ISE, start typing New-CustomUser and you will see that PowerShell recognizes your command just as any native cmdlet, like New-ADUser. You even get tab-completeion on the cmdlet and its parameters. The ISE will even show you the parameters thanks to autosense.
    Actually, you have just written a script module that gets loaded automatically. How awesome it is!

    The other problem with Read-Host is, that it's not suitable for automation, since it cannot run unattended.

  • #16106
    Profile photo of yaron wainberg
    yaron wainberg
    Participant

    Thanks you for all your helpful comments
    I will try the function that you suggested

    the @PSBoundParameters is a new concept to me
    so i will follow up to learn more about

  • #16091
    Profile photo of Istvan Szarka
    Istvan Szarka
    Participant

    I don't get what am I doing wrong with code formatting?
    I use the code tags and the formatting just gets ugly.
    On wordpress.com you can use [code language="powershell"][/code] and it works beautifully, even with color coding.

  • #16095
    Profile photo of Dave Wyatt
    Dave Wyatt
    Moderator

    For simple stuff, you can use PRE tags instead of CODE. If you need to post code with backticks, or embedded strings that look like HTML, I use a PowerShell function to escape the code before posting. See https://powershell.org/forums-tips/ for more information, under "Format Your Code". The function I mentioned is in this blog post, and if you add the code to your ISE profile, you can just press F7 to escape the current ISE window's code and copy it to your clipboard for pasting into a thread on the forums.

    That said, you may see some changes in the WordPress / bbPress plugins being used here at some point in the near future, to make it more friendly for PowerShell. Don posted about this yesterday (https://powershell.org/2014/06/09/wish-list-better-code-formatting-in-the-forums-can-you-help/).

    Edit: Nevermind, now the formatting in the blog post was screwed up too. Download it from TechNet instead.

You must be logged in to reply to this topic.