把powershell 用成 c#是一种什么体验
1个回答
展开全部
ublic class PSHelper
{
public PowerShell ps = PowerShell.Create();
public PSCommand psc = new PSCommand();
public PSObject pss;
public Runspace runspace ;//RunspaceFactory.CreateRunspace();
public static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
public PSHelper()
{
runspace = RunspaceFactory.CreateRunspace();
OpenRunspace();
}
public void OpenRunspace()
{
if (runspace.RunspaceStateInfo.State.ToString().Equals("Closed"))
{
runspace = RunspaceFactory.CreateRunspace();
}
if (runspace.RunspaceStateInfo.State.ToString().Equals("BeforeOpen"))
{
runspace.Open();
}
}
public void Close()
{
runspace.Close();
// ps.Dispose();//add by xiongww
if (runspace.RunspaceStateInfo.State.ToString().Equals("Opened"))
{
runspace.Close();
}
}
public PSCredential GetPSCredential(string computername, string username, string password)
{
SecureString str = new SecureString();
foreach (char ch in password)
{
str.AppendChar(ch);
}
return new PSCredential(username, str);
}
public void InitPSSession(string computername, string username, string password)
{
SecureString str = new SecureString();
foreach (char ch in password)
{
str.AppendChar(ch);
}
PSCredential credential = new PSCredential(username, str);
Hashtable htParameters = new Hashtable();
htParameters.Add("computername", computername);
htParameters.Add("credential", credential);
Collection<PSObject> collection = this.RunPowerShellLocal("new-pssession", htParameters);
this.pss = collection[0];
}
public Collection<PSObject> RunPowerShellRemote(string script)
{
Collection<PSObject> collection;
(!runspace.RunspaceStateInfo.State.ToString().Equals("Closed"))
//{
this.psc.Clear();
ScriptBlock block = ScriptBlock.Create(script);
this.psc.AddCommand("invoke-command");
this.psc.AddParameter("scriptBlock", block);
this.psc.AddParameter("session", this.pss);
OpenRunspace();
this.ps.Commands = this.psc;
this.ps.Runspace = runspace;
collection = this.ps.Invoke();
return collection;
}
}
————————————————
如果能够帮助你,麻烦采纳
你的采纳是我答题的动力
——谢谢你给我的支持!!!
{
public PowerShell ps = PowerShell.Create();
public PSCommand psc = new PSCommand();
public PSObject pss;
public Runspace runspace ;//RunspaceFactory.CreateRunspace();
public static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
public PSHelper()
{
runspace = RunspaceFactory.CreateRunspace();
OpenRunspace();
}
public void OpenRunspace()
{
if (runspace.RunspaceStateInfo.State.ToString().Equals("Closed"))
{
runspace = RunspaceFactory.CreateRunspace();
}
if (runspace.RunspaceStateInfo.State.ToString().Equals("BeforeOpen"))
{
runspace.Open();
}
}
public void Close()
{
runspace.Close();
// ps.Dispose();//add by xiongww
if (runspace.RunspaceStateInfo.State.ToString().Equals("Opened"))
{
runspace.Close();
}
}
public PSCredential GetPSCredential(string computername, string username, string password)
{
SecureString str = new SecureString();
foreach (char ch in password)
{
str.AppendChar(ch);
}
return new PSCredential(username, str);
}
public void InitPSSession(string computername, string username, string password)
{
SecureString str = new SecureString();
foreach (char ch in password)
{
str.AppendChar(ch);
}
PSCredential credential = new PSCredential(username, str);
Hashtable htParameters = new Hashtable();
htParameters.Add("computername", computername);
htParameters.Add("credential", credential);
Collection<PSObject> collection = this.RunPowerShellLocal("new-pssession", htParameters);
this.pss = collection[0];
}
public Collection<PSObject> RunPowerShellRemote(string script)
{
Collection<PSObject> collection;
(!runspace.RunspaceStateInfo.State.ToString().Equals("Closed"))
//{
this.psc.Clear();
ScriptBlock block = ScriptBlock.Create(script);
this.psc.AddCommand("invoke-command");
this.psc.AddParameter("scriptBlock", block);
this.psc.AddParameter("session", this.pss);
OpenRunspace();
this.ps.Commands = this.psc;
this.ps.Runspace = runspace;
collection = this.ps.Invoke();
return collection;
}
}
————————————————
如果能够帮助你,麻烦采纳
你的采纳是我答题的动力
——谢谢你给我的支持!!!
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询