来自c#的Powershell远程命令行开关用于cim-session

时间:2015-08-10 13:04:30

标签: c# powershell powershell-remoting cmdlet

        using (PowerShell pInst=PowerShell.Create()) // implements Idisposable
        {
            string username = Console.ReadLine();

            System.Security.SecureString pwd = getPassword();
            PSCredential credential = new PSCredential(username, pwd);
            pInst.Runspace.SessionStateProxy.SetVariable("cred", credential);
            //pInst.AddScript("$cred;");
            //pInst.AddScript("$session= New-CimSession -ComputerName anksahawin8a $cred");
            //pInst.AddScript("$session;");
            //pInst.AddScript("param($param1) $d = get-date;"+"$d; $s; $param1; get-service");
            //.AddParameter("param1","Ankit");
            //pInst.Invoke();                                     // Synchronous Invocation


            pInst.AddScript("$session= New-CimSession -ComputerName anksahawin8a -Credential $cred;");
            //psOutput = pInst.Invoke();
            pInst.AddScript("$session;");
            Collection<PSObject> psOutput = pInst.Invoke();
            //psOutput = pInst.Invoke();
            if(pInst.Streams.Error.Count>0)                       // Errors
            {

            }
            foreach(PSObject pso in psOutput)
            {
                Console.WriteLine(pso);
                if (pso != null)
                {
                    //String result=pso.Members["Value"].Value.ToString();
                    Console.WriteLine(pso.BaseObject.GetType().FullName);
                    Console.WriteLine(pso.BaseObject.ToString() + "\n");
                }
            }
            Console.WriteLine("Hello");
        }

此代码无效。而 pInst.AddScript(&#34; $ session = New-CimSession&#34;); 对于本地计算机工作......

0 个答案:

没有答案