gpt4 book ai didi

c# - 如何从 .Net 程序运行 Import-PSSessions

转载 作者:行者123 更新时间:2023-11-30 17:00:54 28 4
gpt4 key购买 nike

我正在尝试用 C# 编写代码,通过 Power Shell 命令从用户那里获取邮箱详细信息。

Power Shell 命令脚本是:

Import-PSSession -session (New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://myServer.myDomain.com/Powershell)
Get-Mailbox -Identity helpdesk

脚本在 PowerShell 中运行正常。

现在我的目标是使用 C# 运行它。

这是我的功能:

private void button1_Click(object sender, EventArgs e)  
{
m_RunSpace = RunspaceFactory.CreateRunspace();

m_RunSpace.Open();

Pipeline pipeLine = m_RunSpace.CreatePipeline();

Command newSession = new Command("New-PSSession");
newSession.Parameters.Add("-ConfigurationName", "Microsoft.Exchange");
newSession.Parameters.Add("-ConnectionUri", "http://myServer.myDomain.com/Powershell");

Command createSessionForExch = new Command("Import-PSSession");
createSessionForExch.Parameters.Add("-Session", newSession);

Command getMailbox = new Command("Get-Mailbox");
getMailbox.Parameters.Add("-Identity", "helpdesk");

pipeLine.Commands.Add(createSessionForExch);
pipeLine.Commands.Add(getMailbox);

Collection<PSObject> commandResults = pipeLine.Invoke();

foreach (PSObject cmdlet in commandResults)
{


}
}

但我收到一条错误消息,指出“Get-Mailbox”命令无法识别: enter image description here

可能是因为未正确调用 Import-PSSessions。

我需要帮助如何从 C# 正确运行命令 Import-PSSession

最佳答案

希望您已经找到解决方案。如果没有,请试试这段代码:

 String url = "http://" + ExchangeServerName + "/powershell?serializationLevel=Full";
System.Uri uri = new Uri(url);
Console.WriteLine(url);
System.Security.SecureString securePassword = String2SecureString(password);

System.Management.Automation.PSCredential creds = new System.Management.Automation.PSCredential(userName, securePassword);


Runspace runspace = System.Management.Automation.Runspaces.RunspaceFactory.CreateRunspace();

PowerShell powershell = PowerShell.Create();
PSCommand command = new PSCommand();
command.AddCommand("New-PSSession");


command.AddParameter("ConfigurationName", "Microsoft.Exchange");
command.AddParameter("ConnectionUri", uri);

command.AddParameter("Credential", creds);
command.AddParameter("Authentication", Authentication);
//PSSessionOption sessionOption = new PSSessionOption();
//sessionOption.SkipCACheck = true;
//sessionOption.SkipCNCheck = true;
//sessionOption.SkipRevocationCheck = true;
//command.AddParameter("SessionOption", sessionOption);

powershell.Commands = command;

try
{
runspace.Open();

powershell.Runspace = runspace;

Collection<PSSession> result = powershell.Invoke<PSSession>();

foreach (ErrorRecord current in powershell.Streams.Error)
Console.WriteLine("The following Error happen when opening the remote Runspace: " + current.Exception.ToString() +
" | InnerException: " + current.Exception.InnerException);

if (result.Count != 1)
throw new System.Exception("Unexpected number of Remote Runspace connections returned.");

// Set the runspace as a local variable on the runspace
powershell = PowerShell.Create();

command = new PSCommand();
command.AddCommand("Set-Variable");
command.AddParameter("Name", "ra");
command.AddParameter("Value", result[0]);
powershell.Commands = command;
powershell.Runspace = runspace;
powershell.Invoke();

// First import the cmdlets in the current runspace (using Import-PSSession)
powershell = PowerShell.Create();
command = new PSCommand();
//command.AddScript("Import-PSSession $ra");
command.AddScript("Invoke-Command -ScriptBlock { Get-Mailbox -Identity:" + MailBoxName + " } -Session $ra");
powershell.Commands = command;
powershell.Runspace = runspace;
//powershell.Commands.AddCommand("Import-Module").AddArgument("activedirectory");
powershell.Invoke();

//command = new PSCommand();
//command.AddCommand("Get-Mailbox");

////Change the name of the database

//command.AddParameter("Identity", "");

//powershell.Commands = command;
//powershell.Runspace = runspace;


Collection<PSObject> results = new Collection<PSObject>();
results = powershell.Invoke();

关于c# - 如何从 .Net 程序运行 Import-PSSessions,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21607668/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com