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 - Windows XP, Users and Groups

Reply
 
Old 09-20-2007   #1 (permalink)
MaciekN


 
 

Windows XP, Users and Groups

I'm trying to write scripts for managing Windows XP's users and groups.
I can create user:

$ds=[ADSI]"WinNT://host"
$user=$ds.psbase.Children.Add("new_user","User")
$user.psbase.CommitChanges()
$user.SetPassword("new_pass")
$user.psbase.CommitChanges()

But I can't add user to group.
I tried this:

$user.PSBase.Children.Add("Administratorzy","Group")

But I got this:

Exception calling "Add" with "2" argument(s): "Obiekt usługi Active
Directory z
lokalizowany w ścieżce WinNT://workgroup/host/new_user nie jest
kontenerem."
At line:1 char:26

+ $user.PSBase.Children.Add( <<<< "Administratorzy","Group")


In English: Active Directry object is not a container.

It's horrible! I can't find anywhere how to add user to group. If
someone can help me, I will be happy.

P.S. I want to apologise for my English. I'm from Poland (and I'm proud
of it!)


My System SpecsSystem Spec
Old 09-20-2007   #2 (permalink)
Brandon Shell


 
 

Re: Windows XP, Users and Groups

Two Problems
One problem is that you need to use the User Path i.e.
"WinNT://$server/$User"
Two... you need to add the user via the group object.

Here is my method
================
Param([string]$user,[string]$group,[string]$server)
If(!($server)){$server = get-content env:COMPUTERNAME}
Write-Host ""
Write-Host "Adding User [$user] to group [$group]"
Write-Host
"----------------------------------------------" -ForegroundColor gray
$ary = @()
$srv = [ADSI]("WinNT://$server,computer")
$u = [ADSI]("WinNT://$server/$User,User")
$ary += $u.psbase.path

# Getting Group and Adding User
$g = [ADSI]("WinNT://$server/$group,group")
$g.psbase.invoke("add",$ary)
Write-Host "If no Exception... User Added" -ForegroundColor Green

"MaciekN" <maciek43360@xxxxxx> wrote in message
news:%23CUh8Y6%23HHA.2268@xxxxxx
Quote:

> I'm trying to write scripts for managing Windows XP's users and groups.
> I can create user:
>
> $ds=[ADSI]"WinNT://host"
> $user=$ds.psbase.Children.Add("new_user","User")
> $user.psbase.CommitChanges()
> $user.SetPassword("new_pass")
> $user.psbase.CommitChanges()
>
> But I can't add user to group.
> I tried this:
>
> $user.PSBase.Children.Add("Administratorzy","Group")
>
> But I got this:
>
> Exception calling "Add" with "2" argument(s): "Obiekt usługi Active
> Directory z
> lokalizowany w ścieżce WinNT://workgroup/host/new_user nie jest
> kontenerem."
> At line:1 char:26
>
> + $user.PSBase.Children.Add( <<<< "Administratorzy","Group")
>
>
> In English: Active Directry object is not a container.
>
> It's horrible! I can't find anywhere how to add user to group. If
> someone can help me, I will be happy.
>
> P.S. I want to apologise for my English. I'm from Poland (and I'm proud
> of it!)
>
My System SpecsSystem Spec
Old 09-20-2007   #3 (permalink)
Brandon Shell


 
 

Re: Windows XP, Users and Groups

I was just looking through my method... it is an old one (should still
work)... here is a revised version

function Add-UsertoLocalGroup{
Param([string]$user,[string]$group,[string]$server)
If(!($server)){$server = get-content env:COMPUTERNAME}
Write-Host "Adding User [$user] to group [$group]"
Write-Host
"----------------------------------------------" -ForegroundColor gray
$srv = [ADSI]("WinNT://$server,computer")
$u = [ADSI]("WinNT://$server/$User,User")
# Getting Group and Adding User
$g = [ADSI]("WinNT://$server/$group,group")
$g.psbase.invoke("add",$u.psbase.path)
}

"MaciekN" <maciek43360@xxxxxx> wrote in message
news:%23CUh8Y6%23HHA.2268@xxxxxx
Quote:

> I'm trying to write scripts for managing Windows XP's users and groups.
> I can create user:
>
> $ds=[ADSI]"WinNT://host"
> $user=$ds.psbase.Children.Add("new_user","User")
> $user.psbase.CommitChanges()
> $user.SetPassword("new_pass")
> $user.psbase.CommitChanges()
>
> But I can't add user to group.
> I tried this:
>
> $user.PSBase.Children.Add("Administratorzy","Group")
>
> But I got this:
>
> Exception calling "Add" with "2" argument(s): "Obiekt usługi Active
> Directory z
> lokalizowany w ścieżce WinNT://workgroup/host/new_user nie jest
> kontenerem."
> At line:1 char:26
>
> + $user.PSBase.Children.Add( <<<< "Administratorzy","Group")
>
>
> In English: Active Directry object is not a container.
>
> It's horrible! I can't find anywhere how to add user to group. If
> someone can help me, I will be happy.
>
> P.S. I want to apologise for my English. I'm from Poland (and I'm proud
> of it!)
>
My System SpecsSystem Spec
Old 09-20-2007   #4 (permalink)
MaciekN


 
 

Re: Windows XP, Users and Groups

Thanks!
It works!

Brandon Shell pisze:
Quote:

> Two Problems
> One problem is that you need to use the User Path i.e.
> "WinNT://$server/$User"
> Two... you need to add the user via the group object.
>
> Here is my method
> ================
> Param([string]$user,[string]$group,[string]$server)
> If(!($server)){$server = get-content env:COMPUTERNAME}
> Write-Host ""
> Write-Host "Adding User [$user] to group [$group]"
> Write-Host "----------------------------------------------"
> -ForegroundColor gray
> $ary = @()
> $srv = [ADSI]("WinNT://$server,computer")
> $u = [ADSI]("WinNT://$server/$User,User")
> $ary += $u.psbase.path
>
> # Getting Group and Adding User
> $g = [ADSI]("WinNT://$server/$group,group")
> $g.psbase.invoke("add",$ary)
> Write-Host "If no Exception... User Added" -ForegroundColor Green
>
> "MaciekN" <maciek43360@xxxxxx> wrote in message
> news:%23CUh8Y6%23HHA.2268@xxxxxx
Quote:

>> I'm trying to write scripts for managing Windows XP's users and groups.
>> I can create user:
>>
>> $ds=[ADSI]"WinNT://host"
>> $user=$ds.psbase.Children.Add("new_user","User")
>> $user.psbase.CommitChanges()
>> $user.SetPassword("new_pass")
>> $user.psbase.CommitChanges()
>>
>> But I can't add user to group.
>> I tried this:
>>
>> $user.PSBase.Children.Add("Administratorzy","Group")
>>
>> But I got this:
>>
>> Exception calling "Add" with "2" argument(s): "Obiekt usługi Active
>> Directory z
>> lokalizowany w ścieżce WinNT://workgroup/host/new_user nie jest
>> kontenerem."
>> At line:1 char:26
>>
>> + $user.PSBase.Children.Add( <<<< "Administratorzy","Group")
>>
>>
>> In English: Active Directry object is not a container.
>>
>> It's horrible! I can't find anywhere how to add user to group. If
>> someone can help me, I will be happy.
>>
>> P.S. I want to apologise for my English. I'm from Poland (and I'm proud
>> of it!)
>>
>
My System SpecsSystem Spec
Old 09-21-2007   #5 (permalink)
MaciekN


 
 

Re: Windows XP, Users and Groups

I have one more question. How can I retrieve memebers of group?
My System SpecsSystem Spec
Old 09-21-2007   #6 (permalink)
Brandon Shell


 
 

Re: Windows XP, Users and Groups

I wrote this a long time ago... so long someone had to tell me it was on my
blog But here you go
Found Here http://bsonposh.com/modules/wordpress/?page_id=22 along with lots
of other "functions"

#########################################################################
function Get-GroupMembers{
# From: Brandon Shell
# Example:
# -- To List Users of a group
# PS> Get-GroupMembers -group Administrators -server myserver1
# -- To Check if User is member of Group
# PS> Get-GroupMembers -group Administrators -server myserver -user
jsmith
#################################################################
Param([string]$group,[string]$server,[string]$user)

# Check if $server has value. If not set to Local Host Name
If(!($server)){$server = get-content env:COMPUTERNAME}

# Getting Group Object
$g = [ADSI]("WinNT://$server/$group,group")

# Getting Member User Names
$ulist = $g.psbase.invoke("Members") |
%{$_.GetType().InvokeMember("Name", 'GetProperty', $null, $_, $null)}

# If User is specified we check each member for match
if($user)
{
foreach($u in $ulist)
{
if($u -eq $user){$found = $true}
}
if($found){Write-Host "User [$user] Found" -ForegroundColor
green;$true}
else{Write-Host "User [$user] NOT found!" -ForegroundColor
red;$false}
}
else
{ # No user specified... Output Member list
$ulist
}
}

"MaciekN" <maciek43360@xxxxxx> wrote in message
news:uqgsVHB$HHA.320@xxxxxx
Quote:

>I have one more question. How can I retrieve memebers of group?
My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
Count a users groups VB Script
Vista Users and Groups Vista security
Remove Users From All Groups PowerShell
Users&Groups Vista General
Local Users and Groups Vista General


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