在C#中使用Powershell脚本 [英] Using powershell scritps in C#

查看:107
本文介绍了在C#中使用Powershell脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



  我试图在C#中执行powershell脚本.但是我遇到了异常,例如在受限语言模式或数据部分中不允许执行赋值语句."

这是我的C#代码:


System.Uri serverUri =新的Uri(String.Format("http://{0}/powershell?serializationLevel = Full",sServerName));
PSC凭证;
如果(sUserName.Length> 0&& s sPassword.Length> 0)
{
                    System.Security.SecureString securePassword =新的System.Security.SecureString();

                    foreach(sPassword.ToCharArray()中的char c
                    {
               b securePassword.AppendChar(c);
                    }
                    creds =新的PSCredential(sUserName,securePassword);
}
其他
{
                    //使用Windows身份验证
                    creds =(PSCredential)null;
}
                            
connectionInfo =新的WSManConnectionInfo(serverUri,SHELL_URI,creds);
connectionInfo.AuthenticationMechanism = AuthenticationMechanism.Kerberos;

字符串脚本= 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< PSObject> exec_Result = exec_script.EndInvoke(exec_AsyncResult);

foreach(exec_Result中的PSObject cmdlet)
{  
         PSMemberInfoCollection< PSMemberInfo> collec = cmdlet.成员;           
         foreach(collec中的PSMemberInfo temp)
         {
                    Console.WriteLine(临时名称+``\ t \ t \ t \ t:\ t''+临时值);
         }}
}

这是我的PowerShell脚本:

[Collections.ArrayList] $ serverList =新对象Collections.ArrayList
[string] $ server

如果($ server -eq")
{
                             $ objects = Get-MailboxServer
                             foreach($ objects中的$ object)
                             {
              nbsp; bsp       $ out = $ serverList.Add($ object.Name)
                             }
}

其他
{
                             $ serverList.Add($ server)
}

想知道是什么问题.

谢谢,谢谢,

Viswanath B

解决方案

Viswanath:

这篇文章对您有帮助吗?

http://www.codeproject.com/KB/cs/HowToRunPowerShell.aspx

 

Wendell


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 :


System.Uri serverUri = new Uri(String.Format("http://{0}/powershell?serializationLevel=Full", sServerName));
PSCredential creds;
if (sUserName.Length > 0 && sPassword.Length > 0)
{
                    System.Security.SecureString securePassword = new System.Security.SecureString();

                    foreach (char c in sPassword.ToCharArray())
                    {
                        securePassword.AppendChar(c);
                    }
                    creds = new PSCredential(sUserName, securePassword);
}
else
{
                    // Use Windows Authentication
                    creds = (PSCredential)null;
}
               
connectionInfo = new WSManConnectionInfo(serverUri, SHELL_URI, creds);
connectionInfo.AuthenticationMechanism = AuthenticationMechanism.Kerberos;

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<PSObject> exec_Result = exec_script.EndInvoke(exec_AsyncResult);

foreach (PSObject cmdlet in exec_Result)
{  
          PSMemberInfoCollection<PSMemberInfo> 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

解决方案

Hi Viswanath:

Does this article help you out any?

http://www.codeproject.com/KB/cs/HowToRunPowerShell.aspx

 

Wendell


这篇关于在C#中使用Powershell脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆