我正在尝试从VS.上的C#编写的Windows窗体应用程序中执行PowerShell脚本。
我遇到的问题是我的PowerShell脚本中有很多引号,我不知道如何将其插入到我的代码中。
这是我的PowerShell脚本:
param([string]$inParam)
$Username = 'SERVER\domain_user'
$Password = 'password'
$pass = ConvertTo-SecureString -AsPlainText $Password -Force
$Cred = New-Object System.Management.Automation.PSCredential -ArgumentList $Username,$pass
Invoke-Command -ComputerName SERVER_TEST -Authentication Default -ScriptBlock {&"D:\Programs\myapplication.cmd" $inParam} -Credential $Cred
我如何将其插入以下C#代码?
using (PowerShell powershellInstance = PowerShell.Create())
{
powershellInstance.AddScript();
}
希望这是足够的细节,并提前感谢您的帮助!
答案 0 :(得分:1)
使用multiline string literal并通过将它们加倍来转义嵌套的双引号:
public partial class Form1 : Form
{
public event EventHandler PersonSelected;
public String PersonName { get; set; }
public Form1()
{
InitializeComponent();
PersonName = "Person's name";
}
protected override void OnClick(EventArgs e)
{
Form2 form2 = new Form2(this);
form2.Show();
PersonSelected(this, EventArgs.Empty);
}
}
public partial class Form2 : Form
{
public Form2(Form1 form)
{
InitializeComponent();
form.PersonSelected += (sender, args) =>
{
Form1 form1 = sender as Form1;
if(form1 != null)
textBox1.Text = form1.PersonName;
};
}
}