Discussion:
PowerShell - iterate two arrays of equal length
Web Admin
2012-12-04 04:40:54 UTC
Permalink
Hi all,

I have to create 150 new SP groups and populate them so decided to use
PowerShell to help automate.

I have two string arrays representing the group/user pairing. What's the PS
equivalent of Python's zip function? Or the C# equivalent for that matter?
:)

$groups = "groupA","groupB"...
$users = "userA","userB"...

foreach (*$group* in $groups)
{
if ($web.SiteGroups[*$group*] -eq $null)
{
#If the SharePoint group doesn't exist already
$newGroup = $web.SiteGroups.Add(*$group*, *$user*, $null, "")
}
}

Regards,

Paul
Luis Ortega
2012-12-04 05:29:16 UTC
Permalink
Hi

Not really fluent in Python, so I just googled for what zip does. If I
undestood correctly, all you need is to index the second array. Like this:

$groups = "groupA","groupB";
$users = "userA","userB";

$index = 0
foreach ($group in $groups)
{
Write-Host("Adding " + $users[$index++] + " to group " + $group);
}

Does this resolve your issue?
Post by Web Admin
Hi all,
I have to create 150 new SP groups and populate them so decided to use
PowerShell to help automate.
I have two string arrays representing the group/user pairing. What's the
PS equivalent of Python's zip function? Or the C# equivalent for that
matter? :)
$groups = "groupA","groupB"...
$users = "userA","userB"...
foreach (*$group* in $groups)
{
if ($web.SiteGroups[*$group*] -eq $null)
{
#If the SharePoint group doesn't exist already
$newGroup = $web.SiteGroups.Add(*$group*, *$user*, $null, "")
}
}
Regards,
Paul
_______________________________________________
ozmoss mailing list
http://prdlxvm0001.codify.net/mailman/listinfo/ozmoss
Web Admin
2012-12-04 21:21:30 UTC
Permalink
Hi Luis, yes to a degree.

I was hoping to do a pair matching and count check on the array lengths
first though. But indexing will work as long as I'm careful.

Thanks.
Post by Luis Ortega
$users[$index++
Web Admin
2012-12-05 01:34:29 UTC
Permalink
OK. I'm really close but have an interesting problem I can't crack.

I'm working with a test sample of only three values for each array.

When I run the script I get the following inexplicable mismatch with
UserLogin and DisplayName values.

UserLogin DisplayName
-------------- -------------------
domain\*user1* User2
domain\user2 *User1*
domain\user3 User3

This is causing the wrong user to go into the wrong group.

*Group1* = user2
Group2 = *user1*
Group3 = user3

Yet the result of Write-Host outputs the correct pairing of user to group!

Adding *user1 *to *Group1*
Adding user2 to Group2
Adding user3 to Group3

I'm scared to try with a larger set of data. Can anyone see where I'm going
wrong here please?

[image: Inline images 3]
Post by Web Admin
Hi Luis, yes to a degree.
I was hoping to do a pair matching and count check on the array lengths
first though. But indexing will work as long as I'm careful.
Thanks.
Post by Luis Ortega
$users[$index++
--
Regards,

Paul Noone

SharePoint Farm Admin/Developer
Infrastructure Team
CEO Sydney

p: (02) 9568 8461
f: (02) 9568 8483
e: ***@syd.catholic.edu.au
w: http://www.ceosyd.catholic.edu.au/
Ivan Wilson
2012-12-05 03:51:49 UTC
Permalink
Try using a "for" loop instead and reference both arrays in the same way.

From: ozmoss-***@ozmoss.com [mailto:ozmoss-***@ozmoss.com] On Behalf Of Web Admin
Sent: Wednesday, 5 December 2012 12:34 PM
To: ozMOSS
Subject: Re: PowerShell - iterate two arrays of equal length

OK. I'm really close but have an interesting problem I can't crack.

I'm working with a test sample of only three values for each array.

When I run the script I get the following inexplicable mismatch with UserLogin and DisplayName values.

UserLogin DisplayName
-------------- -------------------
domain\user1 User2
domain\user2 User1
domain\user3 User3

This is causing the wrong user to go into the wrong group.

Group1 = user2
Group2 = user1
Group3 = user3

Yet the result of Write-Host outputs the correct pairing of user to group!

Adding user1 to Group1
Adding user2 to Group2
Adding user3 to Group3

I'm scared to try with a larger set of data. Can anyone see where I'm going wrong here please?

[Inline images 3]

On 5 December 2012 08:21, Web Admin <***@syd.catholic.edu.au<mailto:***@syd.catholic.edu.au>> wrote:
Hi Luis, yes to a degree.

I was hoping to do a pair matching and count check on the array lengths first though. But indexing will work as long as I'm careful.

Thanks.
On 4 December 2012 16:29, Luis Ortega <***@moonspark.com.au<mailto:***@moonspark.com.au>> wrote:
$users[$index++




--
Regards,

Paul Noone

SharePoint Farm Admin/Developer
Infrastructure Team
CEO Sydney

p: (02) 9568 8461
f: (02) 9568 8483
e: ***@syd.catholic.edu.au<mailto:***@syd.catholic.edu.au>
w: http://www.ceosyd.catholic.edu.au/
Web Admin
2012-12-05 04:28:58 UTC
Permalink
Thanks Ivan. Mission already accomplished. But thank you.

Now I have 150 wiki pages to create in the same manner. But I'll create a
new thread for that.:)
Try using a “for” loop instead and reference both arrays in the same
way. ****
** **
Behalf Of *Web Admin
*Sent:* Wednesday, 5 December 2012 12:34 PM
*To:* ozMOSS
*Subject:* Re: PowerShell - iterate two arrays of equal length****
** **
OK. I'm really close but have an interesting problem I can't crack.****
** **
I'm working with a test sample of only three values for each array.****
** **
When I run the script I get the following inexplicable mismatch with
UserLogin and DisplayName values.****
** **
UserLogin DisplayName****
-------------- -------------------****
domain\*user1* User2****
domain\user2 *User1*****
domain\user3 User3****
** **
This is causing the wrong user to go into the wrong group.****
** **
*Group1* = user2****
Group2 = *user1*****
Group3 = user3****
** **
Yet the result of Write-Host outputs the correct pairing of user to group!
****
** **
Adding *user1 *to *Group1*****
Adding user2 to Group2****
Adding user3 to Group3****
** **
I'm scared to try with a larger set of data. Can anyone see where I'm
going wrong here please?****
** **
[image: Inline images 3]****
** **
****
Hi Luis, yes to a degree.****
** **
I was hoping to do a pair matching and count check on the array lengths
first though. But indexing will work as long as I'm careful.****
** **
Thanks.****
wrote:****
$users[$index++****
** **
****
** **
-- ****
Regards,****
** **
Paul Noone****
** **
SharePoint Farm Admin/Developer
Infrastructure Team
CEO Sydney
p: (02) 9568 8461
f: (02) 9568 8483
w: http://www.ceosyd.catholic.edu.au/****
** **
_______________________________________________
ozmoss mailing list
http://prdlxvm0001.codify.net/mailman/listinfo/ozmoss
--
Regards,

Paul Noone

SharePoint Farm Admin/Developer
Infrastructure Team
CEO Sydney

p: (02) 9568 8461
f: (02) 9568 8483
e: ***@syd.catholic.edu.au
w: http://www.ceosyd.catholic.edu.au/
Loading...