WMIC(Windows Management Instrumentation Command-line)在最新版本的Windows中已被弃用,微软推荐使用Windows PowerShell来替代WMIC进行系统管理和信息查询。PowerShell提供了Get-CimInstance和Get-WmiObject命令来获取和操作WMI信息,这两个命令可以作为WMIC的替代品。

WMIC(Windows Management Instrumentation Command-line)在最新版本的Windows中已被弃用,微软推荐使用Windows PowerShell来替代WMIC进行系统管理和信息查询。PowerShell提供了Get-CimInstanceGet-WmiObject命令来获取和操作WMI信息,这两个命令可以作为WMIC的替代品。

1. Get-CimInstance 命令

Get-CimInstance是推荐使用的命令,因为它基于更现代的Windows Management Infrastructure (MI) API。与Get-WmiObject相比,Get-CimInstance提供了更好的远程管理支持和性能。

例如,如果你想要获取系统上所有进程的列表(类似于WMIC process list),可以使用:

powershellCopy Code
Get-CimInstance -ClassName Win32_Process

2. Get-WmiObject 命令

尽管Get-WmiObject是基于旧的WMI技术,但它在较老的PowerShell版本和Windows版本中仍然可用。对于一些特定的场景或者习惯了Get-WmiObject的用户,这个命令仍然是一个有用的工具。

例如,获取系统上所有服务的状态(类似于WMIC service list):

powershellCopy Code
Get-WmiObject -Class Win32_Service

替代常见WMIC命令

以下是一些常见的WMIC命令及其在PowerShell中的等价命令:

  • 获取计算机系统信息:

    • WMIC: wmic csproduct get name
    • PowerShell: Get-CimInstance -ClassName Win32_ComputerSystem | Select-Object -Property Name
  • 获取BIOS信息:

    • WMIC: wmic bios get serialnumber
    • PowerShell: Get-CimInstance -ClassName Win32_BIOS | Select-Object -Property SerialNumber
  • 查看磁盘驱动器信息:

    • WMIC: wmic diskdrive get model
    • PowerShell: Get-CimInstance -ClassName Win32_DiskDrive | Select-Object -Property Model
  • 获取操作系统信息:

    • WMIC: wmic os get caption
    • PowerShell: Get-CimInstance -ClassName Win32_OperatingSystem | Select-Object -Property Caption

总结

随着WMIC的弃用,Get-CimInstanceGet-WmiObject成为了在PowerShell中进行WMI查询的首选方法。Get-CimInstance因其现代化的设计和更好的远程管理支持而被推荐使用,但在某些情况下Get-WmiObject也可以作为一个有效的选择。通过学习这些PowerShell命令,你可以更有效地管理和查询Windows系统。


要使用 PowerShell 替代 WMIC 命令以实现相同或类似的功能,你可以利用 PowerShell 的 Get-CimInstanceGet-WmiObject 命令。下面是一些常见 WMIC 任务的 PowerShell 替代方法。

1. 获取计算机系统信息

WMIC:

shellCopy Code
wmic computersystem get name

PowerShell:

powershellCopy Code
Get-CimInstance -ClassName Win32_ComputerSystem | Select-Object -Property Name

2. 获取 BIOS 信息

WMIC:

shellCopy Code
wmic bios get serialnumber

PowerShell:

powershellCopy Code
Get-CimInstance -ClassName Win32_BIOS | Select-Object -Property SerialNumber

3. 查看磁盘驱动器信息

WMIC:

shellCopy Code
wmic diskdrive get model

PowerShell:

powershellCopy Code
Get-CimInstance -ClassName Win32_DiskDrive | Select-Object -Property Model

4. 获取操作系统信息

WMIC:

shellCopy Code
wmic os get caption

PowerShell:

powershellCopy Code
Get-CimInstance -ClassName Win32_OperatingSystem | Select-Object -Property Caption

5. 查看服务状态

WMIC:

shellCopy Code
wmic service get name, state

PowerShell:

powershellCopy Code
Get-CimInstance -ClassName Win32_Service | Select-Object -Property Name, State

6. 查看启动项

WMIC:

shellCopy Code
wmic startup list brief

PowerShell:

powershellCopy Code
Get-CimInstance -ClassName Win32_StartupCommand | Select-Object -Property Name, Command, Location, User

7. 管理进程

  • 列出进程

    WMIC:

    shellCopy Code
    wmic process list brief

    PowerShell:

    powershellCopy Code
    Get-Process
  • 终止进程

    WMIC:

    shellCopy Code
    wmic process where name="notepad.exe" delete

    PowerShell:

    powershellCopy Code
    Stop-Process -Name notepad -Force

8. 查询安装的软件

WMIC:

shellCopy Code
wmic product get name

PowerShell:

powershellCopy Code
Get-CimInstance -ClassName Win32_Product | Select-Object -Property Name

(注意:使用 Win32_Product 类可能会触发所有 MSI 安装的重新配置检查,因此请谨慎使用。)

总结

通过这些示例,你可以看到 PowerShell 提供了强大的工具来替代 WMIC 命令,实现系统管理和信息查询的任务。Get-CimInstanceGet-WmiObject 是两个非常有用的命令,它们允许你访问和操作 WMI 信息。选择哪一个取决于你的偏好和具体需求,但通常推荐使用 Get-CimInstance,因为它基于较新的 CIM 标准。


9. 管理远程桌面设置

WMIC:

shellCopy Code
wmic RDToggle where AllowTSConnections=0 call SetAllowTSConnections 1

PowerShell:

powershellCopy Code
Set-ItemProperty -Path 'HKLM:\System\CurrentControlSet\Control\Terminal Server' -name "fDenyTSConnections" -Value 0
# 重新启动远程桌面服务以应用更改
Restart-Service -Name TermService -Force

10. 获取网络适配器配置

WMIC:

shellCopy Code
wmic nicconfig get description, ipaddress

PowerShell:

powershellCopy Code
Get-CimInstance -ClassName Win32_NetworkAdapterConfiguration | Where-Object {$_.IPAddress -ne $null} | Select-Object -Property Description, IPAddress

11. 查看系统安装的更新(快速修复工程)

WMIC:

shellCopy Code
wmic qfe list

PowerShell:

powershellCopy Code
Get-HotFix

12. 管理系统服务

  • 列出所有服务

    WMIC:

    shellCopy Code
    wmic service list brief

    PowerShell:

    powershellCopy Code
    Get-Service
  • 启动服务

    WMIC:

    shellCopy Code
    wmic service where name='wuauserv' call startservice

    PowerShell:

    powershellCopy Code
    Start-Service -Name wuauserv
  • 停止服务

    WMIC:

    shellCopy Code
    wmic service where name='wuauserv' call stopservice

    PowerShell:

    powershellCopy Code
    Stop-Service -Name wuauserv -Force

13. 获取逻辑磁盘信息

WMIC:

shellCopy Code
wmic logicaldisk get name, freespace, size, drivetype

PowerShell:

powershellCopy Code
Get-CimInstance -ClassName Win32_LogicalDisk | Select-Object -Property Name, FreeSpace, Size, DriveType

14. 控制和查询环境变量

  • 列出环境变量

    WMIC:

    shellCopy Code
    wmic environment list

    PowerShell:

    powershellCopy Code
    Get-ChildItem Env:
  • 设置环境变量

    WMIC:

    shellCopy Code
    wmic environment where name='PATH' set VariableValue='your_value'

    PowerShell:

    powershellCopy Code
    $env:PATH = 'your_value'

15. 管理用户账户

  • 列出用户账户

    WMIC:

    shellCopy Code
    wmic useraccount list brief

    PowerShell:

    powershellCopy Code
    Get-CimInstance -ClassName Win32_UserAccount | Select-Object -Property Name, Disabled, PasswordRequired, PasswordChangeable

继续深入探讨 PowerShell 的能力,我们可以发现它不仅能够替代 WMIC,还能提供更加高级的数据处理和自动化功能。以下是一些进阶示例,展示如何利用 PowerShell 进行更复杂的系统管理任务。

16. 导出系统信息到 CSV 文件

WMIC: 使用 WMIC 时,你可能需要通过重定向输出到文件来实现导出功能。

PowerShell:

powershellCopy Code
Get-CimInstance -ClassName Win32_OperatingSystem | Select-Object CSName, Caption, OSArchitecture, Version | Export-Csv -Path "C:\system_info.csv" -NoTypeInformation

这个命令获取操作系统的基本信息,并将其导出到指定路径的 CSV 文件中。

17. 监视系统性能

WMIC: WMIC 提供了一些查询系统性能的方法,但不具备实时监控能力。

PowerShell:

powershellCopy Code
Get-Counter -Counter "\Processor(_Total)\% Processor Time" -SampleInterval 1 -MaxSamples 10

这个命令使用 Get-Counter 来监控 CPU 使用率,每秒采样一次,共采样10次。

18. 批量更新本地用户密码

WMIC: WMIC 可以对单个用户执行操作,但对于批量操作则较为复杂。

PowerShell:

powershellCopy Code
$users = Get-LocalUser | Where-Object { $_.Name -notlike "Administrator" }
foreach ($user in $users) {
    $password = ConvertTo-SecureString "NewPassword123" -AsPlainText -Force
    Set-LocalUser -Name $user.Name -Password $password
}

这段脚本将除管理员外的所有本地用户密码更新为 "NewPassword123"。

19. 查询并结束特定进程

WMIC:

shellCopy Code
wmic process where name="notepad.exe" delete

PowerShell:

powershellCopy Code
Get-Process notepad | Stop-Process -Force

这个 PowerShell 命令查找所有名为 "notepad" 的进程并强制结束它们,与 WMIC 功能相似但更简洁。

20. 自动化软件安装

WMIC: WMIC 可以用于查询安装的软件,但不适合进行软件部署。

PowerShell:

powershellCopy Code
Start-Process -FilePath "C:\path\to\installer.exe" -ArgumentList "/S /Q" -Wait -NoNewWindow

这个命令启动一个软件安装程序,使用 "/S /Q" 参数(取决于具体安装程序)以静默模式安装,无需用户交互。

21. 管理 Active Directory 用户

WMIC: WMIC 不直接支持 Active Directory 用户管理。

PowerShell:

powershellCopy Code
Import-Module ActiveDirectory
Get-ADUser -Filter 'Name -like "*Smith*"' | Select-Object Name, SamAccountName, Enabled

这个命令需要 Active Directory PowerShell 模块,并列出所有名字中包含 "Smith" 的 AD 用户。

22. 自动化网络配置

WMIC: WMIC 可以查询网络配置,但不适合进行复杂的网络配置任务。

PowerShell:

powershellCopy Code
New-NetIPAddress -InterfaceAlias "Ethernet0" -IPAddress 192.168.1.2 -PrefixLength 24 -DefaultGateway 192.168.1.1
Set-DnsClientServerAddress -InterfaceAlias "Ethernet0" -ServerAddresses 8.8.8.8, 8.8.4.4

这些命令配置静态 IP 地址和 DNS 服务器地址,适用于网络配置自动化。

总结

通过以上示例,我们可以看到 PowerShell 在系统管理和自动化方面的强大能力。它不仅能完成 WMIC 的所有任务,还能提供更加高级的功能,如实时性能监控、批量操作、网络配置以及与 Active Directory 的集成等。PowerShell 的强大在于其灵活性和扩展性,几乎可以实现任何 Windows 系统管理任务,是系统管理员和高级用户的强大工具。


获取系统信息

WMIC:

shellCopy Code
wmic os get name

PowerShell:

powershellCopy Code
Get-CimInstance -ClassName Win32_OperatingSystem | Select-Object Name

获取CPU信息

WMIC:

shellCopy Code
wmic cpu get name

PowerShell:

powershellCopy Code
Get-CimInstance -ClassName Win32_Processor | Select-Object Name

获取BIOS信息

WMIC:

shellCopy Code
wmic bios get serialnumber

PowerShell:

powershellCopy Code
Get-CimInstance -ClassName Win32_BIOS | Select-Object SerialNumber

列出安装的软件

WMIC:

shellCopy Code
wmic product get name

PowerShell:

powershellCopy Code
Get-CimInstance -ClassName Win32_Product | Select-Object Name

或者,针对更快速的查询,尤其是在大型系统上:

powershellCopy Code
Get-Package

获取服务状态

WMIC:

shellCopy Code
wmic service where name='Spooler' get name, state

PowerShell:

powershellCopy Code
Get-Service -Name Spooler | Select-Object Name, Status

更改服务启动类型

WMIC:

shellCopy Code
wmic service where name='wuauserv' call change startmode='disabled'

PowerShell:

powershellCopy Code
Set-Service -Name wuauserv -StartupType Disabled

获取网络适配器配置

WMIC:

shellCopy Code
wmic nicconfig where IPEnabled=true get Description, IPAddress

PowerShell:

powershellCopy Code
Get-CimInstance -ClassName Win32_NetworkAdapterConfiguration | Where-Object {$_.IPEnabled -eq $true} | Select-Object Description, IPAddress

获取磁盘信息

WMIC:

shellCopy Code
wmic logicaldisk get deviceid, volumename, freespace

PowerShell:

powershellCopy Code
Get-CimInstance -ClassName Win32_LogicalDisk | Select-Object DeviceID, VolumeName, FreeSpace

获取进程列表

WMIC:

shellCopy Code
wmic process get name, processid

PowerShell:

powershellCopy Code
Get-Process | Select-Object Name, Id

获取计算机系统时间

WMIC:

shellCopy Code
wmic os get LocalDateTime

PowerShell:

powershellCopy Code
Get-Date

获取已安装补丁列表

WMIC:

shellCopy Code
wmic qfe list

PowerShell:

powershellCopy Code
Get-HotFix

获取网络连接信息

WMIC:

shellCopy Code
wmic path Win32_PerfFormattedData_Tcpip_NetworkInterface get Name, BytesReceivedPersec, BytesSentPersec

PowerShell:

powershellCopy Code
Get-Counter '\Network Interface(*)\Bytes Received/sec', '\Network Interface(*)\Bytes Sent/sec' | Select-Object -ExpandProperty CounterSamples | Select-Object InstanceName, CookedValue

获取事件日志

WMIC:

shellCopy Code
wmic nteventlog where filename='Application' get Message

PowerShell:

powershellCopy Code
Get-WinEvent -LogName Application | Select-Object Message

 

posted @ 2024-02-04 05:43  suv789  阅读(1203)  评论(0)    收藏  举报