Add a functional user in a IIS_GROUP

This topic contains 3 replies, has 2 voices, and was last updated by Profile photo of Martin Nielsen Martin Nielsen 2 years, 6 months ago.

  • Author
    Posts
  • #15505
    Profile photo of Ward Coysman
    Ward Coysman
    Participant

    Hello,

    I want to change following code :

    # Add IIS User to IIS Group
    function add_user_to_iis_group($username)
    {
    $computergroup = [ADSI]("WinNT://localhost,computer")
    $admins = $computergroup.psbase.children.find("IIS_IUSRS")
    foreach ($member in $admins.psbase.Invoke("Members"))
    {
    $member_name = $member.GetType().InvokeMember("Name", 'GetProperty', $null, $member, $null)
    if ($member_name -eq $username)
    {
    Write-Host $username "already in this group."
    return
    }
    }
    $admins.Add("WinNT://$username")
    Write-Host $username "added to group."

    to Read-Host "enter details"
    $computergroup
    $username

    I already made something like this in Powershell ISE :

    function add_user_to_iis_group $username = read-host "please enter the user"

    But it doesn't work and i don't understand why 🙁

    I'm not an expert in PS.

    Thanks for reading and hopefully replying.

  • #15542
    Profile photo of Martin Nielsen
    Martin Nielsen
    Participant

    Apparently the forum is refusing to let me post the code so I have to reference an off-site paste (I don't actually know if this is permitted?)

    http://pastebin.com/2GKrLUYD

    But in any case, this is actually quite simple.

    First we format the code to make it readable, and then we make the $username parameter mandatory by adding [Parameter(Mandatory=$true)]$username to the function definition.

    By making the $username parameter mandatory you force whoever is running the function to provide a username.

    • #15578
      Profile photo of Ward Coysman
      Ward Coysman
      Participant

      Hello Martin,

      Thank you for your reply but this script doesn't work in PS ISE ?

  • #15590
    Profile photo of Martin Nielsen
    Martin Nielsen
    Participant

    I'm not sure I understand your problem. How does it not work? What have you tried?

You must be logged in to reply to this topic.