Windows Vista Forums

ADD USER TO GROUP ??

  1. #1


    GC Email Manager Guest

    ADD USER TO GROUP ??

    .... in ASP please:



    can someone give me an example (using pop-up windows login authentication)
    of how to get this powershell script to run assuming the user chose the group
    name and the membername was that of the AD user, via an ASP page? Please
    include what authentication to use and how to make the authentication work.
    the full asp page would be gladly appreciated.

    $ add-DistributionGroupMember myDistName -Member myADname

    The idea is that we would present a list of groups to the user. He/she could
    click a group and a button that says "add me to group."

    not sure what user access the code would have to run under.

      My System SpecsSystem Spec

  2. #2


    fixitchris Guest

    RE: ADD USER TO GROUP ??

    start here . I'm sure you'll have to go back to the old blogs... but it's all
    there, at least the powershell side of what you're looking for....

    http://mow001.blogspot.com/2006/09/p...ectory_29.html


      My System SpecsSystem Spec

  3. #3


    fixitchris Guest

    RE: ADD USER TO GROUP ??

      My System SpecsSystem Spec

  4. #4


    GC Email Manager Guest

    RE: ADD USER TO GROUP ??

    I read those and understand writing powershell lines of code. But I still
    can't find a solid example in (.asp) format that I can embed code in an ASP
    page so that when a button is clicked, it runs a powershell script and takes
    action.

    Any examples please?
    --
    Regards,

    Blake Whitney
    GC Email Manager


    "fixitchris" wrote:

    > start here . I'm sure you'll have to go back to the old blogs... but it's all
    > there, at least the powershell side of what you're looking for....
    >
    > http://mow001.blogspot.com/2006/09/p...ectory_29.html
    >


      My System SpecsSystem Spec

  5. #5


    fixitchris Guest

    RE: ADD USER TO GROUP ??

    I read your post regarding the aspx aspx.vb files.... here goes. i assume
    you have Visual Studio. if not then pick up a copy.

    Create SDK folder on your C drive...

    run this in PS
    [appdomain]::currentdomain.getassemblies() | where {($_.fullname -match
    "system.management") -OR ($_.fullname –match "Microsoft")} |copy-item -path
    {$_.location} -destination c:\sdk\ -verbose

    inside Visual Studio create a new web site asp.net... I assume you have .net
    framework, if not download .NET 3.0 FX from m$.com

    when you create your default website in Visual Studio, you will have a
    default.aspx and default.aspx.vb files in your Solution Explorer window. The
    default.aspx file is the file that is publically exposed by IIS, the aspx.vb
    is the CodeBehind file that contains the VB or C# code that handles
    processing. These files can be combined into one, etc.

    Now... you have some files in your c:\sdk folder, these are the powershell
    assemblies you will want to add as references in Visual Studio under
    Website/Add Reference... Menu. I added SYSTEM.MANAGEMENT and
    SYSTEM.MANAGEMENT.AUTOMATION dlls.

    This is the code that goes into default.aspx.vb:


    Imports System.Management
    Imports System.Management.automation
    Imports System.Management.Automation.Runspaces.RunspaceFactory

    Partial Class _Default
    Inherits System.Web.UI.Page
    Public MyRunspace As Runspaces.Runspace
    Public sessionproxy As Hashtable
    Public myinvoke As Automation.RunspaceInvoke
    End Class



    This is the code that goes into default.aspx:

    <%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb"
    Inherits="_Default" %>


    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head id="Head1" runat="server">
    <title>PowerAsp Demo Page</title>
    </head>
    <body>
    <form id="form1" runat="server">
    <div>
    <h3>PowerShell ASP Test!</h3>
    <p>
    <asp:Label ID="DateLabel" runat="server" Text="Todays
    date"></asp:Label>
    </p>
    <asp:TextBox ID="Expression" Width="500" runat="server" Rows="3"
    TextMode="MultiLine">
    function fact($x) {if ($x -lt 2 ) {1} else {$x * (fact ($x-1))}}; fact 3
    </asp:TextBox>
    <p>
    <asp:Button ID="Button1" OnClick="Evaluate" runat="server" Text="Eval
    Expression" Visible="true" />
    <asp:Button ID="Button2" OnClick="Clear" runat="server" Text="Clear
    Listbox " Visible="true" />
    </p>
    <asp:TextBox ID="Script" visible="false" runat="server">
    # mark the date
    $SessionProxy.DateLabel.text = "Evaluated at $(get-date)";
    function out-listbox
    {
    $input | out-string -stream | %{
    $SessionProxy.ResultListBox.Items.Add($_)
    }
    }
    $r = $SessionProxy.Expression.text + ' = ';
    $r += $(if ($SessionProxy.Expression.text)
    {
    invoke-expression $SessionProxy.Expression.Text
    } else {
    'no expression: ' + $SessionProxy.Expression.text
    };
    );
    $SessionProxy.ResultListBox.Items.Add($r);
    </asp:TextBox>
    <p>
    <asp:ListBox ID="ResultListBox" Width="600" runat="server"
    Rows="12"></asp:ListBox>
    </p>
    <script runat="server" language="VB">
    Sub Evaluate(ByVal s As Object, ByVal e As EventArgs)
    If Me.Session("runspace") Is Nothing Then
    MyRunspace =
    System.Management.Automation.Runspaces.RunspaceFactory.CreateRunspace()
    MyRunspace.Open()
    Me.Session.Add("runspace", MyRunspace)
    Me.SessionProxy =
    System.Collections.Hashtable.Synchronized(New Hashtable())
    MyRunspace.SessionStateProxy.SetVariable("SessionProxy",
    Me.SessionProxy)
    Me.Session.Add("SessionProxy", SessionProxy)
    Else
    SessionProxy = Me.Session("SessionProxy")
    MyRunspace = Me.Session("runspace")
    End If
    SessionProxy("DateLabel") = DateLabel
    SessionProxy("Expression") = Expression
    SessionProxy("ResultListBox") = ResultListBox
    MyInvoke = New
    System.Management.Automation.RunspaceInvoke(MyRunspace)
    MyInvoke.Invoke(Script.Text)
    End Sub
    Sub Clear(ByVal s As Object, ByVal e As EventArgs)
    ResultListBox.Items.Clear()
    End Sub
    </script>
    </div>
    </form>
    </body>
    </html>



    Now you can run the webpage from Visual Studio... it works for me. let me
    know if you run into problems.

    Chris


      My System SpecsSystem Spec

  6. #6


    GC Email Manager Guest

    RE: ADD USER TO GROUP ??

    This example seemed to work. However I was wondering two things:

    Can I just paste powershell scripts in there and run them? I'm particularly
    looking to query active directory (i'm a domain admin) and then potentially
    run the scripts for junior admins.

    Any examples of this? This is the most helpful example I've seen on the net
    so far.
    --
    Regards,

    Blake Whitney
    GC Email Manager


    "fixitchris" wrote:

    > I read your post regarding the aspx aspx.vb files.... here goes. i assume
    > you have Visual Studio. if not then pick up a copy.
    >
    > Create SDK folder on your C drive...
    >
    > run this in PS
    > [appdomain]::currentdomain.getassemblies() | where {($_.fullname -match
    > "system.management") -OR ($_.fullname –match "Microsoft")} |copy-item -path
    > {$_.location} -destination c:\sdk\ -verbose
    >
    > inside Visual Studio create a new web site asp.net... I assume you have .net
    > framework, if not download .NET 3.0 FX from m$.com
    >
    > when you create your default website in Visual Studio, you will have a
    > default.aspx and default.aspx.vb files in your Solution Explorer window. The
    > default.aspx file is the file that is publically exposed by IIS, the aspx.vb
    > is the CodeBehind file that contains the VB or C# code that handles
    > processing. These files can be combined into one, etc.
    >
    > Now... you have some files in your c:\sdk folder, these are the powershell
    > assemblies you will want to add as references in Visual Studio under
    > Website/Add Reference... Menu. I added SYSTEM.MANAGEMENT and
    > SYSTEM.MANAGEMENT.AUTOMATION dlls.
    >
    > This is the code that goes into default.aspx.vb:
    >
    >
    > Imports System.Management
    > Imports System.Management.automation
    > Imports System.Management.Automation.Runspaces.RunspaceFactory
    >
    > Partial Class _Default
    > Inherits System.Web.UI.Page
    > Public MyRunspace As Runspaces.Runspace
    > Public sessionproxy As Hashtable
    > Public myinvoke As Automation.RunspaceInvoke
    > End Class
    >
    >
    >
    > This is the code that goes into default.aspx:
    >
    > <%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb"
    > Inherits="_Default" %>
    >
    >
    > <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    > "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    >
    > <html xmlns="http://www.w3.org/1999/xhtml" >
    > <head id="Head1" runat="server">
    > <title>PowerAsp Demo Page</title>
    > </head>
    > <body>
    > <form id="form1" runat="server">
    > <div>
    > <h3>PowerShell ASP Test!</h3>
    > <p>
    > <asp:Label ID="DateLabel" runat="server" Text="Todays
    > date"></asp:Label>
    > </p>
    > <asp:TextBox ID="Expression" Width="500" runat="server" Rows="3"
    > TextMode="MultiLine">
    > function fact($x) {if ($x -lt 2 ) {1} else {$x * (fact ($x-1))}}; fact 3
    > </asp:TextBox>
    > <p>
    > <asp:Button ID="Button1" OnClick="Evaluate" runat="server" Text="Eval
    > Expression" Visible="true" />
    > <asp:Button ID="Button2" OnClick="Clear" runat="server" Text="Clear
    > Listbox " Visible="true" />
    > </p>
    > <asp:TextBox ID="Script" visible="false" runat="server">
    > # mark the date
    > $SessionProxy.DateLabel.text = "Evaluated at $(get-date)";
    > function out-listbox
    > {
    > $input | out-string -stream | %{
    > $SessionProxy.ResultListBox.Items.Add($_)
    > }
    > }
    > $r = $SessionProxy.Expression.text + ' = ';
    > $r += $(if ($SessionProxy.Expression.text)
    > {
    > invoke-expression $SessionProxy.Expression.Text
    > } else {
    > 'no expression: ' + $SessionProxy.Expression.text
    > };
    > );
    > $SessionProxy.ResultListBox.Items.Add($r);
    > </asp:TextBox>
    > <p>
    > <asp:ListBox ID="ResultListBox" Width="600" runat="server"
    > Rows="12"></asp:ListBox>
    > </p>
    > <script runat="server" language="VB">
    > Sub Evaluate(ByVal s As Object, ByVal e As EventArgs)
    > If Me.Session("runspace") Is Nothing Then
    > MyRunspace =
    > System.Management.Automation.Runspaces.RunspaceFactory.CreateRunspace()
    > MyRunspace.Open()
    > Me.Session.Add("runspace", MyRunspace)
    > Me.SessionProxy =
    > System.Collections.Hashtable.Synchronized(New Hashtable())
    > MyRunspace.SessionStateProxy.SetVariable("SessionProxy",
    > Me.SessionProxy)
    > Me.Session.Add("SessionProxy", SessionProxy)
    > Else
    > SessionProxy = Me.Session("SessionProxy")
    > MyRunspace = Me.Session("runspace")
    > End If
    > SessionProxy("DateLabel") = DateLabel
    > SessionProxy("Expression") = Expression
    > SessionProxy("ResultListBox") = ResultListBox
    > MyInvoke = New
    > System.Management.Automation.RunspaceInvoke(MyRunspace)
    > MyInvoke.Invoke(Script.Text)
    > End Sub
    > Sub Clear(ByVal s As Object, ByVal e As EventArgs)
    > ResultListBox.Items.Clear()
    > End Sub
    > </script>
    > </div>
    > </form>
    > </body>
    > </html>
    >
    >
    >
    > Now you can run the webpage from Visual Studio... it works for me. let me
    > know if you run into problems.
    >
    > Chris
    >


      My System SpecsSystem Spec

  7. #7


    fixitchris Guest

    RE: ADD USER TO GROUP ??

    pretty much, yes you can. replace this part

    <asp:TextBox ID="Expression" Width="500" runat="server" Rows="3"
    > TextMode="MultiLine">
    > function fact($x) {if ($x -lt 2 ) {1} else {$x * (fact ($x-1))}}; fact 3
    > </asp:TextBox>



    with this....

    <asp:TextBox ID="Expression" Width="500" runat="server" Rows="3"
    TextMode="MultiLine">
    $([adsi]'').dc;
    </asp:TextBox>

    then when you look lower, you'll see that the TEXT inside the Expression
    TEXTBOX is being executed as a PS script via the INVOKE-EXPRESSION PowerShell
    Command. So in reality you can INVOKE scripts all over the place.

    Do you have a specific script you want to run?



      My System SpecsSystem Spec

  8. #8


    GC Email Manager Guest

    RE: ADD USER TO GROUP ??

    I will take a look. What I'm really trying to do is to have some actions that
    a junior admin can choose from. Here's an example

    1. jr. admin chooses: modify group membership
    2. text box appears (or was already visible) for the jr. admin to put in a
    samaccountname
    3. jr. admin clicks "GO"
    4. page displays groups account is memberOf
    5. jr. admin clicks (or CTRL-clicks) group(s) and clicks "REMOVE"

    Obviously I want to make the page authenticated so that when someone logs in
    I will check their samaccountname against AD security (via group membership)
    and if they're a junior admin I will let them run the page. But I will run
    the scripts as an admin account where the username and pwd is hidden in the
    aspx.vb file, not in the .aspx file.

    I guess what would help would be an example where I can just change the base
    OU to search on, and then input a username and it will spit out the results.
    If the powershell script is there to get this info and spit it out, I should
    be able to find the right script to then update the group membership via
    powershell.

    One other side question if you don't mind:

    If I want this asp.net page to run on windows 2003 server, do i need to
    install .net 3.0 or 2.0 as well as powershell? I tried to install powershell
    (x86 version) and it said that it was not the right OS type or something. Odd.

    Thanks again for being such a help. I'm surprised more people aren't writing
    about this stuff cuz this is the biggest use I can see: delegating actions to
    junior admins by biulding a quick webpage that really only runs powershell
    scripts in the background. we can't afford those other tools (webdir,
    rDirectory, etc.)
    --
    Regards,

    Blake Whitney
    GC Email Manager


    "fixitchris" wrote:

    > pretty much, yes you can. replace this part
    >
    > <asp:TextBox ID="Expression" Width="500" runat="server" Rows="3"
    > > TextMode="MultiLine">
    > > function fact($x) {if ($x -lt 2 ) {1} else {$x * (fact ($x-1))}}; fact 3
    > > </asp:TextBox>

    >
    >
    > with this....
    >
    > <asp:TextBox ID="Expression" Width="500" runat="server" Rows="3"
    > TextMode="MultiLine">
    > $([adsi]'').dc;
    > </asp:TextBox>
    >
    > then when you look lower, you'll see that the TEXT inside the Expression
    > TEXTBOX is being executed as a PS script via the INVOKE-EXPRESSION PowerShell
    > Command. So in reality you can INVOKE scripts all over the place.
    >
    > Do you have a specific script you want to run?
    >
    >


      My System SpecsSystem Spec

  9. #9


    fixitchris Guest

    RE: ADD USER TO GROUP ??

    Try this updated default.aspx code.... This should give you a clearer idea of
    what is happening.

    <%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb"
    Inherits="_Default" %>


    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head id="Head1" runat="server">
    <title>PowerAsp Demo Page</title>
    </head>
    <body>
    <form id="form1" runat="server">
    <div>
    <h3>PowerShell ASP Test!</h3>
    <p>
    <asp:Label ID="DateLabel" runat="server" Text="Todays
    date"></asp:Label>
    </p>
    <asp:TextBox ID="Expression" Width="500" runat="server" Rows="3"
    TextMode="MultiLine">
    Administrator
    </asp:TextBox>
    <p>
    <asp:Button ID="Button1" OnClick="Evaluate" runat="server" Text="Eval
    Expression" Visible="true" />
    <asp:Button ID="Button2" OnClick="Clear" runat="server" Text="Clear
    Listbox " Visible="true" />
    </p>
    <asp:TextBox ID="Script" visible="false" runat="server">
    $objRoot = [ADSI]"WinNT://./Administrator,user";
    $objGrpMembers = @($objRoot.psbase.Invoke("Groups"));
    $objGrpMembers
    |%{$SessionProxy.ResultListBox.Items.Add($_.GetType().InvokeMember("Name",
    'GetProperty', $null, $_, $null))}
    </asp:TextBox>
    <p>
    <asp:ListBox ID="ResultListBox" Width="600" runat="server"
    Rows="12"></asp:ListBox>
    </p>
    <script runat="server" language="VB">
    Sub Evaluate(ByVal s As Object, ByVal e As EventArgs)
    If Me.Session("runspace") Is Nothing Then
    MyRunspace =
    System.Management.Automation.Runspaces.RunspaceFactory.CreateRunspace()
    MyRunspace.Open()
    Me.Session.Add("runspace", MyRunspace)
    Me.SessionProxy =
    System.Collections.Hashtable.Synchronized(New Hashtable())
    MyRunspace.SessionStateProxy.SetVariable("SessionProxy",
    Me.SessionProxy)
    Me.Session.Add("SessionProxy", SessionProxy)
    Else
    SessionProxy = Me.Session("SessionProxy")
    MyRunspace = Me.Session("runspace")
    End If
    sessionproxy("DateLabel") = DateLabel
    SessionProxy("Expression") = Expression
    SessionProxy("ResultListBox") = ResultListBox
    myinvoke = New
    System.Management.Automation.RunspaceInvoke(MyRunspace)
    MyInvoke.Invoke(Script.Text)
    End Sub
    Sub Clear(ByVal s As Object, ByVal e As EventArgs)
    ResultListBox.Items.Clear()
    End Sub
    </script>
    </div>
    </form>
    </body>
    </html>


    >
    > One other side question if you don't mind:
    >
    > If I want this asp.net page to run on windows 2003 server, do i need to
    > install .net 3.0 or 2.0 as well as powershell? I tried to install powershell
    > (x86 version) and it said that it was not the right OS type or something. Odd.


    To serve out ASP.NET Powershelled pages on 2003 server you need:
    IIS6
    ..NET 2 or 3 (see aspnet_regiis.exe command)
    Powershell

    Are you installing x86 Powershell on 64 bit 2003 server? I believe 64 bit
    Powershell came out...?

      My System SpecsSystem Spec

  10. #10


    GC Email Manager Guest

    RE: ADD USER TO GROUP ??

    Is there somewhere that you're learning all this? I see you're using ADSI. Is
    there a tutorial you could point me to regarding this? I'm particularly
    interested in understanding this piece:

    $objRoot = [ADSI]"WinNT://./Administrator,user";
    $objGrpMembers = @($objRoot.psbase.Invoke("Groups"));
    $objGrpMembers |
    %{$SessionProxy.ResultListBox.Items.Add($_.GetType().InvokeMember("Name",
    'GetProperty', $null, $_, $null))}
    what does WinNT://./Administrator,user mean?

    Is that the username/pwd combo to use?

    So far no matter who I put into the textbox the group returned is
    "Administrators" Is this checking local groups? I need it to check Active
    Directory groups.

    Plus will this cycle through the groups.

    Last question for now :-)

    --- Do you have any reason to prefer writing ADSI scripts versus WMI scripts?

    Thanks again for being such a huge help!







    --
    Regards,

    Blake Whitney
    GC Email Manager


    "fixitchris" wrote:

    > Try this updated default.aspx code.... This should give you a clearer idea of
    > what is happening.
    >
    > <%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb"
    > Inherits="_Default" %>
    >
    >
    > <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    > "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    >
    > <html xmlns="http://www.w3.org/1999/xhtml" >
    > <head id="Head1" runat="server">
    > <title>PowerAsp Demo Page</title>
    > </head>
    > <body>
    > <form id="form1" runat="server">
    > <div>
    > <h3>PowerShell ASP Test!</h3>
    > <p>
    > <asp:Label ID="DateLabel" runat="server" Text="Todays
    > date"></asp:Label>
    > </p>
    > <asp:TextBox ID="Expression" Width="500" runat="server" Rows="3"
    > TextMode="MultiLine">
    > Administrator
    > </asp:TextBox>
    > <p>
    > <asp:Button ID="Button1" OnClick="Evaluate" runat="server" Text="Eval
    > Expression" Visible="true" />
    > <asp:Button ID="Button2" OnClick="Clear" runat="server" Text="Clear
    > Listbox " Visible="true" />
    > </p>
    > <asp:TextBox ID="Script" visible="false" runat="server">
    > $objRoot = [ADSI]"WinNT://./Administrator,user";
    > $objGrpMembers = @($objRoot.psbase.Invoke("Groups"));
    > $objGrpMembers
    > |%{$SessionProxy.ResultListBox.Items.Add($_.GetType().InvokeMember("Name",
    > 'GetProperty', $null, $_, $null))}
    > </asp:TextBox>
    > <p>
    > <asp:ListBox ID="ResultListBox" Width="600" runat="server"
    > Rows="12"></asp:ListBox>
    > </p>
    > <script runat="server" language="VB">
    > Sub Evaluate(ByVal s As Object, ByVal e As EventArgs)
    > If Me.Session("runspace") Is Nothing Then
    > MyRunspace =
    > System.Management.Automation.Runspaces.RunspaceFactory.CreateRunspace()
    > MyRunspace.Open()
    > Me.Session.Add("runspace", MyRunspace)
    > Me.SessionProxy =
    > System.Collections.Hashtable.Synchronized(New Hashtable())
    > MyRunspace.SessionStateProxy.SetVariable("SessionProxy",
    > Me.SessionProxy)
    > Me.Session.Add("SessionProxy", SessionProxy)
    > Else
    > SessionProxy = Me.Session("SessionProxy")
    > MyRunspace = Me.Session("runspace")
    > End If
    > sessionproxy("DateLabel") = DateLabel
    > SessionProxy("Expression") = Expression
    > SessionProxy("ResultListBox") = ResultListBox
    > myinvoke = New
    > System.Management.Automation.RunspaceInvoke(MyRunspace)
    > MyInvoke.Invoke(Script.Text)
    > End Sub
    > Sub Clear(ByVal s As Object, ByVal e As EventArgs)
    > ResultListBox.Items.Clear()
    > End Sub
    > </script>
    > </div>
    > </form>
    > </body>
    > </html>
    >
    >
    > >
    > > One other side question if you don't mind:
    > >
    > > If I want this asp.net page to run on windows 2003 server, do i need to
    > > install .net 3.0 or 2.0 as well as powershell? I tried to install powershell
    > > (x86 version) and it said that it was not the right OS type or something. Odd.

    >
    > To serve out ASP.NET Powershelled pages on 2003 server you need:
    > IIS6
    > .NET 2 or 3 (see aspnet_regiis.exe command)
    > Powershell
    >
    > Are you installing x86 Powershell on 64 bit 2003 server? I believe 64 bit
    > Powershell came out...?


      My System SpecsSystem Spec

Page 1 of 2 12 LastLast
ADD USER TO GROUP ??

Similar Threads
Thread Thread Starter Forum Replies Last Post
Create user group in vista basic without the user and group manager window stephP Vista security 0 29 Jul 2009
add user to group /group scope - Global /Group type - Security Michael PowerShell 2 14 Jul 2009
Set Group Policy different for each user group? PainlessTorture System Security 2 12 Jul 2008
Add user to a group? Ian_1 PowerShell 1 06 Mar 2008
Add User to AD Group Thomas M. PowerShell 7 08 Aug 2007