当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] 配置和管理 Windows Server Core 的基本命令

作者:精品下载站 日期:2024-12-14 23:05:52 浏览:15 分类:玩电脑

配置和管理 Windows Server Core 的基本命令


在本文中,我尝试描述用于配置和管理 Windows Server Core 的基本 cmd 和 PowerShell 命令。我认为本指南对于初学者和经验丰富的系统管理员都非常有用,可以作为基本服务器核心管理命令的参考。

服务器核心是一种特殊的Windows Server安装模式,没有大多数图形工具和管理单元。您可以从命令提示符或远程(使用 Windows Admin Center 或其他工具)管理服务器核心。

Windows Server 核心优势:

  • 较低的资源需求;

  • 提高稳定性和安全性;需要的更新较少(由于代码量和使用的组件较少);

  • 非常适合用作基础设施角色服务器(Active Directory 域控制器、DHCP 服务器、Hyper-V 主机、SMB 文件服务器等)。

Server Core 作为通用物理或虚拟 Windows Server 实例进行许可(与完全免费的 Hyper-V Server 不同)。

要以核心模式安装 Windows Server 2016/2019,必须选择典型安装。如果您选择Windows Server(桌面体验),则将安装 GUI 版本的操作系统(在以前的 Windows Server 版本中,它被称为带有 GUI 的服务器)。

[玩转系统] 配置和管理 Windows Server Core 的基本命令

如果不重新安装服务器,则无法在 Windows Server 2016/2019 中在完整 GUI 和核心模式之间切换。

Windows Server Core 安装后,系统将提示您设置本地管理员密码。

[玩转系统] 配置和管理 Windows Server Core 的基本命令

当您登录到 Server Core 时,将出现命令提示符 (cmd.exe)。如果您希望运行 PowerShell 控制台而不是它,请对注册表进行一些更改。运行以下命令:

Powershell.exe
Set-ItemProperty -Path 'HKLM:\Software\Microsoft\Windows NT\CurrentVersion\WinLogon' -Name Shell -Value 'PowerShell.exe'

并重新启动您的服务器:

Restart-Computer -Force

[玩转系统] 配置和管理 Windows Server Core 的基本命令

如果您不小心关闭了命令提示符窗口,请按

Ctrl+Alt+Delete

,打开任务管理器->文件->运行->并运行

cmd.exe

(或者

PowerShell.exe

)。

使用 SCONFIG 配置 Windows Server Core

您可以使用内置的 sconfig 脚本进行基本的服务器核心配置。只需运行

sconfig

控制台中的命令。您将看到一个包含多个项目的菜单:

[玩转系统] 配置和管理 Windows Server Core 的基本命令

使用服务器配置菜单,您可以执行以下操作:

  • 将计算机添加到域或工作组

  • 更改计算机名称(主机名)

  • 添加本地管理员

  • 允许/拒绝远程管理和 ICMP 响应

  • 配置 Windows 更新设置

  • 安装 Windows 更新

  • 启用/禁用 RDP

  • 配置网络适配器设置(IP 地址、网关、DNS 服务器)

  • 设置日期和时间

  • 更改遥测设置

  • 激活您的 Windows Server 实例

  • 注销、重新启动或关闭您的服务器

中的每一项

sconfig

有其编号。要打开您想要的项目,只需输入其

number

并按

Enter

某些 sconfig 菜单项具有子项。要进入某个设置,您还需要输入其编号。

[玩转系统] 配置和管理 Windows Server Core 的基本命令

我们不会描述所有 sconfig 设置,因为它们很容易理解。然而,在大多数情况下,管理员更喜欢使用各种 PowerShell 脚本来配置运行 Server Core 的新主机。它更容易、更快,特别是在大规模部署场景中。

配置服务器核心的基本 PowerShell 命令

让我们看一下可用于配置 Server Core 的基本 PowerShell 命令。

要获取有关 Windows Server 版本和 PowerShell 版本的信息:

Get-ComputerInfo | select WindowsProductName, WindowsVersion, OsHardwareAbstractionLayer
$PSVersionTable

[玩转系统] 配置和管理 Windows Server Core 的基本命令

要重新启动 Server Core,请运行以下 PowerShell 命令:

Restart-Computer

要注销服务器核心控制台,请使用以下命令:

logoff

使用 PowerShell 在 Server Core 上配置网络设置

现在您需要使用 PowerShell 配置网络设置(默认情况下,Windows 配置为从 DHCP 接收 IP 地址)。显示网络适配器列表:

Get-NetIPConfiguration

指定网络适配器接口的索引(

InterfaceIndex

) 您想要更改并设置新的 IP 地址和 DNS 服务器:

New-NetIPaddress -InterfaceIndex 4 -IPAddress 192.168.1.100 -PrefixLength 24 -DefaultGateway 192.168.1.1
Set-DNSClientServerAddress -InterfaceIndex 4 -ServerAddresses 192.168.1.11,192.168.101.11

[玩转系统] 配置和管理 Windows Server Core 的基本命令

检查当前网络设置:

Get-NetIPConfiguration

如果您想重置 IP 地址并返回从 DHCP 获取 IP 地址,请运行以下命令:

Set-DnsClientServerAddress -InterfaceIndex 4 -ResetServerAddresses
Set-NetIPInterface -InterfaceIndex 4 -Dhcp Enabled

启用/禁用网络适配器:

Disable-NetAdapter -Name “Ethernet0”
Enable-NetAdapter -Name “Ethernet 0”

要启用、禁用或检查网络适配器的 IPv6 支持状态:

Disable-NetAdapterBinding -Name "Ethernet0" -ComponentID ms_tcpip6
Enable-NetAdapterBinding -Name "Ethernet0" -ComponentID ms_tcpip6
Get-NetAdapterBinding -ComponentID ms_tcpip6

要为 PowerShell 或系统连接配置 winhttp 代理服务器:

netsh Winhttp set proxy <servername>:<port number>

如何设置日期/时间?

您可以使用图形工具设置日期、时间或时区

intl.cpl

或 PowerShell:

Set-Date -Date "07/21/2021 09:00"
Set-TimeZone "Central Europe Standard Time”

设置计算机名称、加入域并激活服务器核心

要更改计算机名称(主机名):

Rename-Computer -NewName be-srv01 -PassThru

[玩转系统] 配置和管理 Windows Server Core 的基本命令

要将服务器添加到本地 Active Directory 域:

Add-Computer -DomainName "corp.a-d.site" -Restart

如果要将其他用户添加到本地管理员组,可以配置组策略或手动添加:

Add-LocalGroupMember -Group "Administrators" -Member "corp\jsmith"

要激活 Windows Server,请输入您的产品密钥:

slmgr.vbs -ipk <productkey>
slmgr.vbs -ato

或者您可以在 KMS 服务器上激活您的主机。例如,要在 KMS 主机上激活 Windows Server Core 2019 Standart:

slmgr /ipk N69G4-B89J2-4G8F4-WWYCC-J464C
slmgr /skms kms.corp.a-d.site:1688
slmgr /ato

启用 Windows Server Core 的远程管理

要允许通过 RDP 远程访问 Server Core:

cscript C:\Windows\System32\Scregedit.wsf /ar 0

允许远程管理:

Configure-SMRemoting.exe -Enable
Enable-NetFirewallRule -DisplayGroup “Windows Remote Management”

显示当前远程管理设置:

Configure-SMRemoting.exe -Get

要允许 Win-RM 进行 PowerShell 远程处理:

Enable-PSRemoting -force

您可以从另一台服务器(使用 ServerManager.exe)、通过使用 Windows Admin Center (WAC) 的浏览器、使用 RSAT 从任何工作站远程管理运行 Windows Server Core 的服务器,还可以使用 RDP、PowerShell 远程处理或 SSH(当前 Windows 版本具有内置 SSH 服务器)连接到它。

在 Server Core 上配置 Windows 防火墙

您可以在本文中找到有关如何使用 PowerShell 配置 Windows Defender 防火墙的信息。我将在这里展示一些基本命令。

要为所有配置文件启用 Windows Defender 防火墙:

Set-NetFirewallProfile   -Profile Domain,Public,Private -Enabled True

要将网络类型从公共更改为专用:

Get-NetConnectionProfile | Set-NetConnectionProfile -NetworkCategory Private

要完全禁用 Windows 防火墙(不推荐):

Get-NetFirewallProfile | Set-NetFirewallProfile -enabled false

要允许使用远程管理工具进行连接:

Enable-NetFireWallRule -DisplayName “Windows Management Instrumentation (DCOM-In)”
Enable-NetFireWallRule -DisplayGroup “Remote Event Log Management”
Enable-NetFireWallRule -DisplayGroup “Remote Service Management”
Enable-NetFireWallRule -DisplayGroup “Remote Volume Management”
Enable-NetFireWallRule -DisplayGroup “Remote Scheduled Tasks Management”
Enable-NetFireWallRule -DisplayGroup “Windows Firewall Remote Management”
Enable-NetFirewallRule -DisplayGroup "Remote Administration"

如何在 Windows Server Core 上安装更新?

要管理更新选项,最好使用 Windows 更新组策略。但是,您可以手动设置更新设置。

要禁用自动更新:

Set-ItemProperty -Path HKLM:\Software\Policies\Microsoft\Windows\WindowsUpdate\AU -Name AUOptions -Value 1

自动下载可用更新:

Set-ItemProperty -Path HKLM:\Software\Policies\Microsoft\Windows\WindowsUpdate\AU -Name AUOptions -Value 3

要获取已安装更新的列表:

Get-Hotfix

或者

wmic qfe list

要手动安装 Windows 更新,您可以使用

wusa

工具:

wusa kbnamexxxxx.msu /quiet

要从命令提示符安装和管理更新,可以方便地使用 PowerShell 的 PSWindowsUpdate 模块。

管理 Windows 核心角色、服务和进程

要获取 Windows Server Core 上所有可用角色的列表,请运行以下 PowerShell 命令:

Get-WindowsFeature

[玩转系统] 配置和管理 Windows Server Core 的基本命令

要获取 Windows Server 中所有已安装角色和功能的列表(以便您可以快速了解服务器的用途):

Get-WindowsFeature | Where-Object {$_. installstate -eq "installed"} | ft Name,Installstate

例如,要安装 DNS 角色,请运行以下命令:

Install-WindowsFeature DNS -IncludeManagementTools

要获取 Windows 中所有服务的列表:

Get-Service

要查看所有停止的服务:

Get-Service | Where-Object {$_.status -eq   “stopped”}

要重新启动服务:

Restart-Service -Name spooler

了解有关使用 PowerShell 管理服务的更多信息。

要管理进程,您可以使用任务管理器 (taskmgr.exe) 或 PowerShell 进程模块:

Get-Process cmd, wuaucl* | Select-Object ProcessName, StartTime, MainWindowTitle, Path, Company|ft

有用的 Windows Server 核心命令

最后,我将展示一些我经常用来管理 Server Core 的有用的 PowerShell 命令和脚本。

物理磁盘的状态和健康信息(使用默认的Storage磁盘管理模块):

Get-PhysicalDisk | Sort Size | FT FriendlyName, Size, MediaType, SpindleSpeed, HealthStatus, OperationalStatus -AutoSize

可用磁盘空间信息:

Get-WmiObject -Class Win32_LogicalDisk |
Select-Object -Property DeviceID, VolumeName, @{Label='FreeSpace (Gb)'; expression={($_.FreeSpace/1GB).ToString('F2')}},
@{Label='Total (Gb)'; expression={($_.Size/1GB).ToString('F2')}},
@{label='FreePercent'; expression={[Math]::Round(($_.freespace / $_.size) * 100, 2)}}|ft

[玩转系统] 配置和管理 Windows Server Core 的基本命令

有关服务器最近 10 次重新启动的信息:

Get-EventLog system | where-object {$_.eventid -eq 6006} | select -last 10

已安装程序的列表:

Get-ItemProperty HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\* | Select-Object DisplayName, DisplayVersion, Publisher, InstallDate | Format-Table -AutoSize

要从外部网站下载并解压 ZIP 文件:

Invoke-WebRequest https://servername/file.zip -outfile file.zip
Expand-Archive -path '.\file.zip' -DestinationPath C:\Users\Administrator\Documents\

要将所有文件从目录复制到网络上的远程计算机,可以使用 Copy-Item cmdlet:

$session = New-PSSession -ComputerName be-dc01
Copy-Item -Path "C:\Logs\*" -ToSession $session -Destination "C:\Logs\" -Recurse -Force

要安装设备驱动程序:

Pnputil -i -a c:\install\hpspp\hpdp.inf

微软还提供了一个特殊的软件包服务器核心应用兼容性功能按需(FOD),它允许您在 Windows Core Server 2019 上安装一些图形工具和管理单元(MMC、Eventvwr、Hyper-V Manager、PerfMon、Resmon、Explorer.exe、设备管理器、Powershell ISE)。如果您的 Microsoft 订阅处于活动状态,您可以将 FOD 下载为 ISO 文件。您可以按如下方式安装它:

Add-WindowsCapability -Online -Name ServerCore.AppCompatibility~~~~0.0.1.0

服务器核心应用程序兼容性功能按需安装将在服务器核心上使用大约 200 MB 的额外 RAM。

[玩转系统] 配置和管理 Windows Server Core 的基本命令

在本文中,我尝试收集管理 Windows Server Core 所需的最有用的命令。如果日常工作需要的话,我会不时更新文章并添加新命令。

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯