用于在Windows Server 2012上安装.NET 3.5框架的批处理文件

5

我想制作一个批处理文件,在Windows Server 2012上安装.NET Framework 3.5。我尝试了以下方法,但没有成功:

cd /D %userprofile% 
C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe 
Import-Module ServerManager 
powershell -ImportSystemModules Add-WindowsFeature NET-Framework-Features

看起来在进入PowerShell控制台后,最后2个命令未执行。

有人知道为什么会卡住吗?

或者有其他批处理文件可以自动安装Windows Server 2012中的.NET 3.5吗?

经过更多尝试,当手动运行时,我使用以下命令使bat文件工作。

call C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -ImportSystemModules Add-WindowsFeature NET-Framework-Features

但是当我尝试从dotnetInstaller运行它时,同样的批处理文件就不再起作用了。
 <component command="CMD.EXE /K &quot;#APPPATH\Install.net3.5.bat&quot;" command_silent="" command_basic="" uninstall_command="" uninstall_command_silent="" uninstall_command_basic="" returncodes_success="" returncodes_reboot="" disable_wow64_fs_redirection="False" id=".Net 3.5 SP1 Win8Server" display_name=".Net 3.5 SP1" uninstall_display_name="" os_filter="" os_filter_min="winServer2008R2" os_filter_max="" os_filter_lcid="" type="cmd" installcompletemessage="" uninstallcompletemessage="" mustreboot="False" reboot_required="" must_reboot_required="False" failed_exec_command_continue="" allow_continue_on_error="True" default_continue_on_error="False" required_install="True" required_uninstall="True" selected_install="True" selected_uninstall="True" note="" processor_architecture_filter="" status_installed="" status_notinstalled="" supports_install="True" supports_uninstall="False" show_progress_dialog="True" show_cab_dialog="True">
 <installedcheck path="SOFTWARE\Microsoft\NET Framework Setup\NDP\v3.5" fieldname="Install" fieldvalue="1" defaultvalue="False" fieldtype="REG_DWORD" comparison="match" rootkey="HKEY_LOCAL_MACHINE" wowoption="NONE" type="check_registry_value" description="Installed Check" />
      <installedcheck path="SOFTWARE\Microsoft\NET Framework Setup\NDP\v3.5" fieldname="SP" fieldvalue="1" defaultvalue="False" fieldtype="REG_DWORD" comparison="match" rootkey="HKEY_LOCAL_MACHINE" wowoption="NONE" type="check_registry_value" description="Installed Check" />
    </component>  

我收到了这个错误,有什么想法吗?

'Add-WindowsFeature' 这个术语不被认为是一个 cmdlet、function、script 文件或可执行程序的名称。请检查拼写,如果包含路径,则验证路径是否正确,然后重试。 在第 1 行第 19 列 + Add-WindowsFeature <<<< -name net-framework-features + CategoryInfo : ObjectNotFound: (Add-WindowsFeature:String) [], CommandNotFoundException + FullyQualifiedErrorId : CommandNotFoundException

6个回答

4

两种选择:

1)使用脚本文件和File参数。

#############
## script.ps1
Import-Module ServerManager
Add-WindowsFeature NET-Framework-Features

然后执行:

powershell -File c:\script.ps1

2) 使用Command参数:

powershell -Command "Import-Module ServerManager; Add-WindowsFeature NET-Framework-Features"

无论如何,尽量避免使用 -ImportSystemModules 开关(在v3中已弃用),这只是一种过度操作。它将加载所有系统模块,而您所需的仅是ServerManager模块。如果您正在使用v3,则Import-Module命令也是冗余的。请参阅 autp-loading功能模块


1
我用这个批处理文件使它工作起来:

call C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -ImportSystemModules Add-WindowsFeature NET-Framework-Features

在 dotnetInstaller 引导程序中:
<component command="Install.net3.5.bat" command_silent="" command_basic="" uninstall_command="" uninstall_command_silent="" uninstall_command_basic="" returncodes_success="" returncodes_reboot="" disable_wow64_fs_redirection="True" id=".Net 3.5 SP1 Win8Server" display_name=".Net 3.5 SP1" uninstall_display_name="" os_filter="" os_filter_min="winServer2008R2" os_filter_max="" os_filter_lcid="" type="cmd" installcompletemessage="" uninstallcompletemessage="" mustreboot="False" reboot_required="" must_reboot_required="False" failed_exec_command_continue="" allow_continue_on_error="True" default_continue_on_error="False" required_install="True" required_uninstall="True" selected_install="True" selected_uninstall="True" note="" processor_architecture_filter="" status_installed="" status_notinstalled="" supports_install="True" supports_uninstall="False" show_progress_dialog="True" show_cab_dialog="True">
 <installedcheck path="SOFTWARE\Microsoft\NET Framework Setup\NDP\v3.5" fieldname="Install" fieldvalue="1" defaultvalue="False" fieldtype="REG_DWORD" comparison="match" rootkey="HKEY_LOCAL_MACHINE" wowoption="NONE" type="check_registry_value" description="Installed Check" />
      <installedcheck path="SOFTWARE\Microsoft\NET Framework Setup\NDP\v3.5" fieldname="SP" fieldvalue="1" defaultvalue="False" fieldtype="REG_DWORD" comparison="match" rootkey="HKEY_LOCAL_MACHINE" wowoption="NONE" type="check_registry_value" description="Installed Check" />
    </component>  

似乎之前无法工作是因为引导程序将bat进程启动为32位,而PS不喜欢。所以我现在加入了disable_wow64_fs_redirection="True",现在它将bat作为64位进程运行并且可以工作:)

谢谢大家的回复。我发布答案可能会帮助其他人:)


0

使用PowerShell运行命令,请使用-Command参数,如下所示:

powershell.exe -command "&{Import-Module ServerManager; ImportSystemModules Add-WindowsFeature NET-Framework-Features}"

0

这是如何工作的:

cd /D %userprofile% 
C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -Command "Import-Module ServerManager; ImportSystemModules Add-WindowsFeature NET-Framework-Features"
pause

0

您还可以使用chocolatey

choco install dotnet3.5

0

打开命令提示符并输入以下内容:

dism /online /enable-feature /featurename:NetFX3 /all /Source:d:\sources\sxs /LimitAccess

注意:源应该是 Windows 2012 安装光盘。在我的情况下,它位于 D 盘。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接