Return from a called script is not working as expected

Hello,

I will try to be has clear as possible, as English is not my mother tongue.

I have 2 scripts, one calling the other one.

The first (the one which is called) is doing a call to a rest api to get some informations from our Issue Managing System.
The second script is doing many stuffs regarding account creation, with a GUI, so I'll not post all the code here.

Here are the samples :

Script 1 (which is working when used directly using powershell on a Windows 2012 R2 ):

$cpt = 0 $results = Invoke-RestMethod -uri $uri -Headers $headers -ContentType "application/json" foreach ($result in $results.issues) { ## Recherche du manager dans ActiveDirectory sans utiliser le module Active Directory $Manager = $result.fields.customfield_10503.name $root = [ADSI]'' $searcher = new-object System.DirectoryServices.DirectorySearcher($root) $searcher.filter = "(&(objectClass=user)(sAMAccountName=$Manager))" $user = $searcher.findall() ##Affichage de principe pour voir ce que l'on trouve Write-Host "`n" Write-Host "Ticket CSI : $($result.key)" Write-Host "Interne ou Presta ? : $($result.fields.issuetype.name)" Write-Host "Manager : $($result.fields.customfield_10503.name)" Write-Host "Login trouv dans l'AD : $($user.properties.samaccountname)" Write-Host "Prnom : $($result.fields.customfield_10495)" Write-Host "Nom : $($result.fields.customfield_10494)" Write-Host "Fonction : $($result.fields.customfield_10502)" Write-Host "Socit : $($result.fields.customfield_10535.value)" Write-Host "Service : $($result.fields.customfield_16244.value)" Write-Host "Service bis : $($result.fields.customfield_16244.child.value)" Write-Host "Site : $($result.fields.customfield_10453.value)" Write-host "Date de fin de contrat : $($Result.fields.customfield_10501)" Write-Host "Type de contrat : $($result.fields.customfield_10498.value)" Write-Host "Site : $($result.fields.customfield_11441.value)" }

return $results (#used only when I am calling it from the second script)

The result is like this when executed directly (there are many results) :

Ticket CSI : TicketNumber
Interne ou Presta ? : Arrive Interne
Manager : Login_Login
Login trouv dans l'AD : Login_Login
Prnom : Frdric
Nom : XXXXXXXXXX
Fonction : Intitul du poste
Socit : XXXXXXXXXX
Service : XXXXXXXXXX
Service bis : XXXXXXXXXX
Site : XXXXXXXXXX
Date de fin de contrat : 2015-08-28
Type de contrat : CDD
Site : XXXXXXXXXX



Script 2 (it is executed from the Exchange Management Shell for Exchange 2010, on the same server, so from a PS V2 version, that is why I need to start a new powershell session, to have the JIRA_CONNECTION.ps1 working because of theInvoke-RestMethod :

$temps = powershell.exe -file D:\SCRIPTS\JIRA_Connection\JIRA_CONNECTION.ps1

write-host "t0"
 $temps

foreach ($result in $temps.issues )
{
    ## Recherche du manager dans ActiveDirectory sans utiliser le module Active Directory
    $Manager = $result.fields.customfield_10503.name
    $root = [ADSI]''
    $searcher = new-object System.DirectoryServices.DirectorySearcher($root)
    $searcher.filter = "(&(objectClass=user)(sAMAccountName=$Manager))"
    $user = $searcher.findall()
    
    ## Affichage de principe pour voir ce que l'on trouve
    Write-Host "`n"
    Write-Host "Ticket CSI : $($result.key)"
    Write-Host "Interne ou Presta ? : $($result.fields.issuetype.name)"
    Write-Host "Manager : $($result.fields.customfield_10503.name)"
    Write-Host "Login trouv dans l'AD : $($user.properties.samaccountname)"
    Write-Host "Prnom : $($result.fields.customfield_10495)"
    Write-Host "Nom : $($result.fields.customfield_10494)"
    Write-Host "Fonction : $($result.fields.customfield_10502)"
    Write-Host "Socit : $($result.fields.customfield_10535.value)"
    Write-Host "Service : $($result.fields.customfield_16244.value)"
    Write-Host "Service bis : $($result.fields.customfield_16244.child.value)"
    Write-Host "Site : $($result.fields.customfield_10453.value)"
    Write-host "Date de fin de contrat : $($Result.fields.customfield_10501)"
    Write-Host "Type de contrat : $($result.fields.customfield_10498.value)"
    
}


The result of the execution is looking like that :

t0


expand     : schema,names
startAt    : 0
maxResults : 50
total      : 11
issues     : {@{expand=operations,editmeta,changelog,transitions,renderedFields; id=266037; self=https://servicedesk.vsct.fr/rest/api/2/issue/266037; key=ADVII-65;
             fields=}, @{expand=operations,editmeta,changelog,transitions,renderedFields; id=265450; self=https://servicedesk.vsct.fr/rest/api/2/issue/265450;
             key=ADVII-57; fields=}, @{expand=operations,editmeta,changelog,transitions,renderedFields; id=264907;
             self=https://servicedesk.vsct.fr/rest/api/2/issue/264907; key=ADVII-52; fields=}, @{expand=operations,editmeta,changelog,transitions,renderedFields;
             id=264899; self=https://servicedesk.vsct.fr/rest/api/2/issue/264899; key=ADVII-51; fields=}...}





Ticket CSI :
Interne ou Presta ? :
Manager :
Login trouv dans l'AD :
Prnom :
Nom :
Fonction :
Socit :
Service :
Service bis :
Site :
Date de fin de contrat :
Type de contrat :

All the fields are empty and it only display once when entering in the foreach.

Just to compare, here is the result when I display $ Results from the first script

@{expand=schema,names; startAt=0; maxResults=50; total=11; issues=System.Object[]}


expand     : schema,names
startAt    : 0
maxResults : 50
total      : 11
issues     : {@{expand=operations,editmeta,changelog,transitions,renderedFields; id=266037;
             self=https://servicedesk.vsct.fr/rest/api/2/issue/266037; key=ADVII-65; fields=},
             @{expand=operations,editmeta,changelog,transitions,renderedFields; id=265450;
             self=https://servicedesk.vsct.fr/rest/api/2/issue/265450; key=ADVII-57; fields=},
             @{expand=operations,editmeta,changelog,transitions,renderedFields; id=264907;
             self=https://servicedesk.vsct.fr/rest/api/2/issue/264907; key=ADVII-52; fields=},
             @{expand=operations,editmeta,changelog,transitions,renderedFields; id=264899;
             self=https://servicedesk.vsct.fr/rest/api/2/issue/264899; key=ADVII-51; fields=}...}

I don't know what i am missing, or if that is just impossible to do this...

What i have noticed is the fact that the issues array, when it is returned to the calling script, it displays like if it was just a string instead of an array.
I you look well, there is always a Return which make the lines starts with {@{expand=, when i am using the script from the V3 PS version, which seems to not be the case when it is returned...

Thanks in advance for your help.

Pierre-Yves

August 19th, 2015 10:23am

This is "wrong" - not what you should be doing (not sure why you think you need to do this):

Free Windows Admin Tool Kit Click here and download it now
August 19th, 2015 10:32am

Hello,

I usually do what you recommended, but here the problem is coming from the fact that I am using a cmdlet running only under Ps version V3 and more, and that i am using the ps version 2 for my GUI (because of the Exchange CMDLET).

So i am getting this error when trying your idea :

Invoke-RestMethod : The term 'Invoke-RestMethod' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelli
r if a path was included, verify that the path is correct and try again.
At D:\SCRIPTS\JIRA_Connection\JIRA_CONNECTION.ps1:63 char:29
+ $results = Invoke-RestMethod <<<<  -uri $uri -Headers $headers -ContentType "application/json"
    + CategoryInfo          : ObjectNotFound: (Invoke-RestMethod:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundExc

August 19th, 2015 10:45am

I go a little bit further and here is what i found :

If i add that in my script, (i stopped at t19) :

write-host "t16"
 $temps[6]
 write-host "t17"
 $temps[7]
 write-host "t18"
 $temps[8]
 write-host "t19"
 $temps[9]
write-host "t20"
 $temps[10]
 write-host "t21"
 $temps[11]
write-host "t22"
 $temps[12]
 write-host "t23"
 $temps[13]
 write-host "t24"
 $temps[14]

Here is the result, we can see the ... at the end, so there is not all the information, i can't work with this values :

t16
issues     : {@{expand=operations,editmeta,changelog,transitions,renderedFields; id=266037; self=https://servicedesk.vsct.fr/rest/api/2/
t17
             fields=}, @{expand=operations,editmeta,changelog,transitions,renderedFields; id=265450; self=https://servicedesk.vsct.fr/re
t18
             key=ADVII-57; fields=}, @{expand=operations,editmeta,changelog,transitions,renderedFields; id=264907;
t19
             self=https://servicedesk.vsct.fr/rest/api/2/issue/264907; key=ADVII-52; fields=}, @{expand=operations,editmeta,changelog,tr
t20
             id=264899; self=https://servicedesk.vsct.fr/rest/api/2/issue/264899; key=ADVII-51; fields=}...}
t21

t22

t23

t24

That is really a strange thing for me.
I hope you will be able to help me.

If i found something, i'll let you know.

Free Windows Admin Tool Kit Click here and download it now
August 19th, 2015 11:06am

My recommendation is to write a very short sample script that contains only the absolute minimum amount of code needed to reproduce the problem and start with that.

August 19th, 2015 11:16am

I have already minimise the amout of code i need.

The only question is :
Is it possible to return an object to the script that called the other script by using this method :

$temps = powershell.exe -file D:\SCRIPTS\JIRA_Connection\JIRA_CONNECTION.ps1

If that is impossible, i will write into a csv file to read it after, but I would have prefer to use an object.

Free Windows Admin Tool Kit Click here and download it now
August 19th, 2015 12:29pm

Is it possible to return an object to the script that called the other script by using this method :

$temps = powershell.exe -file D:\SCRIPTS\JIRA_Connection\JIRA_CONNECTION.ps1

The direct answer to this question is no. If you run powershell.exe -file, your $temps variable will contain an array of strings containing the textual output of that script. It will not be objects.

August 19th, 2015 1:35pm

Hello,

Ok, so i will create a csv file to manage this point.

I will maybe, be able to go back to an objet if Exchange 2013 is working on Powershell V3 or more.

Thanks again for your help :).

Free Windows Admin Tool Kit Click here and download it now
August 20th, 2015 2:06am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics