来自C#的Powershell命令 [英] Powershell commands from C#
问题描述
我想从C#运行这个powershell命令
PS> test-Dtc -LocalComputerName$ env:COMPUTERNAME-RemoteComputerNamemyRemotePC-ResourceManagerPort 17100 -Verbose
并将输出放入list2
我尝试了什么:
我尝试了谷歌的一些推荐。包括
PowerShell ps = PowerShell.Create();
ps.AddCommand(Test-Dtc);
ps.AddArgument( - LocalComputerName \$ env:COMPUTERNAME \-RemoteComputerName \Remotepc \-ResourceManagerPort 17100 -Verbose);
foreach(PSObject结果在ps.Invoke()中)
{
listBox2。 Items.Add(result);
} //结束foreach。
但到目前为止没有运气。
任何帮助将不胜感激
I would like to run this powershell command from C#
PS> test-Dtc -LocalComputerName "$env:COMPUTERNAME" -RemoteComputerName "myRemotePC" -ResourceManagerPort 17100 -Verbose
and put output in list2
What I have tried:
I tried several recommendations from google.including
PowerShell ps = PowerShell.Create();
ps.AddCommand("Test-Dtc");
ps.AddArgument("-LocalComputerName \"$env:COMPUTERNAME\" -RemoteComputerName \"Remotepc\" -ResourceManagerPort 17100 -Verbose");
foreach (PSObject result in ps.Invoke())
{
listBox2.Items.Add(result);
} // End foreach.
but no luck so far.
Any help would be greatly appreciated
推荐答案
env:COMPUTERNAME-RemoteComputerNamemyRemotePC - ResourceManagerPort 17100 -Verbose
并将输出列入清单2
我尝试过:
我尝试了谷歌的一些推荐。包括
PowerShell ps = PowerShell.Create();
ps.AddCommand(Test-Dtc);
ps.AddArgument( - LocalComputerName \
env:COMPUTERNAME" -RemoteComputerName "myRemotePC" -ResourceManagerPort 17100 -Verbose
and put output in list2
What I have tried:
I tried several recommendations from google.including
PowerShell ps = PowerShell.Create();
ps.AddCommand("Test-Dtc");
ps.AddArgument("-LocalComputerName \"
env: COMPUTERNAME \-RemoteComputerName \Remotepc \-ResourceManagerPort 17100 -Verbose);
foreach(PSObject in ps.Invoke())
{
listBox2.Items.Add(result);
} //结束foreach。
但到目前为止没有运气。
任何帮助将不胜感激
env:COMPUTERNAME\" -RemoteComputerName \"Remotepc\" -ResourceManagerPort 17100 -Verbose");
foreach (PSObject result in ps.Invoke())
{
listBox2.Items.Add(result);
} // End foreach.
but no luck so far.
Any help would be greatly appreciated
是的,您应该看到运行时异常:
Yes, you should be seeing a runtime exception:
System.Management.Automation.RuntimeException 发生
HResult = 0x80131501
Message = - LocalComputerName
System.Management.Automation.RuntimeException occurred
HResult=0x80131501
Message="-LocalComputerName "
这篇关于来自C#的Powershell命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!