父Powershell脚本没有';t在Azure管道中打印来自子脚本的消息

父Powershell脚本没有';t在Azure管道中打印来自子脚本的消息,powershell,azure-devops,azure-pipelines,Powershell,Azure Devops,Azure Pipelines,我们有一个Powershell脚本(a),它正在作为另一个用户(域用户)执行另一个Powershell脚本(B)。子脚本(B)对域中不同服务器上的数据库执行一系列SQL查询。当子脚本(B)执行时,它使用写入主机打印多个状态消息,然后这些状态消息又被父脚本(A)捕获并打印到控制台。当我在开发机器上(在同一域中)从Powershell提示符手动执行父脚本(A)时,这非常有效。我还从子脚本(B)获取所有输出 下面是父脚本(A)中执行子脚本(B)的部分代码 try{ $ProcessInfo =

我们有一个Powershell脚本(a),它正在作为另一个用户(域用户)执行另一个Powershell脚本(B)。子脚本(B)对域中不同服务器上的数据库执行一系列SQL查询。当子脚本(B)执行时,它使用
写入主机打印多个状态消息,然后这些状态消息又被父脚本(A)捕获并打印到控制台。当我在开发机器上(在同一域中)从Powershell提示符手动执行父脚本(A)时,这非常有效。我还从子脚本(B)获取所有输出

下面是父脚本(A)中执行子脚本(B)的部分代码

try{
    $ProcessInfo = New-Object System.Diagnostics.ProcessStartInfo 
    $ProcessInfo.FileName = "powershell.exe"
    $ProcessInfo.Domain = "jmres"
    $ProcessInfo.UserName = $username
    $ProcessInfo.Password = ConvertTo-SecureString $password -AsPlainText -Force
    $ProcessInfo.RedirectStandardError = $true 
    $ProcessInfo.RedirectStandardOutput = $true 
    $ProcessInfo.UseShellExecute = $false 
    $ProcessInfo.Arguments = $file, $fileArguments 
    $Process = New-Object System.Diagnostics.Process 
    $Process.StartInfo = $ProcessInfo
    $Process.Start() | Out-Null
    $stdOutput = $Process.StandardOutput.ReadToEnd()
    $stdError = $Process.StandardError.ReadToEnd() 
    Write-Host $stdOutput
    Write-Host $stdError
    $Process.WaitForExit()
}
catch{
    Write-Host "Could not execute script."
    Write-Error $Error[0]
    Exit
}
现在,在Azure发布管道中,我们在部署应用程序时通过Powershell任务自动执行父脚本(A)。子脚本(B)作为指定用户运行良好,我可以验证SQL查询是否针对给定的SQL server正确执行

好的。因此,这里的问题是,来自父脚本(A)的状态消息被打印到管道中的Powershell控制台。但是来自子脚本(B)的状态消息不可用。我已经挣扎了好几天,现在试图找出原因

我尝试过的事情:

  • 将子脚本(B)中的
    Write Host
    替换为
    Echo
    ,以将输出重定向到管道,而不是直接重定向到控制台
  • 尝试仅打印
    StandardOutput
    流,并忽略
    StandardError
    流,以避免出现所述的潜在死锁
  • 已尝试关闭(设置
    $false
    )对
    标准输出
    标准错误
    流的重定向,并将输出发送到子脚本(B)自己的控制台窗口,然后希望它显示在Azure管道控制台中
什么都不管用。Azure管道控制台中的输出是两行空行,我希望在其中显示子脚本(B)的输出。到目前为止,我的发现是,问题在于这两条线

$stdOutput = $Process.StandardOutput.ReadToEnd()
$stdError = $Process.StandardError.ReadToEnd()
这两个变量是空的。为什么
ReadToEnd()
没有从流中获取任何内容?为什么从Powershell提示符手动执行而不是在Azure管道中执行时它可以工作?有人知道下一步该做什么吗

更新1(重要): 所以这很尴尬。今天早上我发现了我的一个大脑袋屁。我想我已经验证过了,SQL查询在哪里对数据库进行了攻击。我错了。子脚本(B)根本不执行。因此,当从Azure管道执行父脚本(A)时,不会对数据库触发SQL查询。这就是为什么我得不到输出。奇怪的是,当我从Powershell提示符手动执行父脚本(A)时,它会工作

这显然是一个关于执行脚本时的用户上下文的问题,而不是像我所想的那样,捕获子脚本的输出(B)。我不知道是否要删除这个问题。管理员必须做出决定。

请检查是否在管道中打印到控制台。例如:

设置变量:

- pwsh: |
    Write-Host "##vso[task.setvariable variable=sauce;]crushed tomatoes"
    Write-Host "##vso[task.setvariable variable=secretSauce;issecret=true]crushed tomatoes with garlic"
    Write-Host "##vso[task.setvariable variable=outputSauce;isoutput=true]canned goods"
  name: SetVars
- pwsh: |
    Write-Host "Non-secrets automatically mapped in, sauce is $env:SAUCE"
    Write-Host "Secrets are not automatically mapped in, secretSauce is $env:SECRETSAUCE"
    Write-Host "You can use macro replacement to get secrets, and they'll be masked in the log: $(secretSauce)"
    Write-Host "Future jobs can also see $env:SETVARS_OUTPUTSAUCE"
    write-Host "Future jobs can also see $(SetVars.outputSauce)"
阅读变量:

- pwsh: |
    Write-Host "##vso[task.setvariable variable=sauce;]crushed tomatoes"
    Write-Host "##vso[task.setvariable variable=secretSauce;issecret=true]crushed tomatoes with garlic"
    Write-Host "##vso[task.setvariable variable=outputSauce;isoutput=true]canned goods"
  name: SetVars
- pwsh: |
    Write-Host "Non-secrets automatically mapped in, sauce is $env:SAUCE"
    Write-Host "Secrets are not automatically mapped in, secretSauce is $env:SECRETSAUCE"
    Write-Host "You can use macro replacement to get secrets, and they'll be masked in the log: $(secretSauce)"
    Write-Host "Future jobs can also see $env:SETVARS_OUTPUTSAUCE"
    write-Host "Future jobs can also see $(SetVars.outputSauce)"
控制台输出:

Non-secrets automatically mapped in, sauce is crushed tomatoes
Secrets are not automatically mapped in, secretSauce is 
You can use macro replacement to get secrets, and they'll be masked in the log: ***
Future jobs can also see canned goods
Future jobs can also see canned goods
请检查以打印到管道中的控制台。例如:

设置变量:

- pwsh: |
    Write-Host "##vso[task.setvariable variable=sauce;]crushed tomatoes"
    Write-Host "##vso[task.setvariable variable=secretSauce;issecret=true]crushed tomatoes with garlic"
    Write-Host "##vso[task.setvariable variable=outputSauce;isoutput=true]canned goods"
  name: SetVars
- pwsh: |
    Write-Host "Non-secrets automatically mapped in, sauce is $env:SAUCE"
    Write-Host "Secrets are not automatically mapped in, secretSauce is $env:SECRETSAUCE"
    Write-Host "You can use macro replacement to get secrets, and they'll be masked in the log: $(secretSauce)"
    Write-Host "Future jobs can also see $env:SETVARS_OUTPUTSAUCE"
    write-Host "Future jobs can also see $(SetVars.outputSauce)"
阅读变量:

- pwsh: |
    Write-Host "##vso[task.setvariable variable=sauce;]crushed tomatoes"
    Write-Host "##vso[task.setvariable variable=secretSauce;issecret=true]crushed tomatoes with garlic"
    Write-Host "##vso[task.setvariable variable=outputSauce;isoutput=true]canned goods"
  name: SetVars
- pwsh: |
    Write-Host "Non-secrets automatically mapped in, sauce is $env:SAUCE"
    Write-Host "Secrets are not automatically mapped in, secretSauce is $env:SECRETSAUCE"
    Write-Host "You can use macro replacement to get secrets, and they'll be masked in the log: $(secretSauce)"
    Write-Host "Future jobs can also see $env:SETVARS_OUTPUTSAUCE"
    write-Host "Future jobs can also see $(SetVars.outputSauce)"
控制台输出:

Non-secrets automatically mapped in, sauce is crushed tomatoes
Secrets are not automatically mapped in, secretSauce is 
You can use macro replacement to get secrets, and they'll be masked in the log: ***
Future jobs can also see canned goods
Future jobs can also see canned goods

你查过我的答复了吗?有帮助吗?你查过我的回复了吗?有帮助吗?