Import-Module ServerManager Import-Module : 未能加载指定的模块“ServerManager”,因为在任何模块目录中都没有找到有效模块文件...(通过Setup Factory调用PowerShell的脚本)

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

C:\Windows\System32\WindowsPowerShell\v1.0\Modules 下有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:\Windows\System32\CMD.exe(这个是64位的CMD,尽管名字叫System32);一个是C:\Windows\SysWOW64\CMD.exe(这个是32位的CMD.SysWOW64文件夹下的东西是64位操作系统为运行32位的应用程序而准备的)。

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

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

附(运行的脚本):

InstallDotNET.bat

@echo on
cd C:\Windows\System32\WindowsPowerShell\v1.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安装包制作工具制作安装包


下一篇:android 上传文件"Content-Type",为"application/octet-stream" 用php程序在服务端用$GLOBALS['HTTP_RAW_POST_DATA']接受(二)