Powershell Read host issue

Kev51 VST 41 Reputation points
2022-02-01T19:50:09.877+00:00

Hi,

I have this string that causes me trouble.

If I write it this way, it does work properly.

Set-ADUser -Identity bsimard -Add @{"msDS-cloudExtensionAttribute1" = "1"}

But this way, the way I would like to, it does not.

Set-ADUser -Identity bsimard -Add @{"msDS-cloudExtensionAttribute1" = (Read-Host "Pour ajouter l utilisateur dans le groupe PRODUCTION ECRITURE entrer 1 sinon entrer 0")}

Sorry for the french part it is my client's first language.

Thanks!

Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,462 questions
0 comments No comments
{count} votes

Accepted answer
  1. Andreas Baumgarten 104K Reputation points MVP
    2022-02-01T19:57:23.747+00:00

    Hi @Kev51 VST ,

    why not keep it simple like this?

    $a = Read-Host "Pour ajouter l utilisateur dans le groupe PRODUCTION ECRITURE entrer 1 sinon entrer 0"  
    Set-ADUser -Identity bsimard -Add @{"msDS-cloudExtensionAttribute1" = "$a"}  
    

    ----------

    (If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)

    Regards
    Andreas Baumgarten

    1 person found this answer helpful.

2 additional answers

Sort by: Most helpful
  1. Rich Matheisen 45,906 Reputation points
    2022-02-01T20:05:47.11+00:00

    Does this work?

    $x = @{"msDS-cloudExtensionAttribute1" = (Read-Host "Pour ajouter l utilisateur dans le groupe PRODUCTION ECRITURE entrer 1 sinon entrer 0")}
    Set-ADUser -Identity bsimard -Add $x
    

    I'm assuming that your requirement is that the user should be asked to make a choice, and that you're not opposed to having two lines of code vs. one?

    0 comments No comments

  2. MotoX80 32,911 Reputation points
    2022-02-01T20:08:20.917+00:00

    But this way, the way I would like to, it does not.

    What did you type in? What error do you get?

    In the very first programming course that I took, the professor taught us to always validate our data. By coding the read-host the way that you do, you are not giving your script a chance to do any data validation. It would be better if your script prompted the user for all input data first before you did the Set-ADUser call. At a minimum you should check for a null response. In the case of an attribute like that, you would want to verify that it contains either a 0 or a 1, or whatever the valid range of responses is. And if the user typed in an alpha or symbol character, the script should tell the user: "Invalid character" or "Invalid input" and prompt them to enter the value again.

    0 comments No comments