我試圖從Web Api中運行一個PowerShell命令。我所得到的是這樣的錯誤 術語'Get-MailUser'不能作為cmdlet、函式、腳本檔案或可執行程式的名稱被識別。 我可以從PowerShell 7命令中運行同樣的代碼,而且運行良好。我在專案中使用了微軟的PowerShell SDK,它顯示為7.1級。 如果有任何幫助,我們將不勝感激!
這是一個代碼。
這是我使用的代碼。
if (powerhell == null)
{
using (Runspace runspace = RunspaceFactory.CreateRunspace()
{
runspace.Open();
powershell = PowerShell.Create();
powershell.Runspace = runspace;
PSCommand command = new PSCommand()。
command.AddCommand("Set-ExecutionPolicy").AddArgument("RemoteSigned") 。
command.AddCommand("New-PSSession")。
command.AddCommand("Import-Module").AddParameter("Name", "PowerShellGet") 。
command.AddCommand("Install-Module").AddParameter("Name", "ExchangeOnlineManagement") .AddParameter("Force");
command.AddCommand("Import-Module").AddParameter("Name", "ExchangeOnlineManagement") 。
command.AddCommand("Connect-ExchangeOnline").AddParameter("CertificateThumbPrint", "mythumbprint")
.AddParameter("AppId", "My app id")
.AddParameter("Organization", "mycompany.onmicrosoft.com")。)
command.AddCommand("Get-MailUser").AddParameter("Identity", "myemailaddress") 。
powershell.Commands = command;
// Collection<PSObject> results = powershell.Invoke();
var t1 = powershell.Invoke<PSSession> ()。
}
}
uj5u.com熱心網友回復:
當你連續呼叫AddCommand時,你實際上是在組成一個管道--所以相當于你在PowerShell中的代碼將是:
Set- ExecutionPolicy RemoteSigned |New-PSSession|Import-Module - 名稱PowerShellGet |Install-Module -Name ExchangeOnlineManagement -Force |Import-Module -Name ExchangeOnlineManagement|連接-ExchangeOnline - CertificateThumbPrint -AppId "my app id" -Organization mycompany. onmicrosoft.com |Get-MailUser -Identity myemailaddress
......這可能是不是你想要的。
記得在命令之間呼叫AddStatement()來代替:
command.AddCommand("Install-Module") 。 AddParameter("Name", "ExchangeOnlineManagement").AddParameter("Force") 。
command.AddStatement();
command.AddCommand("Import-Module").AddParameter("Name", "ExchangeOnlineManagement") 。
command.AddStatement()。
/...等等。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/326669.html
標籤:
