【问题标题】:PowerShell - script 1 calls script 2 - how to return value from script 2 to script 1PowerShell - 脚本 1 调用脚本 2 - 如何将值从脚本 2 返回到脚本 1
【发布时间】:2013-08-30 13:47:29
【问题描述】:

我有两个 PowerShell 脚本。一个脚本使用提升的凭据调用另一个 PowerShell 脚本,使用 Start-Process

但我正在努力让第二个脚本将输出值返回给第一个脚本。

这里是脚本 #1,使用 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

当我执行脚本时,输出是

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
}

我愿意使用其他功能和cmdlet,但是脚本自动以提升模式运行是绝对必要的,因为第三方程序正在调用脚本1,它调用脚本2,因此脚本2需要将值发送回脚本 1,用于第三方程序。

(我在问题中发布了更新的问题,ProcessStartInfo and Process in PowerShell - Authentication Error)。

【问题讨论】:

    标签: powershell return-value elevated-privileges start-process


    【解决方案1】:

    您可以使用ProcessStartInfoProcess 来读取标准输出。

    这是你可能会做的一个例子:

    $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
    

    【讨论】:

    • @David Martin 这段代码很有帮助,但我仍然遇到问题。我用更新的代码编辑了我的原始问题。
    • @David Martin 代码中的小东西,你把密码放在用户名属性中
    • @IfediOkonkwo 具体问题是第二个脚本(无法更改)在不同的安全上下文中运行,因此需要跳过所有环节。
    • @IfediOkonkwo 您可以将第二个脚本的结果通过管道传输到第一个脚本中,或者将结果分配给一个变量。例如$newvariable = .\2.ps1 如果你有一个具体的问题,你应该发布一个新问题,我相信你会得到很多回复。
    • 嗯。我喜欢这个$newvariable = .\2.ps1 的想法。我要研究它。谢谢你的麻烦。
    猜你喜欢
    • 2015-09-25
    • 2013-07-16
    • 2016-03-14
    • 1970-01-01
    • 2010-12-20
    • 2016-06-22
    • 2010-11-11
    • 2015-07-07
    • 1970-01-01
    相关资源
    最近更新 更多