Roland,

 

I would recommend changing your approach to solve for this. You can
kind-sorta pass objects from one PowerShell Runspace to another by
serializing and then deserializing the objects. Rather than use an array to
represent the 1) Group, 2) Role, 3) Scope, I would recommend using a custom
PowerShell object (aka. [PSCustomObject]), which is easily created through a
HashTable. You can create an array of those objects, and then serialize them
to disk, similar to the following example:

 

################

# Script #1

################

 

# 1. Create empty array to hold ConfigMgr roles

$CM_Roles = @();

 

# 2. Add custom object to Array for FullAdministrators

$CM_Roles += [PSCustomObject]@{

    GroupName = 'CM_Global_Administration';

    Role = 'FullAdministrator';

    Scope = 'ALL';

    };

 

# 3. Add custom object to Array for ApplicationAdministrators

$CM_Roles += [PSCustomObject]@{

    GroupName = 'CM_Global_Packagers';

    Role = 'ApplicationAdministrator';

    Scope = 'ALL';

    };

 

# 4. Persist the objects to disk

Export-Clixml -Path "$Home\ConfigMgr Roles.xml" -InputObject $CM_Roles;

 

################

# Script #2

################

 

# 5. Import the persisted objects

$CM_Roles = Import-Clixml -Path "$Home\ConfigMgr Roles.xml";

 

# 6. Do stuff here

foreach ($Role in $CM_Roles) {

    Write-Host -Object ('Adding group ({0}) to role ({1}) with scope ({2})'
-f $Role.GroupName, $Role.Role, $Role.Scope);

} 

 

Cheers,

Trevor Sullivan

 

From: [email protected] [mailto:[email protected]]
On Behalf Of Roland Janus
Sent: Thursday, October 31, 2013 12:06 PM
To: [email protected]
Subject: RE: [mssms] OT: Powershell pass array help

 

Really? Powershell is not that almighty after all :)

 

That can't be true in general.

I do that already, but without any additional separators and especially no
blanks.

 

This would work:

 

$CM_Roles =
@("CM_Global_Administration","FullAdministrator","ALL","CM_Global_Packagers"
,"ApplicationAdministrator","ALL")

 

Every entry is an array entry then:

 

CM_Global_Administration

FullAdministrator

ALL

CM_Global_Packagers

ApplicationAdministrator

ALL

 

But as soon as there is a blank in there I get this again.

$CM_Roles = @("CM_Global_Administration","Full
Administrator","ALL","CM_Global_Packagers","Application
Administrator","ALL")

 

CM_Global_Administration

Full

Administrator

ALL

CM_Global_Packagers

Application

Administrator

ALL

 

I'd like to use this:

$CM_Roles = @("CM_Global_Administration,Full
Administrator,ALL","CM_Global_Packagers,Application Administrator,ALL")

 

Why aren't the commas and the blanks put into single array entry just as a
string?

 

"CM_Global_Administration,Full Administrator,ALL"

"CM_Global_Packagers,Application Administrator,ALL"

 

-R

 

 

 

From: [email protected] <mailto:[email protected]>
[mailto:[email protected]] On Behalf Of Trevor Sullivan
Sent: Donnerstag, 31. Oktober 2013 14:14
To: [email protected] <mailto:[email protected]> 
Subject: RE: [mssms] OT: Powershell pass array help

 

Roland,

 

You can't pass a .NET [Object] from one PowerShell session to another (not
without some advanced hacking, anyway). To make this work, you'll probably
have to -join or [String]::Join() the array values together into a single,
contiguous string, pass the entire string as a single parameter to the
secondary script, and then -Split or [String].Split() the String into an
array again.

 

Cheers,

Trevor Sullivan

 

From: [email protected] <mailto:[email protected]>
[mailto:[email protected]] On Behalf Of Roland Janus
Sent: Thursday, October 31, 2013 5:52 AM
To: [email protected] <mailto:[email protected]> 
Subject: [mssms] OT: Powershell pass array help

 

Not completely off-topic, trying to use CM12 cmdlets.

 

I have an array:

 

$CM_Roles = @("'CM_Global_Administration','Full
Administrator','ALL'";"'CM_Global_Packagers','Application
Administrator','ALL'")

Two entries, separated with ";" and each one separated with ","

 

This works fine: foreach ($x in $CM_Roles) {$x}

'CM_Global_Administration','Full Administrator','ALL'

'CM_Global_Packagers','Application Administrator','ALL'

 

I have the single quotes and can use split to get another array:

 

foreach ($x in $CM_Roles) {$x -split ","}

 

'CM_Global_Administration'

'Full Administrator'

'ALL'

'CM_Global_Packagers'

'Application Administrator'

'ALL'

 

All good. Still with quotes.

 

But I need to pass the array to another ps-script, namely an x86 like that:

 

&"$env:SystemRoot\syswow64\WindowsPowerShell\v1.0\powershell.exe"
".\CM_Assign_Security.ps1" @cm_roles

 

But the script with this in it:

 

Foreach ($x in $args) {$x}

 

only returns the values without the single quotes or ',':

 

CM_Global_Administration Full Administrator ALL

CM_Global_Packagers Application Administrator ALL

 

I've also tried like a million variations, like with param() in the script.
Then I only get one array entry back.

 

For sure someone knows how to do that? And it is probably trivial, but
google wasn't helping.

Those examples never had to use quotes.

 

-R

 

 

 

 



Reply via email to