PowerShell: el script 1 llama al script 2: cómo devolver el valor del script 2 al script 1

Tengo dos scripts de PowerShell. Una secuencia de comandos invoca otra secuencia de comandos de PowerShell utilizando credenciales elevadas, utilizandoProceso de inicio.

Pero estoy luchando con cómo hacer que la segunda secuencia de comandos devuelva el valor de salida a la primera secuencia de comandos.

Aquí está el script # 1, que se invoca con script1.psl "sender-ip = 10.10.10.10"

function getUser($abc) {

    <#Previously created password file in C:\Script\cred.txt, read-host -assecurestring | convertfrom-securestring | out-file C:\Script\cred.txt#>

    $password = get-content C:\Script\cred.txt| convertto-securestring
    $credentials = new-object -typename System.Management.Automation.PSCredential -argumentlist "DOMAIN\Username",$password
    $output = start-process powershell -Credential $credentials -ArgumentList '-noexit','-File', 'C:\script\script2.ps1', $abc
    return $output
}

[string]$abc = $args

getUser($abc)

Write-host Output is $output

Cuando ejecuto el script, la salida es

Output is

Y aquí está script2, que genera el valor deseado en una ventana cmd en lugar de devolver el valor al script 1:

$userID = $NULL
$line_array = @()
$multi_array = @()
[hashtable]$my_hash = @{}

foreach ($i in $args) {
   $line_array+= $i.split(" ")
}

foreach ($j in $line_array) {
    $multi_array += ,@($j.split("="))
}

foreach ($k in $multi_array) {
    $my_hash.add($k[0],$k[1])
}

$Sender_IP = $my_hash.Get_Item("sender-ip")


<# Courtesy of http://blogs.technet.com/b/heyscriptingguy/archive/2012/02/19/use-powershell-to-find-last-logon-times-for-virtual-workstations.aspx#>

<# Gather information on the computer corresponding to $Sender_IP #>
$Win32OS = Get-WmiObject -Class Win32_OperatingSystem -ComputerName $Sender_IP

<# Determine the build number #>
$Build = $Win32OS.BuildNumber


<# Running Windows Vista with SP1 and later, i.e. $Build is greater than or equal to 6001 #>
if ($Build -ge 6001) {
    $Win32User = Get-WmiObject -Class Win32_UserProfile -ComputerName $Sender_IP
    $Win32User = $Win32User | Sort-Object -Property LastUseTime -Descending
    $LastUser = $Win32User | Select-Object -First 1
    $UserSID = New-Object System.Security.Principal.SecurityIdentifier($LastUser.SID)
    $userId = $UserSID.Translate([System.Security.Principal.NTAccount])
    $userId = $userId.Value
}

<# Running Windows Vista without SP1 and earlier, i.e $Build is less than or equal to 6000 #>
elseif ($Build -le 6000) {
    $SysDrv = $Win32OS.SystemDrive
    $SysDrv = $SysDrv.Replace(":","$")
    $ProfDrv = "\\" + $Sender_IP + "\" + $SysDrv
    $ProfLoc = Join-Path -Path $ProfDrv -ChildPath "Documents and Settings"
    $Profiles = Get-ChildItem -Path $ProfLoc
    $LastProf = $Profiles | ForEach-Object -Process {$_.GetFiles("ntuser.dat.LOG")}
    $LastProf = $LastProf | Sort-Object -Property LastWriteTime -Descending | Select-Object -First 1
    $userId = $LastProf.DirectoryName.Replace("$ProfLoc","").Trim("\").ToUpper()
}

else{
    $userId = "Unknown/UserID"
}


# Logic block to return the correct value
if ($userId -ne $NULL) {
    return "userId=" + $userId
}
elseif ($userID -eq $NULL)
{
    return "userId=" + $userId
}

Estoy dispuesto a usar otras funciones y cmdlets, pero es absolutamente necesario que la secuencia de comandos se ejecute automáticamente en modo elevado porque un programa de terceros está llamando a la secuencia de comandos 1, que llama a la secuencia de comandos 2, por lo que la secuencia de comandos 2 debe enviar valor al script 1, que va al programa de terceros.

(Publiqué una pregunta actualizada en una pregunta,ProcessStartInfo y proceso en PowerShell: error de autenticación).

Respuestas a la pregunta(1)

Su respuesta a la pregunta