Import-Module ServerManager Import-Module : 未能加載指定的模塊“ServerManager”,因為在任何模塊目錄中都沒有找到有效模塊文件..


操作系統: 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調用PowerShell的腳本

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM