Import-Module ServerManager Import-Module : 未能加载指定的模块“ServerManager”,因为在任何模块目录中都没有找到有效模块文件..

时间:2022-07-26
本文章向大家介绍Import-Module ServerManager Import-Module : 未能加载指定的模块“ServerManager”,因为在任何模块目录中都没有找到有效模块文件..,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

操作系统: Windows server 2008 R2(64位)

C:WindowsSystem32WindowsPowerShellv1.0Modules 下有ServerManager的东东,可是还是找不到。

解决这个问题,需要如下2步:

1、Powershell中的执行策略(Execution Policy)

原因一个是因为Powershell默认的执行策略是Restricted,即可以运行单条命令,但不能运行脚本,包括格式和配置文件 (.ps1xml)、模块脚本文件 (.psm1) 和 Windows PowerShell 配置文件 (.ps1)、以及.bat文件。 解决方案是把执行策略改得宽松一点,比如RemoteSigned或者Unrestricted。在Powershell中运行以下命令即可: Set-ExecutionPolicy RemoteSigned 或 Set-ExecutionPolicy Unrestricted或-ExecutionPolicy RemoteSigned

2、64位操作系统和32位操作系统的关系

CMD ,64位操作系统有两个。一个是:C:WindowsSystem32CMD.exe(这个是64位的CMD,尽管名字叫System32);一个是C:WindowsSysWOW64CMD.exe(这个是32位的CMD.SysWOW64文件夹下的东西是64位操作系统为运行32位的应用程序而准备的)。

可是我编译生成的.exe是32位的,通过Setup Factory的File.Run(...)或者Shell.Execute(...),会调用C:WindowsSysWOW64CMD.exe(32位的CMD)。而ServerManager Module只有64为才有

所以改为编译成64位的就好了。

附(运行的脚本):

InstallDotNET.bat


@echo on

cd C:WindowsSystem32WindowsPowerShellv1.0
powershell -ExecutionPolicy "UnRestricted" -File C:InstallDotNET.ps1

InstallDotNET.ps1

import-module ServerManager
Add-WindowsFeature NET-Framework
exit

特别感谢—他们的博客最终给了我解决方案:

  • http://www.tuicool.com/articles/RvqqI3
  • http://blog.csdn.net/leon110/article/details/6203380

备注:

笔者应用场景:通过Setup Factory调用PowerShell的脚本