[英]How to pass parameters to a powershell script using c# winforms?
我想通過 winforms 執行 powershell 腳本並獲得格式良好的輸出。 我設法讓它工作,但現在我需要將參數傳遞給我的腳本。 我無法做到這一點。
這是我的 RunScript 函數:
private string RunScript(string scriptFile)
{
Runspace runSpace = RunspaceFactory.CreateRunspace();
runSpace.Open();
Pipeline pipeLine = runSpace.CreatePipeline();
Command script = new Command(scriptFile);
script.Parameters.Add("COM_USERNAME", usernameBox.Text);
script.Parameters.Add("COM_PASSWORD", passwordBox.Text);
script.Parameters.Add("installDir", installDirBox.Text);
script.Parameters.Add("TEMPVAULT_PATH", tempVaultBox.Text);
script.Parameters.Add("MAX_REQ_LIMIT", maxReqLimBox.Text);
script.Parameters.Add("MAX_BUFF_LIMIT", maxBuffLimBox.Text);
pipeLine.Commands.AddScript(script.ToString());
pipeLine.Commands.Add("Out-String");
Collection<PSObject> results = pipeLine.Invoke();
runSpace.Close();
StringBuilder strBuilder = new StringBuilder();
foreach (PSObject item in results)
{
strBuilder.AppendLine(item.ToString());
}
return strBuilder.ToString();
}
這是我正在嘗試的腳本:
param (
[bool] $STARTSERVICE = $false ,
[bool] $INSTALL = $false ,
[bool] $INSTALL_DASHBOARD = $false,
[bool] $DASHBOARD_SETTINGS = $false,
[bool] $DASHBOARD_CREATENEWDB = $false,
[bool] $DALIM_SETTINGS = $false,
[bool] $INSTALLIIS = $true,
[bool] $FIRST_INSTALL = $true,
[bool] $RECOVERY = $false,
[string] $COM_USERNAME,
[string] $COM_PASSWORD,
[string] $RECOVERY_ADM_NAME,
[string] $RECOVERY_ADM_PWD,
[string] $Windows2012DVDLetter = "F:",
[string] $COM_UNCPATH,
[string] $installDir = "C:\Program Files\App\",
[string] $TEMPVAULT_PATH = "C:\TempVault",
$SOAP_MaxPostSize = 4294967295,
$MAX_REQ_LIMIT = 500000000,
$MAX_BUFF_LIMIT = 500000000
)
Write-Output "`nUsername = " $COM_USERNAME
Write-Output "`nPassword = " $COM_PASSWORD
Write-Output "`nCOM_UNCPATH = " $COM_UNCPATH
Write-Output "`nMaximum Request Limit = " $MAX_REQ_LIMIT
Write-Output "`nMaximum Buff Limit = " $MAX_BUFF_LIMIT
Write-Output "`nIsFirstInstall = " $FIRST_INSTALL
Write-Output "`nInstallation Directory = " $installDir
Write-Output "`nTempVault Path = " $TEMPVAULT_PATH
Write-Output "`nRestriction level = " $RESTRICT_LVL
我的輸出僅顯示腳本值中預先注冊的值,但我嘗試顯示的值(文本框輸入)沒有。 我錯過了什么嗎?
注意:以下假設scriptFile
是*.ps1
文件的路徑,而不是該文件的內容(包含 Powershell 代碼的字符串)。
有關如何處理后一種情況,請參閱底部部分。
您可以大大簡化您的調用:
private string RunScript(string scriptFile)
{
using (var ps = PowerShell.Create()) {
ps.AddCommand(scriptFile) // Be sure to pass a *full path*
.AddParameter("COM_USERNAME", usernameBox.Text)
.AddParameter("COM_PASSWORD", passwordBox.Text)
.AddParameter("installDir", installDirBox.Text)
.AddParameter("TEMPVAULT_PATH", tempVaultBox.Text)
.AddParameter("MAX_REQ_LIMIT", maxReqLimBox.Text)
.AddParameter("MAX_BUFF_LIMIT", maxBuffLimBox.Text)
.AddCommand('Out-String'); // Add a pipeline segment
// Return the 1st (and in this case only) output object, as a string.
return ps.Invoke<string>()[0];
}
}
使用PowerShell.Create()
創建類PowerShell
的實例,它提供基於隱式創建的運行空間的更高級別的 API :
.AddCommand()
自動添加新的管道段。至於你嘗試了什么:
pipeLine.Commands.AddScript(script.ToString());
.AddScript()
方法用於添加任意的PowerShell 代碼片段,而不是用於添加具有關聯參數的Command
實例。
( Command
實例表示 PowerShell 命令,例如Out-String
或外部可執行文件的名稱/路徑或腳本文件 ( *.ps1
) 的路徑[1] )。
通過使用.ToString()
存儲在script
的Command
實例進行字符串化,您實際上只是將腳本路徑作為要執行的命令傳遞 - 您使用.AddParameter()
添加的所有參數都將丟失,這就是為什么您只看到腳本輸出中的默認參數值。
相反,您應該按如下方式添加您的Command
實例:
pipeLine.Commands.Add(script)
如果scriptFile
不是文件路徑,而是腳本文件的內容(包含 PowerShell 代碼的字符串):
正如您已經澄清的那樣,這是您的實際用例,因為腳本作為資源嵌入到您通過RunScript(Properties.Resources.<the script>)
傳遞的可執行文件中
調整頂部的簡化方法如下:
// If `scriptFile` is the *contents* of a *.ps1 file,
// add it as a script [block] with .AddScript(),
// then add parameters (and the additional pipeline command).
ps.AddScript(scriptFile)
.AddParameter("COM_USERNAME", usernameBox.Text)
.AddParameter("COM_PASSWORD", passwordBox.Text)
.AddParameter("installDir", installDirBox.Text)
.AddParameter("TEMPVAULT_PATH", tempVaultBox.Text)
.AddParameter("MAX_REQ_LIMIT", maxReqLimBox.Text)
.AddParameter("MAX_BUFF_LIMIT", maxBuffLimBox.Text)
.AddCommand('Out-String'); // Add a pipeline segment
[1] PowerShell 只允許對位於PATH
環境變量中列出的目錄中的可執行文件/腳本執行僅按名稱執行(例如, foo.ps1
)。 否則,必須指定文件路徑,使用完整路徑最安全,因為 .NET 的當前目錄通常與 PowerShell 的不同。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.