Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > PowerShell

Vista - ADD USER TO GROUP ??

Reply
 
Old 11-16-2006   #1 (permalink)
GC Email Manager


 
 

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
Old 11-17-2006   #2 (permalink)
fixitchris


 
 

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
Old 11-17-2006   #3 (permalink)
fixitchris


 
 

RE: ADD USER TO GROUP ??

My System SpecsSystem Spec
Old 11-21-2006   #4 (permalink)
GC Email Manager


 
 

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
Old 11-21-2006   #5 (permalink)
fixitchris


 
 

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
Old 11-27-2006   #6 (permalink)
GC Email Manager


 
 

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
Old 11-28-2006   #7 (permalink)
fixitchris


 
 

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
Old 11-28-2006   #8 (permalink)
GC Email Manager


 
 

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
Old 11-29-2006   #9 (permalink)
fixitchris


 
 

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
Old 11-29-2006   #10 (permalink)
GC Email Manager


 
 

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:

Quote:
$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
Reply

Thread Tools


Similar Threads
Thread Forum
Create user group in vista basic without the user and group manager window Vista security
add user to group /group scope - Global /Group type - Security PowerShell
Set Group Policy different for each user group? System Security
Add user to a group? PowerShell
Add User to AD Group PowerShell


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46