我正在尝试使用Process.Start重定向的I/O来调用PowerShell.exe字符串,并以UTF-8的形式返回输出.但我似乎无法做到这一点.
我尝试过的:
-Command参数运行Console.OutputEncoding在这两个我的控制台应用程序,并在PowerShell脚本$OutputEncoding在PowerShell中设置Process.StartInfo.StandardOutputEncodingEncoding.Unicode而不是做到这一切Encoding.UTF8在每种情况下,当我检查我给出的字节时,我会得到与原始字符串不同的值.我真的很想解释为什么这不起作用.
这是我的代码:
static void Main(string[] args)
{
DumpBytes("Héllo");
ExecuteCommand("PowerShell.exe", "-Command \"$OutputEncoding = [System.Text.Encoding]::UTF8 ; Write-Output 'Héllo';\"",
Environment.CurrentDirectory, DumpBytes, DumpBytes);
Console.ReadLine();
}
static void DumpBytes(string text)
{
Console.Write(text + " " + string.Join(",", Encoding.UTF8.GetBytes(text).Select(b => b.ToString("X"))));
Console.WriteLine();
}
static int ExecuteCommand(string executable, string arguments, string workingDirectory, Action<string> output, Action<string> error)
{
try
{
using …Run Code Online (Sandbox Code Playgroud) 一些C#代码使用参数执行powershell脚本.我想从Powershell获取一个返回码和一个字符串,以便知道如果Powershell脚本中的所有内容都正常.
什么是正确的方法 - 在Powershell和C#中
# Powershell script
# --- Do stuff here ---
# Return an int and a string - how?
# In c# I would do something like this, if this was a method:
# class ReturnInfo
# {
# public int ReturnCode;
# public string ReturnText;
# }
# return new ReturnInfo(){ReturnCode =1, ReturnText = "whatever"};
Run Code Online (Sandbox Code Playgroud)
void RunPowershellScript(string scriptFile, List<string> parameters)
{
RunspaceConfiguration runspaceConfiguration = RunspaceConfiguration.Create();
using (Runspace runspace = RunspaceFactory.CreateRunspace(runspaceConfiguration))
{
runspace.Open();
RunspaceInvoke scriptInvoker …Run Code Online (Sandbox Code Playgroud) 从 Azure AD 中的 C# 应用程序运行 powershell 脚本。
添加了以下 DLL 参考
Runspace runspace = RunspaceFactory.CreateRunspace();
runspace.Open();
Pipeline pipeline = runspace.CreatePipeline();
pipeline.Commands.AddScript("Import-Module AzureAD -Force;");
pipeline.Commands.AddScript("$password = ConvertTo-SecureString " + "\"abc1234\"" + " -AsPlainText -Force");
pipeline.Commands.AddScript("$Cred = New-Object System.Management.Automation.PSCredential (" + "\"abc@abc.com\"" + ", $password)");
pipeline.Commands.AddScript("Connect-AzureAD -Credential $Cred");
pipeline.Commands.AddScript("Get-AzureADApplication -Filter " + "\"DisplayName eq " + "\'PortalTestApp\'" + "\"");
var result = pipeline.Invoke();
Run Code Online (Sandbox Code Playgroud)
获取错误:
术语“Connect-AzureAD”不被识别为 cmdlet、函数、脚本文件或可运行程序的名称。检查名称的拼写,或者如果包含路径,请验证路径是否正确,然后重试。
我正在尝试使用 Visual Studio 构建图形平台。我不是开发人员,我想在单击按钮时运行 PowerShell 或批处理文件。问题是,当我尝试 C# 语法时,即使我安装了 PowerShell 扩展,它也不起作用。
我尝试了在互联网上找到的一些代码,process.start在所有情况下使用或尝试创建命令,但命令的名称未定义并且不起作用。
private void Button1_Click(object sender, EventArgs e)
{
Process.Start("path\to\Powershell.exe",@"""ScriptwithArguments.ps1"" ""arg1"" ""arg2""");
}
Run Code Online (Sandbox Code Playgroud)
我想启动我的.ps1脚本,但出现错误
名称进程未定义
我正在编写一个C#应用程序,它需要使用命令行参数执行Powershell脚本并检索输出(也就是我期望在PowerShell ISE的输出窗口中看到的内容 - 包括异常信息)
我找到了许多使用PowerShell V1对象完成此任务的代码示例.这些示例创建了运行空间,管道等(例如,使用命令行参数从C#执行PowerShell脚本)
我已经看到一些使用PowerShell V2的不同方法的一些参考.(这里的最佳答案:在Pipeline.Invoke抛出后在C#中捕获Powershell输出)使用V2似乎更简单.没有弄乱运行空间,piplines或其中任何一个.像这样的东西:
PowerShell powerShell = PowerShell.Create();
powerShell.AddScript(script);
var results = powerShell.Invoke();
Run Code Online (Sandbox Code Playgroud)
有没有什么好的工作示例使用PowerShell V2对象从C#代码中执行脚本?是否有任何已知的PowerShell V2(甚至V3)对象的良好文档以及如何使用它们的最佳实践?来自微软的官方文档应该是什么?一个好的书或网站,一个一个地分解System.Management.Automation组件?
我目前正在构建一个应用程序,以便从ASP.NET MVC网站自动执行某些Exchange 2010操作.
现在,当我尝试调用New-AddressList命令时,我遇到了一个ParameterBindingException.
我正在尝试创建以下调用(其工作):
new-AddressList -Name "7 AL" -RecipientContainer "myDomain.local/Customers/7" -IncludedRecipients 'AllRecipients' -Container '\' -DisplayName "7 AL"
Run Code Online (Sandbox Code Playgroud)
我是通过以下方式完成的:
var NewAddressList = new Command("New-AddressList");
NewAddressList.Parameters.Add("Name", "7 AL");
NewAddressList.Parameters.Add("RecipientContainer", "myDomain.local/Customers/7");
NewAddressList.Parameters.Add("IncludedRecipients", "AllRecipients");
NewAddressList.Parameters.Add("Container", @"\");
NewAddressList.Parameters.Add("DisplayName", "7 AL");
CommandsList.Add(NewAddressList);
Run Code Online (Sandbox Code Playgroud)
这个命令列表提供给我调用的管道,给出了以下错误:
New-AddressList:输入对象不能绑定到命令的任何参数,因为该命令不接受管道输入或输入及其属性与接受管道输入的任何参数都不匹配.
任何可能导致这种情况的线索?
使用Trace-Command输出给出:
PS C:\Users\ext_kefu> Trace-Command -Name parameterbinding -Expression {New-AddressList -Name "7 AL" -RecipientContainer "myDomain.local/Customers/7" -IncludedRecipients 'AllRecipients' -Container '\' -DisplayName "7 AL"} -PSHost
DEBUG: ParameterBinding Information: 0 : BIND NAMED cmd line args [New-AddressList]
DEBUG: ParameterBinding Information: 0 : BIND …Run Code Online (Sandbox Code Playgroud) 我尝试结合 stackoverflow 的两个答案(第一和第二)
InitialSessionState iss = InitialSessionState.CreateDefault();
// Override ExecutionPolicy
PropertyInfo execPolProp = iss.GetType().GetProperty(@"ExecutionPolicy");
if (execPolProp != null && execPolProp.CanWrite)
{
execPolProp.SetValue(iss, ExecutionPolicy.Bypass, null);
}
Runspace runspace = RunspaceFactory.CreateRunspace(iss);
runspace.Open();
Pipeline pipeline = runspace.CreatePipeline();
//Here's how you add a new script with arguments
Command myCommand = new Command(scriptfile);
CommandParameter testParam = new CommandParameter("key","value");
myCommand.Parameters.Add(testParam);
pipeline.Commands.Add(myCommand);
// Execute PowerShell script
results = pipeline.Invoke();
Run Code Online (Sandbox Code Playgroud)
在我的 powershell 脚本中,我有以下参数:
Param(
[String]$key
)
Run Code Online (Sandbox Code Playgroud)
但是,当我执行此操作时,会出现以下异常:
System.Management.Automation.CmdletInvocationException: Cannot validate argument on parameter 'Session'. …Run Code Online (Sandbox Code Playgroud) 我试图用c#中的参数调用powershell脚本.是否有任何选项只提供powershell脚本文件和参数,而不是将整个powershell命令作为c#代码中的字符串.