我尝试在C#中执行powershell脚本。但我得到的例外情况如“限制语言模式或数据部分不允许使用赋值语句。”
这是我的C#代码:
string script = System.IO.File.ReadAllText(@"C:\script.ps1"); PowerShell exec_script = PowerShell.Create(); exec_script.RunspacePool = rs; exec_script.AddScript(script); IAsyncResult exec_AsyncResult = exec_script.BeginInvoke(); PSDataCollection exec_Result = exec_script.EndInvoke(exec_AsyncResult); foreach (PSObject cmdlet in exec_Result) { PSMemberInfoCollection collec = cmdlet.Members; foreach (PSMemberInfo temp in collec) { Console.WriteLine(temp.Name + "\t\t\t\t:\t" + temp.Value); } }
以下是我的PowerShell脚本:
[Collections.ArrayList]$serverList = New-Object Collections.ArrayList [string]$server if ($server -eq "") { $objects = Get-MailboxServer foreach ($object in $objects) { $out = $serverList.Add($object.Name) } } else { $serverList.Add($server) }
想知道问题是什么。
谢谢提前,
Viswanath B