gpt4 book ai didi

c# - 如何将参数从 C# 程序传递给 powershell 脚本?

转载 作者:太空宇宙 更新时间:2023-11-03 13:30:39 25 4
gpt4 key购买 nike

我有一个脚本文件 sample.ps1,其中包含以下命令:

copy-item C:\source -destination C:\destination.

我不想硬编码源和目标的值,而是想将其作为参数传递给脚本。

copy-item $source -destination $destination.

我想从独立客户端调用此脚本并将源和目标作为参数传递。我有以下程序来执行脚本文件:

            string shellUri = "http://schemas.microsoft.com/powershell/Microsoft.PowerShell";
string userName = "MachineName\\Administrator";
string password = "Password";
SecureString securePassword = new SecureString();

foreach (char c in password)
{
securePassword.AppendChar(c);
}
PSCredential credential = new PSCredential(userName, securePassword);
WSManConnectionInfo connectionInfo = new WSManConnectionInfo(false, "machinename", 5985, "/wsman", shellUri, credential);
using (Runspace runspace = RunspaceFactory.CreateRunspace(connectionInfo))
{

runspace.Open();
String file = "C:\\scripts\\Sample.ps1";
Pipeline pipeline = runspace.CreatePipeline();

pipeline.Commands.AddScript(System.IO.File.ReadAllText(file));
}

我想通过C#程序将参数传递给脚本Sample.ps1。这怎么可能?

最佳答案

关于c# - 如何将参数从 C# 程序传递给 powershell 脚本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20677586/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com