简体   繁体   English

如何在包含Host.Ui.prompt命令的代码创建的Powershell外壳中执行脚本?

[英]How can I execute scripts in a code created powershell shell that has Host.Ui.prompt commands in it?

I have a Powershell Commandlet which prompts a user from a secure string based on a condition. 我有一个Powershell Commandlet,它可以根据条件从安全字符串中提示用户。 Now I want to automate the testing of this commandlet for which I use a Powershell Remote Runspace to Invoke the commandlet. 现在,我想自动化此Commandlet的测试,为此我使用Powershell远程运行空间来调用Commandlet。 Currently it fails with this error. 当前,它因此错误而失败。

Write-Host : A command that prompts the user failed because the host program or the command type       does not support user interaction. Try a host program that supports user interaction, such as the Windows PowerShell Console or Windows PowerShell ISE, and remove prompt-related commands from command types that do not support user interaction, such as Windows PowerShell workflows.

How can I automate this? 我该如何自动化?

It sounds like you are running powershell via c#. 听起来您正在通过c#运行powershell。 You can't prompt the user for input from the powershell script. 您无法提示用户输入Powershell脚本的输入。 You either need to pre-provide the necessary info in the script, or prompt for the info from your application and then pass the info to the powershell script. 您要么需要在脚本中预先提供必要的信息,要么从您的应用程序中提示输入信息,然后将信息传递给powershell脚本。

As ojk mentioned the easiest way to accomplish this would probably be to use a powershell function then pass the necessary parameters to it via the code. 正如ojk所提到的,最简单的方法可能是使用powershell函数,然后通过代码将必要的参数传递给它。

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

相关问题 如何在创建的 powershell shell 中包含 Write-Host 命令的代码中执行脚本? - How can I execute scripts in a code created powershell shell that has Write-Host commands in it? 是否有等效于$ Host.UI.Prompt()和$ Host.UI.PromptForChoice()的Cmdlet? - Are there Cmdlets equivalent to $Host.UI.Prompt() and $Host.UI.PromptForChoice()? 从命令行运行时,Powershell host.ui.prompt不会弹出对话框 - Powershell host.ui.prompt doesn't pop up a dialog box when run from the command line 如何从 Windows 命令提示符调用提升的 PowerShell 以执行命令和 PowerShell 脚本文件? - How do I call an elevated PowerShell from Windows command prompt to execute commands and a PowerShell script file? 如何通过 Powershell 执行 SQL 脚本? - How can I execute SQL scripts via Powershell? 如何使用 Powershell 执行 PHP 代码? - How can I execute PHP code with Powershell? 我无法在PowerShell中执行某些命令 - I can't execute some commands in PowerShell 为什么我不能使用 powershell (5.1) 执行远程脚本 - Why can't I execute remote scripts using powershell (5.1) 如何在PowerShell ISE中更改Read-Host提示符的WindowTitle? - How can I change the WindowTitle of the Read-Host prompt in the PowerShell ISE? Team Foundation Server:执行Powershell脚本/命令来构建而不是msbuild - Team Foundation Server: Execute Powershell scripts/commands to build instead of msbuild
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM