[英]Running PowerShell commands from C#
我有powershell腳本,可以連接到在線交換並執行諸如創建共享郵箱,日歷,添加許可證等任務。
問題是,當我從C#類運行這些命令時,我得到了錯誤。
這是我的聯系:
string schemaURI = "http://schemas.microsoft.com/powershell/Microsoft.Exchange";
Uri connectTo = new Uri("https://outlook.office365.com/powershell-liveid/");
var securePassword = new SecureString();
foreach (char c in password)
{
securePassword.AppendChar(c);
}
this.credential = new PSCredential(login, securePassword);
WSManConnectionInfo connectionInfo = new WSManConnectionInfo(connectTo, schemaURI, credential);
connectionInfo.MaximumConnectionRedirectionCount = 5;
connectionInfo.AuthenticationMechanism = AuthenticationMechanism.Basic;
this.remoteRunspace = RunspaceFactory.CreateRunspace(connectionInfo);
this.remoteRunspace.Open();
這是我的PowerShell,可以正常工作:
PowerShell powershell = PowerShell.Create();
powershell.Runspace = this.remoteRunspace;
PSCommand command = new PSCommand();
command.AddCommand("new-mailbox");
command.AddParameter("Name", mailboxName);
command.AddParameter("Shared");
command.AddParameter("PrimarySmtpAddress", formatedMailboxName);
powershell.Commands = command;
powershell.Invoke();
這是無法正常工作的代碼:
PowerShell powershell = PowerShell.Create();
powershell.Runspace = this.remoteRunspace;
PSCommand command = new PSCommand();
command.AddCommand("Connect-MsolService");
command.AddCommand("Import-Module MSOnline");
command.AddParameter("Credential", this.credential);
command.AddCommand("Set-MsolUser");
command.AddParameter("UserPrincipalName", userLogin);
command.AddParameter("UsageLocation", "SE");
我得到的錯誤如下:
附加信息:術語“導入模塊MSOnline”不被視為cmdlet,函數,腳本文件或可運行程序的名稱。 檢查名稱的拼寫,或者是否包含路徑,請驗證路徑是否正確,然后重試。
我嘗試了各種操作,例如復制粘貼dll,將活動解決方案平台的CPU更改為64位,但沒有任何幫助。
可能是愚蠢的,但您嘗試了以下方法嗎?
command.AddCommand("Import-Module 'MSOnline'");
我有一個類似的問題,但是有一個skypeforbusiness模塊。 當我添加“”時有效。
您可以在Powershell ise中執行Import-Module MSOnline命令嗎?
我在這里找到問題的解決方案
下載scriptCreateNewMail [下載腳本分配行] 1類程序:Cmdlet {
string AdminUserName = "";
string AdminPassword = "";
string MailBoxUserAliasName = "red";
string MailBoxUserName = "redyellow";
string MailBoxUserFirstName = "red";
string MailBoxUserLastName = "yellow";
string MailBoxUserDisplayName = "red yellow";
string MailBoxUserMicrosoftOnlineServicesID = "red.yellow@vt0365dev.onmicrosoft.com";
string MailBoxUserPassword = "P@ssw0rd";
string MailBoxUserUsageLocation = "IN"; // Set Country of the User
string MailBoxUserAddLicenses = ""; // Assign Licenses
/// <summary>
/// Connect to the PowerShell
/// 1. Connect to 365 office with Admin login
/// 2 Create New MailBox
/// 3 Set the Country to the user
/// 3 Set the Country to the user
/// 4. Assign Licences to account user
/// </summary>
/// <param name="commandFileName"> command file name</param>
/// <param name="ErrorMessage"> error message</param>
/// <returns> true or false </returns>
public bool ConnectPowerShell(string commandFileName, ref string ErrorMessage)
{
Program Object_Pro = new Program();
String input;
Runspace runspace = null;
Pipeline pipeline = null;
PipelineStateInfo info = null;
try
{
if (File.Exists(commandFileName))
{
using (StreamReader sr = File.OpenText(commandFileName))
{
runspace = RunspaceFactory.CreateRunspace();
pipeline = runspace.CreatePipeline();
runspace.Open();
while ((input = sr.ReadLine()) != null)
{
//Replace the Peramter Value to original value
input = input.Replace("@@username", Object_Pro.AdminUserName);
input = input.Replace("@@password", Object_Pro.AdminPassword);
input = input.Replace("@@Alias", Object_Pro.MailBoxUserAliasName.ToLower());
input = input.Replace("@@Name", Object_Pro.MailBoxUserName.ToLower());
input = input.Replace("@@FirstName", Object_Pro.MailBoxUserFirstName.ToLower());
input = input.Replace("@@LastName", Object_Pro.MailBoxUserLastName.ToLower());
input = input.Replace("@@DisplayName", Object_Pro.MailBoxUserDisplayName.ToLower());
input = input.Replace("@@MicrosoftOnlineServicesID", Object_Pro.MailBoxUserMicrosoftOnlineServicesID.ToLower());
input = input.Replace("@@UPassword", Object_Pro.MailBoxUserPassword);
input = input.Replace("@@UsageLocation", Object_Pro.MailBoxUserUsageLocation);
input = input.Replace("@@AddLicenses", Object_Pro.MailBoxUserAddLicenses);
pipeline.Commands.AddScript(input);
}
pipeline.Commands.Add("Out-String");
Collection<PSObject> results = pipeline.Invoke();
runspace.Close();
}
info = pipeline.PipelineStateInfo;
if (info.State != PipelineState.Completed)
{
ErrorMessage = info.Reason.ToString();
return false;
}
else
{
ErrorMessage = "";
return true;
}
}
else
{
ErrorMessage = "File not found";
return false;
}
}
catch (Exception ex)
{
ErrorMessage = ex.Message;
return false;
}
}
static void Main(string[] args)
{
string StrErrorMessage = string.Empty;
bool ConnectResult;
// Create Object of Program class
Program ObjProgram = new Program();
// File Name Of the PowerShell Command
string fileName = @"E:\scriptCreateNewMail.txt";
string ScriptFileNameForSetLocation = @"E:\scriptUserlocationset.txt";
try
{
ConnectResult = ObjProgram.ConnectPowerShell(fileName, ref StrErrorMessage);
if (ConnectResult)
{
ConnectResult = ObjProgram.ConnectPowerShell(ScriptFileNameForSetLocation, ref StrErrorMessage);
if (ConnectResult)
{
Console.WriteLine("task completed");
}
else
{
Console.WriteLine(StrErrorMessage);
}
}
else
{
Console.WriteLine(StrErrorMessage);
}
}
catch (ExitException ex)
{
Console.WriteLine(ex.Message);
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.