我創建了一個 powershell 腳本,其中包括:
param($ServerName, $Location)
"Application Name: $ServerName"
"Location: $Location"
當我在 powershell 中并運行.\Params.ps1 ConsoleApp1 Washington, D.C.它時,它將顯示:
Application Name: ConsoleApp1
Location: Washington D.C.
所以我知道這很好用。現在我想把它帶到 c# 并執行引數傳遞。
在我的控制臺應用程式中,我創建了以下內容:
static void Main(string[] args)
{
string[] scriptParam = { "ConsoleApp1", "Washington, D.C."};
string powerShell = PerformScript(scriptParam);
Console.WriteLine(powerShell);
Console.WriteLine("\nPowershell script excuted!!");
Console.ReadLine();
}
static string PerformScript(string scriptParameters)
{
InitialSessionState runspaceConfiguration = InitialSessionState.Create();
Runspace runspace = RunspaceFactory.CreateRunspace(runspaceConfiguration);
runspace.Open();
Pipeline pipeline = runspace.CreatePipeline();
PowerShell ps = PowerShell.Create();
ps.Runspace = runspace;
ps.AddCommand(@"C:\Users\users\source\repos\ConsoleApp1\powershell\Params.ps1");
foreach (string scriptParameter in scriptParameters)
{
ps.AddParameter(scriptParameter);
}
Collection<PSObject> psObjects = pipeline.Invoke();
runspace.Close();
StringBuilder stringBuilder = new();
foreach (PSObject item in psObjects)
{
stringBuilder.AppendLine(item.ToString());
}
return stringBuilder.ToString();
}
但是當我運行程式時,我得到了一個Exception Unhandled在線
Collection<PSObject> psObjects = pipeline.Invoke();
System.Management.Automation.PSInvalidOperationException: '管道不包含命令。'
傳遞引數時我做錯了什么嗎?
更新代碼:
static string PerformScript(string[] scriptParameters)
{
InitialSessionState runspaceConfiguration = InitialSessionState.Create();
Runspace runspace = RunspaceFactory.CreateRunspace(runspaceConfiguration);
runspace.Open();
Pipeline pipeline = runspace.CreatePipeline();
using (var ps = PowerShell.Create())
{
var result = ps.AddCommand(@"C:\Users\users\source\repos\ConsoleApp1\powershell\Params.ps1")
.AddArgument("ConsoleApp1")
.AddArgument("Washington, D.C.")
.Invoke();
foreach (var o in result)
{
Console.WriteLine(o);
}
}
Collection<PSObject> psObjects = pipeline.Invoke();
runspace.Close();
StringBuilder stringBuilder = new();
foreach (PSObject item in psObjects)
{
stringBuilder.AppendLine(item.ToString());
}
return stringBuilder.ToString();
}
uj5u.com熱心網友回復:
使用.AddArgument(),不是.AddParameter()。
您要傳遞的是位置(未命名)引數,即僅引數值,其目標引數由它們的位置隱含,這就是.AddArgument()目的。
相比之下,.AddParameter()對于命名引數,您首先指定引數名稱,然后是其值(引數)。
這是一個簡化的示例:
using (var ps = PowerShell.Create()) {
var result = ps.AddCommand(@"C:\Users\users\source\repos\ConsoleApp1\powershell\Params.ps1")
.AddArgument("ConsoleApp1")
.AddArgument("Washington, D.C.")
.Invoke();
foreach (var o in result) {
Console.WriteLine(o);
}
}
注:以上用途大大簡化的API,可直接通過的方法PowerShell實體(與創建.Create()):如果自動創建一個默認的會話狀態的運行空間是足夠的,有沒有必要明確的創建與運行空間的RunspaceFactory.CreateRunspace(),一個會話狀態InitialSessionState.Create()和運行空間的管道.CreatePipeline()- 請參閱底部以了解如何將這些技術應用于您的代碼。
至于你嘗試了什么:
如果您錯誤地僅使用.AddParameter()單個方法引數,PowerShell SDK 所做的就是將省略的引數值默認為true,這.AddParameter('foo')相當于-foo: $true從 PowerShell 內部傳遞。
僅當您傳遞的引數名稱是開關引數(型別為System.Management.Automation.SwitchParameter,[switch]在 PowerShell 代碼中)時,這才按預期作業。
應用于您的代碼:
static void Main(string[] args)
{
Console.WriteLine(
PerformScript(
@"C:\Users\users\source\repos\ConsoleApp1\powershell\Params.ps1",
new string[] { "ConsoleApp1", "Washington, D.C." }
)
);
}
static string PerformScript(string scriptPath, string[] scriptArguments)
{
StringBuilder stringBuilder = new();
using (var ps = PowerShell.Create())
{
ps.AddCommand(scriptPath);
foreach (var arg in scriptArguments)
{
ps.AddArgument(arg);
}
foreach (var o in ps.Invoke())
{
stringBuilder.AppendLine(o.ToString());
}
}
return stringBuilder.ToString();
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/338506.html
