【问题标题】:powershell function return bad resultpowershell 函数返回错误的结果
【发布时间】:2018-10-16 08:29:54
【问题描述】:

我有这个函数,它以另一个用户身份运行命令并返回结果。

$OutputEncoding = [ System.Text.Encoding]::UTF8   
write-host (get-date -format s) " Beginning script..."
function getCreds()
{
    $Username = 'domain\user'
    $Password = 'test'
    $pass = ConvertTo-SecureString -AsPlainText $Password -Force
    $Credential = New-Object System.Management.Automation.PSCredential -ArgumentList $UserName, $pass
    return $Credential;
}
function executeAsCryptUser($cmd){

write-host "cmd:"$cmd
$creds=getCreds
#Use System.Diagnostics to start the process as UserB
$ProcessInfo = New-Object System.Diagnostics.ProcessStartInfo
#With FileName we're basically telling powershell to run another powershell process
$ProcessInfo.FileName = "powershell.exe"
#CreateNoWindow helps avoiding a second window to appear whilst the process runs
$ProcessInfo.CreateNoWindow = $true
#Note the line below contains the Working Directory where the script will start from
$ProcessInfo.WorkingDirectory = $env:windir
$ProcessInfo.RedirectStandardError = $true 
$ProcessInfo.RedirectStandardOutput = $true 
$ProcessInfo.UseShellExecute = $false
#The line below is basically the command you want to run and it's passed as text, as an argument
$ProcessInfo.Arguments = $cmd
#The next 3 lines are the credential for UserB, as you can see, we can't just pass $Credential
$ProcessInfo.Username = $creds.GetNetworkCredential().username
$ProcessInfo.Domain = $creds.GetNetworkCredential().Domain
$ProcessInfo.Password = $creds.Password
#Finally start the process and wait for it to finish
$Process = New-Object System.Diagnostics.Process 
$Process.StartInfo = $ProcessInfo 
$Process.Start() 
$Process.WaitForExit() 
#Grab the output
$GetProcessResult = $Process.StandardOutput.ReadToEnd().ToString()
Write-host "Potential Error:"+ $Process.StandardError.ReadToEnd().ToString()
$result=$GetProcessResult.Trim()
write-Host "Debug output:"$result

return $result
}

$pop=executeAsCryptUser "whoami"
write-host "out:" $pop

当我运行它时,它可以工作,但是我在返回变量中注入了一个“True”。我得到以下输出:

出:真正的域\测试

如果我调试脚本并查看$result 并在变量中找不到“True”的痕迹。

有什么问题? 谢谢

【问题讨论】:

    标签: windows shell powershell


    【解决方案1】:

    您的问题是您将两个值写入输出流,该输出流用于在函数之间传输值(沿管道传输)。

    值从何而来:

      # Below return "True" to the output-stream
      $Process.Start()
      ...
      # Below appends the content of "result" and return from the function
      return $result
    

    阅读link 关于流的信息。简而言之:命令的每个返回值都会写入输出流,除非您将结果存储在变量中。

    我认为你需要改变

    $Process.Start()

    $Process.Start() | Out-Null

    上面的行丢弃了True 返回值。作为替代方案,您可以将结果存储在变量中并保持不使用,例如:

    $rv = $Process.Start()

    另外请注意,PowerShells return 语句具有不同的行为,例如C# 之一。

    您的线路:

    return $result

    相当于:

    # Append content to output stream
    $result
    # return from the function
    return
    

    阅读此link 了解有关退货的更多信息。

    希望对您有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-05-04
      • 2013-01-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-12
      相关资源
      最近更新 更多