简体   繁体   English

Powershell - 如何在 Powershell 脚本中以非交互方式调用 cmdlet?

[英]Powershell - How to call cmdlet in non-interactive way in Powershell script?

In my powershell script, I am calling a cmdlet (Say "Connect-Database").在我的 powershell 脚本中,我调用了一个 cmdlet(比如“连接数据库”)。 If the cmdlet needs additional parameter, it prompts the user to supply those values ( say, "Connect-Database" can not get credentials from registry, so it prompts for "Username" "password" on Powershell console and expects the user to give that value ).如果 cmdlet 需要其他参数,它会提示用户提供这些值(例如,“连接数据库”无法从注册表获取凭据,因此它会在 Powershell 控制台上提示输入“用户名”“密码”,并希望用户提供价值 )。

I looked into the code of Powershell cmdlet.我查看了 Powershell cmdlet 的代码。 I found that it is using "CommandInvocationIntrinsics" to prompt the user (using "NewScriptBlock" and then "Invoke" method of "CommandInvocationIntrinsics" ).我发现它使用“CommandInvocationIntrinsics”来提示用户(使用“NewScriptBlock”,然后使用“CommandInvocationIntrinsics”的“Invoke”方法)。

Since I am calling this cmdlets from the Powershell script, I want that whenever such a prompting happened, it will be suppressed and an exception is thrown.由于我是从 Powershell 脚本调用此 cmdlet,因此我希望无论何时发生此类提示,都会将其抑制并引发异常。

The code is something like this -代码是这样的 -

try
{
    $db = Connect-Database <databasename>
    #if username / password is prompted, it should be converted into error. But it should not be shown in powershell console.

    if($Error.Count > 0)
    {
        throw $Error[0]
    }
}
catch
{
    #do something
}

My way of doing that, is first to enumerate mandatory parameters, and then initialized them with $null vars.我这样做的方法是首先枚举强制参数,然后使用 $null 变量对其进行初始化。 So there is no more interaction and an error is thrown.所以没有更多的交互并抛出错误。

Easy solution, don't prompt.简单的解决方法,不要提示。 I feel that you should either always prompt (by using mandatory parameters), or never prompt during the execution of your command.我觉得您应该始终提示(通过使用强制参数),或者在执行命令期间从不提示。 If you don't have enough information, throw an error.如果您没有足够的信息,则抛出错误。 If your command is being used interactively, the user can rerun with the correct information.如果您的命令以交互方式使用,用户可以使用正确的信息重新运行。

This is a part of the design of PowerShell and likely cannot be overcome.这是 PowerShell 设计的一部分,可能无法克服。

If you are trying to be non-interactive, you should be ensuring that you have collected all the necessary information before trying to call the cmdlet (aka, you should not have missing parameters for your non-interactive calls).如果您尝试使用非交互式,则应确保在尝试调用 cmdlet 之前已收集所有必要信息(也就是,您的非交互式调用不应缺少参数)。 You are likely following an anti-pattern and should re-think your approach.您可能正在遵循反模式,应该重新考虑您的方法。 Prompting at the beginning of your non-interactive script (aka, short startup interaction), having parameters for your non-interactive script (those will, in turn, prompt if missing when set to mandatory), or reading from a config file (and verifying the information first) are a few approaches.在您的非交互式脚本开始时提示(又名,短暂的启动交互),为您的非交互式脚本提供参数(如果设置为强制时,这些参数反过来会提示),或从配置文件中读取(和首先验证信息)是几种方法。 Something common you will notice is that they all involve failing fast / failing early , a very good design.您会注意到的共同点是它们都涉及快速失败/早期失败,这是一个非常好的设计。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM