每位 Azure VM 管理员必备的 50 条 Windows 命令(PowerShell + Active Directory)

发布: (2025年12月13日 GMT+8 02:47)
2 min read
原文: Dev.to

Source: Dev.to

此列表的存在原因

Azure 门户很好用,除非:

  • RDP 速度慢
  • 需要修复网络
  • Active Directory 出现故障
  • 正在排查数十台虚拟机

此时你需要真正的 Windows 命令。

网络

Get-NetIPAddress | Format-Table InterfaceAlias, IPAddress, PrefixLength
Resolve-DnsName azure-noob.com -Server 8.8.8.8
tracert 10.0.1.4
Test-NetConnection -ComputerName 10.0.1.4 -Port 443
route print
ipconfig /flushdns
netstat -ano | findstr ESTABLISHED

活动目录

Add-Computer -DomainName contoso.com -Credential (Get-Credential) -Restart
nltest /dsgetdc:contoso.com
repadmin /replsummary
Get-ADDomainController -Filter * | Select Name, IPv4Address
Reset-ComputerMachinePassword -Server DC01 -Credential (Get-Credential)
nltest /sc_query:contoso.com

磁盘管理

Get-Disk | Format-Table Number, FriendlyName, Size, PartitionStyle
Initialize-Disk -Number 2 -PartitionStyle GPT
New-Partition -DiskNumber 2 -UseMaximumSize -DriveLetter F
Format-Volume -DriveLetter F -FileSystem NTFS -NewFileSystemLabel "Data"
Resize-Partition -DriveLetter C -Size (Get-PartitionSupportedSize -DriveLetter C).SizeMax
Get-PhysicalDisk | Select FriendlyName, HealthStatus, OperationalStatus

服务管理

Get-Service | Where-Object {$_.Status -eq "Running"} | Format-Table Name, DisplayName
Start-Service -Name "W32Time"
Stop-Service -Name "Spooler"
Set-Service -Name "wuauserv" -StartupType Manual
Get-Service -Name "W32Time" | Select -ExpandProperty DependentServices
Get-Service WSearch | Restart-Service

性能与监控

Get-Counter '\Processor(_Total)\% Processor Time' -SampleInterval 1 -MaxSamples 5
Get-WmiObject Win32_OperatingSystem |
    Select @{N="FreeGB";E={[math]::Round($_.FreePhysicalMemory/1MB,2)}}
Get-Process | Sort-Object CPU -Descending | Select -First 10 Name, CPU, PM
Get-Counter '\PhysicalDisk(_Total)\Disk Reads/sec','\PhysicalDisk(_Total)\Disk Writes/sec'
(Get-Date) - (Get-CimInstance Win32_OperatingSystem).LastBootUpTime

Windows 更新

Get-WindowsUpdate
Install-WindowsUpdate -AcceptAll -AutoReboot
Get-WmiObject -Class Win32_QuickFixEngineering | Select HotFixID, InstalledOn

本地用户与组

Get-LocalGroupMember -Group "Administrators"
Add-LocalGroupMember -Group "Administrators" -Member "CONTOSO\john.doe"
query user
logoff 2 /server:localhost

文件权限

Get-Acl C:\Important\File.txt | Format-List

防火墙

Get-NetFirewallRule | Where Enabled -eq $true | Select Name, DisplayName
New-NetFirewallRule -DisplayName "Allow SQL" -Direction Inbound -Protocol TCP -LocalPort 1433 -Action Allow
Set-NetFirewallProfile -Profile Domain,Public,Private -Enabled $false

事件日志

Get-EventLog -LogName System -EntryType Error -Newest 20
Get-EventLog -LogName Application | Where-Object {$_.Message -like "*SQL*"}
Get-EventLog -LogName Security -InstanceId 4624 -Newest 10

Azure‑特定命令

Get-Service WindowsAzureGuestAgent
Invoke-RestMethod -Headers @{"Metadata"="true"} -Method GET -Uri "http://169.254.169.254/metadata/instance?api-version=2021-02-01"
Get-AzVMExtension -ResourceGroupName "RG-Prod" -VMName "VM-SQL-01"

系统信息

Get-ComputerInfo | Select WindowsProductName, WindowsVersion, OsBuildNumber
Get-WmiObject -Class Win32_Product | Select Name, Version
Get-WindowsDriver -Online

清理与维护

net stop wuauserv
del C:\Windows\SoftwareDistribution\*.* /s /q
net start wuauserv
Remove-Item -Path "$env:TEMP\*" -Recurse -Force -ErrorAction SilentlyContinue
Back to Blog

相关文章

阅读更多 »