簡體   English   中英

從c#運行Powershell腳本時出錯

[英]Error running Powershell script from c#

我有一個ps1腳本,而不是從powershell執行時運行正常。 它在Office365中創建用戶:

Param(
  [string]$adminUser,
  [string]$password,
  [string]$adminSite,
  [string]$userDisplayName,
  [string]$userFirstName,
  [string]$userLastName,
  [string]$userPrincipalName,
  [string]$userLicense,
  [string]$userOffice,
  [string]$userDepartment
)
try {
    [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client")
    [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client.Runtime")

    $executionPolicy = Get-ExecutionPolicy
    Set-ExecutionPolicy RemoteSigned

    $secpasswd = ConvertTo-SecureString $password -AsPlainText -Force
    $credential = New-Object System.Management.Automation.PSCredential($adminUser,$secpasswd)

    Connect-MSolService -Credential $credential
    #Write-Host "Conected to MSolService ..." -ForegroundColor Green 
    Connect-SPOService -Url $adminSite -Credential $credential # Here fail when running from .NET
    #Write-Host "Conected to SP Online ..." -ForegroundColor Green

    $user = New-MsolUser -FirstName $userFirstName -LastName $userLastName -UserPrincipalName $userPrincipalName -DisplayName $userDisplayName -LicenseAssignment $userLicenseAssignment -Office $userOffice -Department $userDepartment -UsageLocation ES
}catch [Exception] {
    #Write-host "An Exception ocurred. The proccess is uncompleted" -ForegroundColor Red
    #Write-Host $_.Exception.Message -ForegroundColor Red
    Set-ExecutionPolicy $executionPolicy
    return $false
}

Set-ExecutionPolicy $executionPolicy
    return $user

有用。 但是,我有一個C#程序以這種方式執行此腳本:

private Collection<PSObject> RunPsScriptFromFile(string psScriptPath, Dictionary<string, Object> parameters) {
  if (!File.Exists(psScriptPath)) {
    throw new FileNotFoundException("File not found.", psScriptPath);
  }

  Collection<PSObject> returnObjects = null;

  using (Runspace runSpace = RunspaceFactory.CreateRunspace()) {
    runSpace.Open();
    RunspaceInvoke runSpaceInvoker = new RunspaceInvoke(runSpace);
    Pipeline pipeLine = runSpace.CreatePipeline();

    Command cmd = new Command(psScriptPath, false);
    if (parameters != null && parameters.Count > 0) {
      foreach (KeyValuePair<string, Object> p in parameters) {
        CommandParameter cp = new CommandParameter(p.Key, p.Value);
        cmd.Parameters.Add(cp);
      }
    }

    pipeLine.Commands.Add(cmd);
    returnObjects = pipeLine.Invoke();
  }

  return returnObjects;
}

這個程序適用於其他腳本,但對於這個,我得到以下錯誤(在我在腳本中標記的行):

在“Microsoft.Online.SharePoint.PowerShell”模塊中找到了“Connect-SPOService”命令,但無法加載該模塊。 有關更多信息,請運行“Import-Module Microsoft.Online.SharePoint.PowerShell”。

我發現了一個關於此的問題,但沒有回答: 從c#代碼運行ps1時出錯(Office 365)

我修改了我的C#代碼:

pipeLine.Commands.Add(cmd);
returnObjects = pipeLine.Invoke();
var error = pipeLine.Error.ReadToEnd(); // New line

“error”var包含以下內容:

目前的處理器架構是X86。 “C:\\ Program Files \\ SharePoint Online Management Shell \\ Microsoft.Online.SharePoint.PowerShell \\ Microsoft.Online.SharePoint.PowerShell.psd1”模塊需要Amd64體系結構。

我找到了這個文件,我改變了這一行

# Processor architecture (None, X86, Amd64, IA64) required by this module
ProcessorArchitecture = 'Amd64'

對於這一個:

# Processor architecture (None, X86, Amd64, IA64) required by this module
ProcessorArchitecture = 'X86'

我不知道這是否是一個很好的解決方案,但它是有效的。 我會繼續尋找。 任何建議都很好。

C:\\Users\\<your user>\\AppData\\Roaming\\Microsoft\\Windows\\Start Menu\\Programs\\Windows PowerShell

有兩個版本的powershell,x86和AMD64,沒有后綴名稱。

較舊版本的模塊僅以32位編寫,較新版本包括最新的在線sharepoint僅以64位編寫。

如果以32位啟動環境,它顯然不會運行任何64位模塊,並且在一些邊緣情況下,您需要運行舊的32位來運行非常舊的模塊。

更好的解決方案是調用powershell腳本%SystemRoot%\\system32\\WindowsPowerShell\\v1.0\\powershell.exe -command "script.ps1"

我猜你首先找到了syswow 32位版本。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM