【发布时间】:2011-03-16 07:56:40
【问题描述】:
我试图在 C# 中执行 powershell 脚本。但我得到了“在受限语言模式或数据部分中不允许赋值语句”之类的异常。
这是我的 C# 代码:
字符串脚本 = System.IO.File.ReadAllText(@"C:\script.ps1"); PowerShell exec_script = PowerShell.Create(); exec_script.RunspacePool = rs; exec_script.AddScript(脚本); IAsyncResult exec_AsyncResult = exec_script.BeginInvoke(); PSDataCollection exec_Result = exec_script.EndInvoke(exec_AsyncResult); foreach(exec_Result 中的 PSObject cmdlet) { PSMemberInfoCollection 集合 = cmdlet.Members; foreach(集合中的 PSMemberInfo 临时) { Console.WriteLine(temp.Name + "\t\t\t\t:\t" + temp.Value); } }这是我的 PowerShell 脚本:
[Collections.ArrayList]$serverList = New-Object Collections.ArrayList
[字符串]$服务器
if ($server -eq "")
{
$objects = 获取邮箱服务器
foreach ($objects 中的$object)
{
$out = $serverList.Add($object.Name)
}
}
别的
{
$serverList.Add($server)
}
想知道是什么问题。
先谢谢了,
维斯瓦纳特 B
【问题讨论】:
标签: c# powershell