从c#运行Powershell脚本出错
本文关键字:脚本 出错 Powershell 运行 | 更新日期: 2023-09-27 18:06:37
我有一个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 '模块中找到'Connect-SPOService'命令。PowerShell',但无法加载该模块。更多信息,请运行'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"变量包含以下内容:
当前的处理器架构是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'
我不知道这是不是一个好的解决方案,但它是有效的。我会继续找的。欢迎提出任何建议
In C:'Users'<your user>'AppData'Roaming'Microsoft'Windows'Start Menu'Programs'Windows PowerShell
powershell有两个版本:x86和AMD64,没有后缀名。
旧版本的模块仅用32位编写,新版本包括最新的sharepoint online仅用64位编写。
如果你以32位启动环境,它显然不会运行任何64位模块,在一些边缘情况下,你需要运行旧的32位来运行非常旧的模块。
一个更好的解决方案是调用你的powershell脚本%SystemRoot%'system32'WindowsPowerShell'v1.0'powershell.exe -command "script.ps1"
我猜你先找到syswow 32位版本