question

Michal-3596 avatar image
0 Votes"
Michal-3596 asked StefanHorz edited

System Center Orchestrator 2019 + Powershell script = problem with passing hash table properties

Hello everyone,

I'm trying to orchestrate user creation in SC Orchestrator.
Workflow would look like this:

PS Script to collect new user data > For each found user create Service Manager request > Collect data from Service Manager and pass them to Create user activity in Orchestrator

1st step is done and main code block for output looks like this:

 if ($user_GC -ne $null) {
    
  $output = foreach ($user in $user_GC) {
     
   $properties = @{
     'FirstName' = $user.GivenName
     'LastName' = $user.Surname.Substring(0,1)+($user.Surname.Substring(1).tolower())
     'Email' = $user.GABCustomUser3
     'Department' = $user.Department
     'JobTitle' = $user.Title
     'Organization' = 'test'
     'GUID'= ($user.GivenName.ToLower()).Substring(0,1)+'.'+$user.Surname.ToLower()
     }
     New-Object -Type psobject -Property $properties
     $NewUserExists = $true
  }
  }

Output of $output variable in Orchestrator looks like this:
@{Department=; Organization=test; JobTitle=; LastName=Test; Email=testing@test.com; FirstName=test; GUID=t.test}

In Powershell I can look for found user properties by calling for example $output.FirstName but in Orchestrator it simply fails with information that such variable does not exist.

Could you please help me with this one?

windows-server-powershellmsc-orchestrator
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

AndreasBaumgarten avatar image
0 Votes"
AndreasBaumgarten answered AndreasBaumgarten edited

Hi @Michal-3596 ,

in general: If the variable does not contain a value the variable will not be available in Published Data

I can't see your full script in the screenshot so I could only guess:
The definition of all the individual variables are "inside" the $result part? If so, this doesn't work.
Take a look on my script I posted above. The individual variables are set outside the $result .

Just guessing:
The variables should be set between the curly bracket in line 39 and 40 and not before line 39.

If this is not the case please post the full script here. Otherwise it's only "guessing" how your script might looking.


(If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)

Regards
Andreas Baumgarten


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

AndreasBaumgarten avatar image
0 Votes"
AndreasBaumgarten answered AndreasBaumgarten commented

Hi @Michal-3596 ,

have you tried to use a different name for the variable $output ? For instance $result or $outputUser ... anything different than $output.


(If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)

Regards
Andreas Baumgarten

· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hello @AndreasBaumgarten
Unfortunetly still the same: "The property 'FirstName' cannot be found on this object. Verify that the property exists and can be set."

0 Votes 0 ·

Hi @Michal-3596 ,

without knowing how $user_GC looks like it's hard to test your code in Orchestrator. Sorry.

What do you mean with "but in Orchestrator it simply fails with information that such variable does not exist"?
I don't understand what you would like to achieve, what you expect and what is next in Orchestrator.

**----------
(If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)

Regards
Andreas Baumgarten**





0 Votes 0 ·
Michal-3596 avatar image
0 Votes"
Michal-3596 answered

@AndreasBaumgarten

Whole code looks like this:

 Import-Module ActiveDirectory
     $dateforlookup = (Get-Date).AddDays(-1)
        
     $DC_GC = "DC01"
     $DC_PL = "DC02"
        
     $OU_GC = "1st distinguished name of OU"
     $OU_PL = "2nd distinguished name of OU"
        
     $user_PL = Get-ADUser -Server $DC_PL -SearchBase $OU_PL -Filter * -Properties mail
     $user_GC = Get-ADUser -Server $DC_GC -SearchBase $OU_GC -Filter { whenCreated -gt $dateforlookup } -Properties GivenName,Surname,GABCustomUser3,whenCreated,Title,Department | Where {$_.GABCustomUser3 -notin $user_PL.mail} 
        
     if ($user_GC -ne $null) {
        
         foreach ($user in $user_GC) {
        
         $result = @{ 
         $Fname = $user.GivenName
         $Lname = $user.Surname.Substring(0,1)+($user.Surname.Substring(1).tolower())
         #'Email' = $user.GABCustomUser3
         #'Department' = $user.Department
         #'JobTitle' = $user.Title
         #'Organization' = 'test'
         #'GUID'= ($user.GivenName.ToLower()).Substring(0,1)+'.'+$user.Surname.ToLower()
         }
         $NewUserExists = $true
         }
       }

To sums this up. I need to know how to subscribe to data from powershell output in my runbook.
For example in this case I would like to send e-mail and in it's subject there would be GUID of each new user that was found in my script.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

AndreasBaumgarten avatar image
0 Votes"
AndreasBaumgarten answered

Hi @Michal-3596 ,

In general: Every variable of a PowerShell script needs to be published by the Run .Net Script activity separated. If you publish the variable $result it will contain just the information it is a System.Collections.Hashtable . You can't use $result.email in later Runbook activities

If you want the variables $Fname, $Email, $GUID separated you have to publish all these variables separated in the Run .Net Scriptactivity.

Easy script for testing:

 $result = @{
 Fname = 'Peter'
 Lname = 'Pan'
 Email = 'peter.pan@test.local'
 GUID = 'peter.pan'
 }
    
 $Firstname = 'Peter'
 $Lastname = 'Pan'
 $EmailAddress = 'peter.pan@test.local'
 $userGUID = 'peter.pan'


This it looks like in Orchestrator:

98555-image.png


98440-image.png



(If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)

Regards
Andreas Baumgarten



image.png (78.7 KiB)
image.png (75.4 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Michal-3596 avatar image
0 Votes"
Michal-3596 answered

Dear @AndreasBaumgarten

I've tried to separate each value in my script:


98574-11.png


And here it is in Orchestrator:

98597-22.png




Problem is in runbook tester I don't see these variables. I tried to pass e-mail to get user activity but it crashes (it works when i type e-mail as plain text)


11.png (105.7 KiB)
22.png (46.2 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

StefanHorz avatar image
0 Votes"
StefanHorz answered StefanHorz edited

Hi @Michal-3596 ,
yes, you must paste every single Variable without formatting in the Published Data tab. I would modify your script like below:

 Import-Module ActiveDirectory
 $dateforlookup = (Get-Date).AddDays(-1)
            
 $DC_GC = "DC01"
 $DC_PL = "DC02"
            
 $OU_GC = "1st distinguished name of OU"
 $OU_PL = "2nd distinguished name of OU"
            
 $user_PL = Get-ADUser -Server $DC_PL -SearchBase $OU_PL -Filter * -Properties mail
 [array]$user_GC = Get-ADUser -Server $DC_GC -SearchBase $OU_GC -Filter { whenCreated -gt $dateforlookup } -Properties GivenName,Surname,GABCustomUser3,whenCreated,Title,Department | Where {$_.GABCustomUser3 -notin $user_PL.mail} 
            
 if ($user_GC.count -gt 0) {
            
     $NewUserExists = $true
    
     $Fname = @()
     $GUID = @()
     $Lname = @()
     $Email = @()
     $Department = @()
     $JobTitle = @()
     $Organization = @()
    
    
 foreach ($user in $user_GC) {
              
     $Fname += $user.GivenName
     $GUID += ($user.GivenName.ToLower()).Substring(0,1)+'.'+$user.Surname.ToLower()
     $Lname += $user.Surname.Substring(0,1)+($user.Surname.Substring(1).tolower())
     $Email += $user.GABCustomUser3
     $Department += $user.Department
     $JobTitle += $user.Title
     $Organization += 'test'
     }
             
 }


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Michal-3596 avatar image
0 Votes"
Michal-3596 answered

Hi @AndreasBaumgarten @StefanHorz

Thank you so much for your help, indeed I needed to collect informations outside of main for_each and collect them into separate variables, now it works just how I wanted to :)

I would need to integrate it further with Service Manager but I think I will create another question if I hit the wall again.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.