3

我有兩個PowerShell腳本。一個腳本使用提升的憑據調用另一個PowerShell腳本,使用Start-ProcessPowerShell - 腳本1調用腳本2 - 如何將腳本2中的值返回到腳本1

但我正在努力如何使第二個腳本返回到第一個腳本的輸出值。

這裏是腳本#1,其被調用,script1.psl 「發送器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 

當我執行該腳本,則輸出是

Output is 

而這裏是script2,它將所需的值輸出到cmd窗口,而不是將值返回給腳本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 
} 

我是wi使用其他函數和cmdlet,但絕對有必要使腳本自動運行在提升模式下,因爲第三方程序正在調用腳本1,該腳本調用腳本2,因此腳本2需要將值發送回腳本1,到第三方程序。

(我在問題中發佈了更新的問題,ProcessStartInfo and Process in PowerShell - Authentication Error)。

回答

2

您可以使用ProcessStartInfoProcess這將允許您閱讀StandardOutput。

這裏是你可以做什麼的例子:

$startInfo = New-Object System.Diagnostics.ProcessStartInfo 
$startInfo.FileName = "powershell.exe" 
$startInfo.Arguments = "C:\script\script2.ps1" 

$startInfo.RedirectStandardOutput = $true 
$startInfo.UseShellExecute = $false 
$startInfo.CreateNoWindow = $false 
$startInfo.Username = "DOMAIN\Username" 
$startInfo.Password = $password 

$process = New-Object System.Diagnostics.Process 
$process.StartInfo = $startInfo 
$process.Start() | Out-Null 
$standardOut = $process.StandardOutput.ReadToEnd() 
$process.WaitForExit() 

# $standardOut should contain the results of "C:\script\script2.ps1" 
$standardOut 
+0

@大衛·馬丁這個代碼是非常有用的,但我仍然有麻煩。我使用更新的代碼編輯了我的原始問題。 – Glowie

+1

@ David Martin在密碼中輸入用戶名屬性的小小東西 – Bostwick

+0

你在開玩笑吧!所以,爲了將script2中的值返回給script1,我真的需要完成所有這些工作,憑證傳遞和所有這些?犛牛!我寧願開始將腳本轉換爲函數。 –

相關問題