简体   繁体   English

如何将用户凭据从一个 Powershell 脚本传递到另一个?

[英]How To Pass User Credentials from One Powershell Script to Another?

I have a main menu script where I ask for the user to enter admin credentials and I want to use them in other scripts that the main menu calls.我有一个主菜单脚本,我要求用户输入管理员凭据,我想在主菜单调用的其他脚本中使用它们。

Here is my call to other scripts which launches the new script just fine and the called script runs.这是我对其他脚本的调用,它可以很好地启动新脚本并且被调用的脚本运行。

Start-Process PowerShell.exe -ArgumentList "-noexit", "-command &$ScriptToRun -UserCredential $UserCredential"

In the called script I accept the parameters like this在被调用的脚本中,我接受这样的参数

#Accept User Credentials if sent from Main menu
param(
[parameter(ValueFromPipelineByPropertyName=$true,ValueFromPipeline=$true,Mandatory=$false)]
  [PSCredential]$UserCredential
)

Instead of accepting the credentials, though, it is launching the prompt asking for credentials with the name of the object in the user field.但是,它不是接受凭据,而是启动提示,要求在用户字段中使用对象名称提供凭据。

在此处输入图片说明

Any help would be greatly appreciated in figuring this out, as I am banging my head against the wall.解决这个问题的任何帮助将不胜感激,因为我正在用头撞墙。

You can't pass non-string object directly across process boundaries like that, the input arguments to the new process is always going to be bare strings.您不能像那样直接跨进程边界传递非字符串对象,新进程的输入参数始终是裸字符串。

What you can do instead is use $UserCredentials to simply launch the new process:可以做的是使用$UserCredentials来简单地启动新进程:

Start-Process PowerShell.exe -Credential $UserCredential -ArgumentList "-noexit", "-command &$ScriptToRun" 

... or, preferably, call Invoke-Command from the calling script instead of starting a new process: ...或者,最好从调用脚本调用Invoke-Command而不是启动新进程:

Invoke-Command -ScriptBlock $scriptBlockToRun -Credential $UserCredential

One simple way to launch a process with credentials is to use Jobs.使用凭据启动进程的一种简单方法是使用作业。

$UserCredential = Get-Credential
$ScriptBlock = {param($x) "Running as $(whoami).  x=$x"}
& $ScriptBlock 10          # Just check it works under current credentials
$Job = Start-Job -Credential $UserCredential -ScriptBlock $ScriptBlock -ArgumentList 20
Receive-Job -Job $Job -Wait -AutoRemoveJob

One advantage of using jobs is that you get Output, Error, Verbose and Warning streams passed back to the caller which is not possible when using Start-Process.使用作业的优点之一是您将输出、错误、详细和警告流传递回调用者,这在使用 Start-Process 时是不可能的。

When using jobs all arguments and return values are implicitly serialized, which has limitations when passing complex objects, but copes well with all the basic types.使用作业时,所有参数和返回值都被隐式序列化,这在传递复杂对象时有局限性,但可以很好地处理所有基本类型。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将凭据从一个PowerShell脚本传递到另一个 - Passing credentials from one powershell script to another 如何将开关参数从一个 powershell 脚本传递给另一个? - How to pass switch parameter from one powershell script to another? 将参数从一个Powershell脚本传递到另一个 - Pass arguments from one powershell script to another 将参数从另一个PowerShell脚本传递给一个PowerShell脚本 - To pass argument to one powershell script from another powershell script 如何将 powershell 哈希表从一个脚本传递到 Jenkins 管道中的另一个脚本 - How to pass powershell hashtable from one script to another script in Jenkins Pipeline 如何将参数或变量从一个 powershell 脚本传递到另一个? - How do I pass arguments or variables from one powershell script to another? 将变量值从 AZ Powershell 任务中的一个 Powershell 脚本传递到 Azure DevOps Release Pipeline 中下一个 AZ Powershell 任务中的另一个脚本 - Pass variable value from one Powershell script in AZ Powershell task to another script in the next AZ Powershell task in Azure DevOps Release Pipeline 将变量值从 Powershell 任务中的一个脚本传递到 Azure DevOps 发布管道中下一个 Powershell 任务中的另一个脚本 - Pass variable value from one Powershell script in Powershell task to another script in the next Powershell task in Azure DevOps Release Pipeline 如何将SQL凭据作为命令行参数传递给PowerShell脚本 - How to pass SQL credentials to an PowerShell script as command-line arguments 如何在具有域用户凭据的本地计算机上运行Powershell脚本 - How to run Powershell script on local computer but with credentials of a domain user
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM