Script to give AzureAD users admin rights?

expand collapsive

Hello, have a question about scripting. I’m new to this stuff and still figuring it out so apologies if I’m asking something really stupid…

When I need to shift AzureAD user accounts to the Administrators group in Windows I can use the following command: net localgroup administrators /add “AzureAD\username”

But is there a way I can upload a single script in a way where I don’t need to edit it each time to change the username?

Is that the kind of thing arguments are used for? If so, what do I need to put in the uploaded script, and what should I be entering in the arguments field when deploying the script on Hexnode?

Thank you.

All Replies

  • Hello @clyde, welcome to Hexnode Community!

    Yes, you got that absolutely right. This is where arguments come in handy. In the script, you can declare a variable and the value given as the argument will be passed to the variable during execution.
    You can use wildcards to pass the respective value to each user.

    That is, if yours is a batch script, it will look like this:

    Where %1 is the variable.

    In the Arguments field in the Hexnode UEM portal, you can enter the wildcard %username%.

    Enter wildcard in the arguments field

    Hope this helped with your query.

    Regards,
    Chloe Edison
    Hexnode UEM

  • Participant

    Faustica

    Participant

    To use arguments in your script, you need to define them at the beginning of the script using special variables called positional parameters. These variables are represented by numbers, starting from $0, which corresponds to the name of the script itself. For example, if you want to pass the username as an argument, you can define it like this:

    <br />param(<br />[string]$Username<br />)<br />

    This creates a parameter named “Username” that expects a string value. You can then use this parameter in your script wherever you need to reference the username, like this:

    <br />net localgroup administrators /add "AzureAD\$Username"<br />

    When you run the script, you can pass the username as an argument like this:

    <br />.\myscript.ps1 -Username jdoe<br />

    This will add the user “jdoe” to the Administrators group.

  • Participant

    Clyde

    Participant

    Stuck again…

    How do I deal with spaces in file and folder names in powershell?

    For example, creating a new folder with this PS script:

    param([string]$Dir)
    New-Item -Path “$Dir” -ItemType “directory”

    This works with the argument: .\newdir.ps1 -Dir c:\Test

    But not: .\newdir.ps1 -Dir c:\Another Test

    Did some googling and found many many different answers to this involving brackets, single quotes, and all kinds of things, and none of them seem to work. Some just outright fail, others drop odd directores into system32.

  • @clyde To pass arguments that contain a white space in a file/folder name or a path to a file/folder, you must include it in double quotes (” “).

    For example, to create a new folder with folder name Another Test using your script:

    You can use the following argument: .\newdir.ps1 -Dir "c:\Another Test".

    We hope this helps. Feel free to reach out to us if you have any more queries.

    Regards
    Eren Schwarz
    Hexnode UEM