用于cim会话的来自c#的Powershell远程commandlet 使用(PowerShell pInst=PowerShell.Create())//实现Idisposable { 字符串username=Console.ReadLine(); System.Security.SecureString pwd=getPassword(); PSCredential=新的PSCredential(用户名,pwd); pInst.Runspace.SessionStateProxy.SetVariable(“cred”,凭证); //pInst.AddScript(“$cred;”); //pInst.AddScript(“$session=New CimSession-ComputerName anksahawin8a$cred”); //pInst.AddScript(“$session;”); //pInst.AddScript(“param($param1)$d=获取日期;“+”$d;$s;$param1;获取服务”); //.AddParameter(“param1”、“Ankit”); //pInst.Invoke();//同步调用 pInst.AddScript(“$session=New-CimSession-ComputerName-anksahawin8a-Credential$cred;”); //psOutput=pInst.Invoke(); pInst.AddScript(“$session;”); 集合psOutput=pInst.Invoke(); //psOutput=pInst.Invoke(); if(pInst.Streams.Error.Count>0)//错误 { } foreach(psOutput中的pso对象pso) { 控制台写入线(pso); if(pso!=null) { //字符串结果=pso.Members[“Value”].Value.ToString(); WriteLine(pso.BaseObject.GetType().FullName); Console.WriteLine(pso.BaseObject.ToString()+“\n”); } } Console.WriteLine(“你好”); }
此代码不起作用。鉴于 pInst.AddScript(“$session=New CimSession”); 对于本地计算机工程用于cim会话的来自c#的Powershell远程commandlet 使用(PowerShell pInst=PowerShell.Create())//实现Idisposable { 字符串username=Console.ReadLine(); System.Security.SecureString pwd=getPassword(); PSCredential=新的PSCredential(用户名,pwd); pInst.Runspace.SessionStateProxy.SetVariable(“cred”,凭证); //pInst.AddScript(“$cred;”); //pInst.AddScript(“$session=New CimSession-ComputerName anksahawin8a$cred”); //pInst.AddScript(“$session;”); //pInst.AddScript(“param($param1)$d=获取日期;“+”$d;$s;$param1;获取服务”); //.AddParameter(“param1”、“Ankit”); //pInst.Invoke();//同步调用 pInst.AddScript(“$session=New-CimSession-ComputerName-anksahawin8a-Credential$cred;”); //psOutput=pInst.Invoke(); pInst.AddScript(“$session;”); 集合psOutput=pInst.Invoke(); //psOutput=pInst.Invoke(); if(pInst.Streams.Error.Count>0)//错误 { } foreach(psOutput中的pso对象pso) { 控制台写入线(pso); if(pso!=null) { //字符串结果=pso.Members[“Value”].Value.ToString(); WriteLine(pso.BaseObject.GetType().FullName); Console.WriteLine(pso.BaseObject.ToString()+“\n”); } } Console.WriteLine(“你好”); },c#,powershell,powershell-remoting,cmdlet,C#,Powershell,Powershell Remoting,Cmdlet,此代码不起作用。鉴于 pInst.AddScript(“$session=New CimSession”); 对于本地计算机工程 using (PowerShell pInst=PowerShell.Create()) // implements Idisposable { string username = Console.ReadLine(); System.Security.SecureString pwd
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");
}