tags:

views:

52

answers:

1

Hi,

I trying to execute the powershell script in C#. But I am getting the exception like "Assignment statements are not allowed in restricted language mode or a Data section."

Here is my C# code :

 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);
          }                       
}

Here is my PowerShell script :

[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)
}

wondering what is the problem.

Thanks is advance,

Viswanath B

+1  A: 

It looks like it's a server restriction on Exchange: check this thread on technet.

Paolo Tedesco