首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >PowerShell -脚本1调用脚本2-如何从脚本2返回值到脚本1

PowerShell -脚本1调用脚本2-如何从脚本2返回值到脚本1
EN

Stack Overflow用户
提问于 2013-08-30 13:47:29
回答 1查看 14.9K关注 0票数 2

我有两个PowerShell脚本。一个脚本使用提升的凭据调用另一个PowerShell脚本,使用启动过程

但是,我很难让第二个脚本将输出值返回给第一个脚本。

下面是脚本# 1,它使用script1.psl“发送方-ip=10.10.10.10”调用。

代码语言:javascript
复制
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

当我执行脚本时,输出是

代码语言:javascript
复制
Output is

下面是script2,它将所需的值输出到cmd窗口,而不是将该值返回到脚本1:

代码语言:javascript
复制
$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,脚本1转到第三方程序。

(我在一个问题( https://stackoverflow.com/questions/18540505)中发布了更新的问题)。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-08-30 14:26:42

您可以使用ProcessStartInfo流程,这将允许您读取StandardOutput。

下面是你可能做的事情的一个例子:

代码语言:javascript
复制
$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
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18534500

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档