从c#运行Powershell脚本时出错

时间:2014-06-03 07:50:49

标签: c# powershell sharepoint office365 ps1

我有一个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;
}

这个程序适用于其他脚本,但对于这个,我收到以下错误(在脚本中标记的第一行):

&#39; Connect-SPOService&#39;命令在模块&#39; Microsoft.Online.SharePoint.PowerShell&#39;中找到,但无法加载模块。有关详细信息,请运行“导入 - 模块Microsoft.Online.SharePoint.PowerShell&#39;。

我发现了一个关于此的问题,但没有回答: Error running ps1 from c# code (Office 365)

2 个答案:

答案 0 :(得分:2)

我修改了我的C#代码:

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

&#34;错误&#34; var包含以下内容:

当前的处理器架构是X86。 &#39; C:\ Program Files \ SharePoint Online Management Shell \ Microsoft.Online.SharePoint.PowerShell \ Microsoft.Online.SharePoint.PowerShell.psd1&#39;模块需要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'

我不知道它是否是一个很好的解决方案,但它的确有效。我会继续寻找。 任何建议都很好。

答案 1 :(得分:0)

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

有两种版本的Powershell x86和AMD64都没有后缀名称。

模块的旧版本仅以32位编写,包括最新在线共享点的较新版本仅以64位编写。

如果以32位启动环境,则显然不会运行任何64位模块,并且在少数情况下,您需要运行旧的32位才能运行非常旧的模块。

更好的解决方案是调用您的powershell脚本 %SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe -command "script.ps1"

我想您会首先找到syswow 32位版本。