Sam*_*der 4 c# windows powershell
我有 ac# 应用程序,它使用以下代码来调用 powershell 脚本:
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();
Run Code Online (Sandbox Code Playgroud)
这工作正常并运行脚本。
但是,当我在脚本中包含这一行时:
Import-Module ServerManager
Run Code Online (Sandbox Code Playgroud)
脚本失败:
errors occurred during script execution: Import-Module : The specified module 'Servermanager' was not loaded because no valid module file was found in any module directory.
当我只在机器上的 powershell 中运行脚本时,这很好用。
在机器上执行 Get-Module : Format-List 结果:
Name : Servermanager
Path : C:\Windows\system32\WindowsPowerShell\v1.0\Modules\Servermanager\ServerManager.psm1
Description :
ModuleType : Script
Version : 2.0.0.0
NestedModules : {Microsoft.Windows.ServerManager.PowerShell}
ExportedFunctions : {Disable-ServerManagerStandardUserRemoting, Enable-ServerManagerStandardUserRemoting}
ExportedCmdlets : {Get-WindowsFeature, Install-WindowsFeature, Uninstall-WindowsFeature}
ExportedVariables :
ExportedAliases : {Add-WindowsFeature, Remove-WindowsFeature}
并包括$env:path在脱壳脚本中会导致:
C:\Windows\system32;
C:\Windows;C:\Windows\System32\Wbem;
C:\Windows\System32\WindowsPowerShell\v1.0\;
C:\Program Files\Microsoft\Web Platform Installer\;
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
我还检查了从应用程序调用时,脚本是否以相同用户身份运行,并且从 PS 直接运行时使用相同版本的 powershell。
什么可能阻止 PS 加载 ServerManager 模块?
所以结果证明它与平台目标有关。构建为 AnyCPU(选中“首选 32 位”复选框)或构建为 x86,我们看到了这个问题。构建为 x64,问题在我正在安装的 64 位 Windows 上消失了。
| 归档时间: |
|
| 查看次数: |
1876 次 |
| 最近记录: |