首页 > 解决方案 > 提示时如何将参数作为输入传递给powershell脚本?

问题描述

我是PowerShell的新手。“dt setup”是我试图自动化的命令,文件名是 dtSetupAutomation.ps1。此命令将提示我输入 UserMailId 和 UserName,然后我必须按 Enter。我编码以获取 UserMailId 和 UserName 作为强制参数,但我不知道如何在命令提示时将其作为输入提供。谁能帮帮我吗?dtSetupAutomation.ps1:

param(
    [Parameter(Mandatory=$True)]
    [string]$UserMailId,

    [Parameter(Mandatory=$True)]
    [Security.SecureString]$UserFullName
)
dt setup

什么“dt setup”提示:

对于以问号开头的每一行中的每个提示,脚本都会停止,如下所示:

PS C:\> dt setup
 ====> Git Proxy configuration
? Allow devtool to manage your github proxy settings in the .gitconfig file? [? for help] (Y/n) Y
? Your email: [? for help] preethibe.91@gmail.com
? Your full name: [? for help] Preethi Palanisamy
 ====> Github token creation
? You already have a valid token. Skip token creation? (Y/n) Y
====== Git configuration ======
Git user.name: Preethi Palanisamy
Git user.email: preethibe.91@gmail.com
...
PS C:\>

标签: powershellparameters

解决方案


这解决了我的问题:

$myshell = New-Object -com "Wscript.Shell"
.\dt.exe setup $myshell.sendkeys("Y") $myshell.sendkeys("{ENTER}") $myshell.sendkeys("preethix.palanisamy@intel.com") $myshell.sendkeys("{ENTER}") $myshell.sendkeys("{ENTER}")

推荐阅读