为什么在 c# 中通过启动进程运行 powershell 时不导入 powershell 模块? [英] Why do powershell modules not import when running powershell via start process in c#?

查看:41
本文介绍了为什么在 c# 中通过启动进程运行 powershell 时不导入 powershell 模块?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 c# 应用程序,它使用以下代码来调用 powershell 脚本:

I have a c# application which uses the following code to shell out a call to a powershell script:

string scriptFileToExecute = "someScript.ps1;
var startInfo = new ProcessStartInfo();
startInfo.FileName = @"powershell.exe";
startInfo.RedirectStandardOutput = true;
startInfo.RedirectStandardError = true;
startInfo.UseShellExecute = false;
startInfo.CreateNoWindow = false;
startInfo.Arguments = string.Format(@"& '{0}' '{1}'", scriptFileToExecute, "argument");

var process = new Process { StartInfo = startInfo };
process.Start();

string output = process.StandardOutput.ReadToEnd();

这可以正常运行并运行脚本.

This works ok and runs the script.

但是,当我在脚本中包含这一行时:

However when I include this line in the script:

Import-Module ServerManager

脚本失败:

在脚本执行过程中发生错误:Import-Module:未加载指定的模块Servermanager",因为在任何模块目录中都找不到有效的模块文件.

当我只在机器上的 powershell 中运行脚本时,这工作正常.

This works fine when I run the script just in powershell on the machine.

在机器上执行 Get-Module : Format-List 结果:

Doing a Get-Module : Format-List on the machine results in:

名称:服务器管理器路径:C:\Windows\system32\WindowsPowerShell\v1.0\Modules\Servermanager\ServerManager.psm1描述 :模块类型:脚本版本:2.0.0.0嵌套模块:{Microsoft.Windows.ServerManager.PowerShell}ExportedFunctions : {Disable-ServerManagerStandardUserRemoting, Enable-ServerManagerStandardUserRemoting}ExportedCmdlets : {Get-WindowsFeature, Install-WindowsFeature, Uninstall-WindowsFeature}导出变量:导出别名:{Add-WindowsFeature, Remove-WindowsFeature}

并在脱壳脚本中包含 $env:path 导致:

and including $env:path in the shelled out script results in:

C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files\Microsoft\Web 平台安装程序\;C:\Program Files (x86)\Microsoft ASP.NET\ASP.NET Web Pages\v1.0\;C:\Program Files\Microsoft SQL Server\110\Tools\Binn\

$env:PSModulePath 输出:

C:\Users\Administrator.PORTAL\Documents\WindowsPowerShell\Modules;C:\Program Files (x86)\WindowsPowerShell\Modules;C:\Windows\system32\WindowsPowerShell\v1.0\Modules\

这似乎意味着它应该加载模块,因为它存在于 C:\Windows\system32\WindowsPowerShell\v1.0\Modules\Servermanager\ServerManager.psm1

Which seems to imply that it should load the module, as it exists at C:\Windows\system32\WindowsPowerShell\v1.0\Modules\Servermanager\ServerManager.psm1

我还检查了脚本在从应用程序调用时与从 PS 直接运行时使用相同版本的 powershell 以相同用户身份运行.

I've also checked that the script runs as the same user with the same version of powershell when invoked from the app as when run from PS directly.

什么可能阻止 PS 加载 ServerManager 模块?

What could be preventing PS from loading the ServerManager module?

推荐答案

原来是和平台目标有关.构建为 AnyCPU(选中首选 32 位"复选框)或构建为 x86,我们看到了这个问题.构建为 x64,问题在我正在安装的 64 位 Windows 上消失了.

So it turns out to be related to the platform target. Building as AnyCPU (with the 'prefer 32bit' checkbox checked) or building as x86 and we see this issue. Building as x64 and the issue goes away on the 64bit windows I'm installing on.

这篇关于为什么在 c# 中通过启动进程运行 powershell 时不导入 powershell 模块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆