[TOC]

0x00 操作系统信息查看

Get-ComputerInfo 命令 - 获取系统和操作系统属性的合并对象。

描述: 该cmdlet获取系统和操作系统属性的合并对象(OS/Bios/计算机系统信息/处理等信息),此cmdlet在Windows PowerShell 5.1中引入。

语法&参数:

1
Get-ComputerInfo [[-Property] <System.String[]>] [<CommonParameters>]

基础示例:

1
2
3
4
5
6
7
8
# 示例1.获取所有计算机属性
Get-ComputerInfo

# 示例2.获取所有计算机操作系统属性
$OS = Get-ComputerInfo -Property "os*"
Write-Host $OS.OsName $OS.OsVersion - $OS.OsArchitecture
# Microsoft Windows 10 专业版 10.0.19041 - 64 位
Write-Output $OS.OsHotFixes # 安全更新

补充示例:

  • 0) 基础操作系统信息利用Get-WmiObject -Class Win32_OperatingSystem命令查看

    1
    2
    3
    4
    5
    6
    SystemDirectory : C:\WINDOWS\system32
    Organization :
    BuildNumber : 19041
    RegisteredUser : WeiyiGeek
    SerialNumber : 00331-10010-00111-AA535
    Version : 10.0.19041
  • 1) PowerShell结合systeminfo.exe提取当前计算机信息可以当做替代方案(值得学习)

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    function Get-SystemInfo
    {
    param($ComputerName = $env:COMPUTERNAME)
    $header = 'Hostname','OSName','OSVersion','OSManufacturer','OSConfiguration','OS Build Type','RegisteredOwner','RegisteredOrganization','Product ID','Original Install Date','System Boot Time','System Manufacturer','System Model','System Type','Processor(s)','BIOS Version','Windows Directory','System Directory','Boot Device','System Locale','Input Locale','Time Zone','Total Physical Memory','Available Physical Memory','Virtual Memory: Max Size','Virtual Memory: Available','Virtual Memory: In Use','Page File Location(s)','Domain','Logon Server','Hotfix(s)','Network Card(s)'
    systeminfo.exe /FO CSV /S $ComputerName |Select-Object -Skip 1 | ConvertFrom-CSV -Header $header
    }

    Hostname : WEIYIGEEK
    OSName : Microsoft Windows 10 专业版
    OSVersion : 10.0.19041 暂缺 Build 19041
    OSManufacturer : Microsoft Corporation
    OSConfiguration : 独立工作站
    OS Build Type : Multiprocessor Free
    RegisteredOwner : WeiyiGeek
    RegisteredOrganization : 暂缺
    Product ID : 00331-10000-00001-AA535
    Original Install Date : 2020/11/5, 0:09:24
    System Boot Time : 2021/5/19, 8:50:20
    System Manufacturer : Dell Inc.
    System Model : OptiPlex 9020
    System Type : x64-based PC
    Processor(s) : 安装了 1 个处理器。,[01]: Intel64 Family 6 Model 60 Stepping 3 GenuineIntel ~3301 Mhz
    BIOS Version : Dell Inc. A11, 2015/4/1
    Windows Directory : C:\WINDOWS
    System Directory : C:\WINDOWS\system32
    Boot Device : \Device\HarddiskVolume1
    System Locale : zh-cn;中文(中国)
    Input Locale : zh-cn;中文(中国)
    Time Zone : (UTC+08:00) 北京,重庆,香港特别行政区,乌鲁木齐
    Total Physical Memory : 16,326 MB
    Available Physical Memory : 9,326 MB
    Virtual Memory: Max Size : 30,582 MB
    Virtual Memory: Available : 20,801 MB
    Virtual Memory: In Use : 9,781 MB
    Page File Location(s) : C:\pagefile.sys
    Domain : WORKGROUP
    Logon Server : \\WEIYIGEEK
    Hotfix(s) : 安装了 8 个修补程序。,[01]: KB4601554,[02]: KB4577266,[03]: KB4580325,[04]: KB4586864,[05]: KB4593175,[06]: KB4598481,[07]: KB5003173,[08]: KB5003242
    Network Card(s) : 安装了 5 个 NIC。,[01]: TAP-Windows Adapter V9, 连接名: 以太网 2, 状态: 媒体连接已中断,[02]: TAP-Windows Adapter V9, 连接名: 以太网 3, 状态: 媒体连接已中断,[03]: VMware Virtual Ethernet Adapter for VMnet1, 连接名:
    VMware Network Adapter VMnet1, 启用 DHCP: 否, IP 地址, [01]: 192.168.151.253, [02]: 192.168.1.253, [03]: fe80::6dfc:881b:1f89:55f3,[04]: VMware Virtual Ethernet Adapter for VMnet8, 连接名: VMware Network Adapter VMnet
    8, 启用 DHCP: 是, DHCP 服务器: 192.168.150.254, IP 地址, [01]: 192.168.150.1, [02]: fe80::f469:f9fb:99bc:5c22,[05]: Intel(R) Ethernet Connection I217-LM, 连接名: 以太网, 启用 DHCP: 否, IP 地址, [01]: 10.20.172.1
    03, [02]: fe80::d97d:fe6c:10bf:4244
  • 2) 该Cmdlet获取的主要信息可以作为以后提取参考
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    # - Windos系统相关信息
    WindowsProductName : Windows Server 2019 Standard
    WindowsEditionId : ServerStandard
    WindowsInstallationType : Server Core
    WindowsCurrentVersion : 6.3
    WindowsVersion : 1809
    WindowsProductId : 00429-00000-00001-AA739

    # - Bios 相关信息
    BiosManufacturer : VMware, Inc.
    BiosFirmwareType : Uefi
    BiosName : VMW71.00V.17369862.B64.2012240522
    BiosVersion : INTEL - 6040000
    BiosBIOSVersion : {INTEL - 6040000, VMW71.00V.17369862.B64.2012240522, VMware, Inc. - 10000}
    BiosSeralNumber : VMware-56 4d 17 fd ab 99 b0 87-c8 8f 44 08 4b 42 0b b6
    CsBootupState : Normal boot
    OsBootDevice : \Device\HarddiskVolume2
    BiosReleaseDate : 2020/12/24 8:00:00

    # - 操作系统相关信息
    CsName : WEIYIGEEK
    CsAdminPasswordStatus : Enabled
    CsManufacturer : VMware, Inc.
    CsModel : VMware7,1
    OsName : Microsoft Windows 10 专业版
    OsType : WINNT
    OsProductType : Server
    OsServerLevel : ServerCore
    OsArchitecture : 64 位
    OsOperatingSystemSKU : 48
    OsVersion : 10.0.19041
    OsBuildNumber : 17763
    OsSerialNumber : 00429-00000-00001-AA739
    OsInstallDate : 2021/5/19 11:42:45
    OsSystemDevice : \Device\HarddiskVolume4
    OsSystemDirectory : C:\Windows\system32
    OsCountryCode : 86
    OsCodeSet : 936
    OsLocaleID : 0804
    OsCurrentTimeZone : 480
    TimeZone : (UTC+08:00) 北京,重庆,香港特别行政区,乌鲁木齐
    OsLanguage : zh-CN
    OsLocalDateTime : 2021/5/22 10:44:26
    OsLastBootUpTime : 2021/5/19 17:47:49

    # - 处理器相关信息
    CsProcessors : {Intel(R) Xeon(R) CPU E3-1271 v3 @ 3.60GHz, Intel(R) Xeon(R) CPU E3-1271 v3 @ 3.60GHz, Intel(R) Xeon(R) CPU E3-1271 v3 @ 3.60GHz, Intel(R) Xeon(R) CPU E3-1271 v3 @ 3.60GHz}
    OsBuildType : Multiprocessor Free
    CsNumberOfProcessors : 4
    CsNumberOfLogicalProcessors : 4
    OsMaxNumberOfProcesses : 4294967295

    # - 内存相关信息
    OsTotalVisibleMemorySize : 4193312
    OsFreePhysicalMemory : 2941040
    OsTotalVirtualMemorySize : 5635104
    OsFreeVirtualMemory : 4460064
    OsInUseVirtualMemory : 1175040
    OsMaxProcessMemorySize : 137438953344

    # - 网卡相关信息
    CsNetworkAdapters : {Ethernet0}

    # - 系统补丁信息
    OsHotFixes : {KB4601554, KB4577266, KB4580325, KB4586864...}

0x01 磁盘资源信息查看

Get-PSDrive 命令 - 获取当前会话中的驱动器

描述: Get-PSDrive cmdlet 不仅显示由提供程序公开的驱动器,而且还显示 Windows 逻辑驱动器,其中包括映射到网络共享的驱动器。

基础语法:

1
2
3
4
# 语法
Get-PSDrive [-LiteralName] <System.String[]> [-PSProvider <System.String[]>] [-Scope <System.String>] [-UseTransaction] [<CommonParameters>]

Get-PSDrive [[-Name] <System.String[]>] [-PSProvider <System.String[]>] [-Scope <System.String>] [-UseTransaction] [<CommonParameters>]

基础实例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# 1.此命令获取当前会话中的驱动器 (Alias:, Cert:, Env:, Function:, HKCU:, HKLM:, and Variable:).。
PS C:\> Get-PSDrive
Name Used (GB) Free (GB) Provider Root CurrentLocation
Alias Alias
C 127.50 21.94 FileSystem C:\ Users\WeiyiGeek
Cert Certificate \
D 28.61 232.39 FileSystem D:\
E 67.53 192.47 FileSystem E:\
Env Environment
F 192.02 68.26 FileSystem F:\
Function Function
G FileSystem G:\
H 105.37 826.11 FileSystem H:\
HKCU Registry HKEY_CURRENT_USER
HKLM Registry HKEY_LOCAL_MACHINE
I FileSystem I:\
K 408.66 522.82 FileSystem K:\
Variable Variable
WSMan WSMan
Z 167.71 763.80 FileSystem \\10.20.10.101\Work


# 2.获取在电脑上安装一个驱动器信息
PS C:\foo> Get-PSDrive D
# Name Used (GB) Free (GB) Provider Root
# ---- --------- --------- -------- ----
# D 1211.06 123642.32 FileSystem D:\


# 3.获取Windows PowerShell文件系统提供程序支持的所有驱动器
PS C:\> Get-PSDrive -PSProvider FileSystem
Name Used (GB) Free (GB) Provider Root
---- --------- --------- -------- ----
A A:\
C 202.06 23718.91 FileSystem C:\
D 1211.06 123642.32 FileSystem D:\
G 202.06 710.91 FileSystem \\Music\GratefulDead


# 4.检查驱动器是否用作Windows PowerShell驱动器名(便于后续判断是否存在该驱动器)
if (Get-PSDrive X -ErrorAction SilentlyContinue) {
Write-Host 'The X: drive is already in use.'
} else {
New-PSDrive -Name X -PSProvider Registry -Root HKLM:\SOFTWARE # 后续讲解
}


# 5.比较文件系统驱动器的类型以及多种方式获取磁盘信息
PS C:\> Get-PSDrive -PSProvider FileSystem
PS C:\> net use
# New connections will be remembered.
# Status Local Remote Network
# -------------------------------------------------------------------------------
# OK Z: \\10.20.10.101\Work Microsoft Windows Network

PS C:\> [System.IO.DriveInfo]::GetDrives() | Format-Table
# Name DriveType DriveFormat IsReady AvailableFreeSpace TotalFreeSpace TotalSize RootDirectory VolumeLabel
# ---- --------- ----------- ------- ------------------ -------------- --------- ------------- -----------
# C:\ Fixed NTFS True 23552114688 23552114688 160457101312 C:\ OS
# D:\ Fixed NTFS True 249524043776 249524043776 280247660544 D:\ 软件
# Z:\ Network NTFS True 820123381760 820123381760 ...201224192 Z:\

PS N:\> Get-CimInstance -Class Win32_LogicalDisk
# DeviceID DriveType ProviderName VolumeName Size FreeSpace
# -------- --------- ------------ ---------- ---- ---------
# C: 3 OS 160457101312 23552049152
# D: 3 软件 280247660544 249524043776
# G: 5
# Z: 4 \\10.20.10.101\Work 1000201224192 820123381760

PS C:\> Get-CimInstance -Class Win32_NetworkConnection
# LocalName RemoteName ConnectionState Status
# --------- ---------- --------------- ------
# Z: \\10.20.10.101\Work Connected OK


New-PSDrive 命令 - 创建临时和永久映射的网络驱动器。

描述:该New-PSDrivecmdlet会创建与数据存储中的某个位置映射或关联的临时和持久驱动器,例如网络驱动器,本地计算机上的目录或注册表项,以及与Windows关联的持久Windows映射的网络驱动器。远程计算机上的文件系统位置。

语法参数:

1
New-PSDrive [-Name] <System.String> [-PSProvider] <System.String> [-Root] <System.String> [-Confirm] [-Credential <System.Management.Automation.PSCredential>] [-Description <System.String>] [-Persist] [-Scope <System.String>] [-UseTransaction] [-WhatIf] [<CommonParameters>]

基础示例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# 1.创建一个映射到网络共享的临时PowerShell驱动器。
New-PSDrive -Name "WeiyiGeek" -PSProvider "FileSystem" -Root "\\10.20.17.103\Temp"
# Name Used (GB) Free (GB) Provider Root
# ---- --------- --------- -------- ----
# WeiyiGeek FileSystem \\10.20.17.103\Temp
Get-ChildItem -Path WeiyiGeek: # 从PowerShell会话中查看内容(下面创建的驱动器都可以采用此种方式查看)
# 目录: \\10.20.172.103\Temp
# Mode LastWriteTime Length Name
# ---- ------------- ------ ----
# d----- 2020/9/22 9:18 5小更新包update2

# 2.创建映射到本地目录的临时驱动器(效果同上只不过是本地的映射)
$parameters = @{
Name = "myHome"
PSProvider = "FileSystem"
Root = "C:\Users\weiyigeek\Documents"
Description = "Maps to my My Documents folder."
}
New-PSDrive @parameters

# 3.创建一个名为MyCompany的驱动器,该驱动器映射到HKLM:\Software\MyCompany注册表项。
New-PSDrive -Name "MyCompany" -PSProvider "Registry" -Root "HKLM:\Software\MyCompany"
# Name Provider Root
# ---- -------- ----
# MyCompany Registry HKLM:\Software\MyCompany

# 4.使用凭据创建一个持久性映射的网络驱动器
# 请记住,如果在脚本中使用代码段请将Scope参数值设置为“ Global”,以确保驱动器在当前范围之外仍然存在。
$cred = Get-Credential -Credential WEIYIGEEK\WeiyiGeek
New-PSDrive -Name "x" -Persist -Scope Global -Root "\\10.20.172.103\Temp" -PSProvider "FileSystem" -Credential $cred
# Name Used (GB) Free (GB) Provider Root CurrentLocation
# ---- --------- --------- -------- ---- ---------------
# x 28.77 232.23 FileSystem \\10.20.17.103\Temp

# 5.创建永久和临时驱动器
# 两者区别: 如果您关闭PowerShell会话,然后打开一个新会话则该临时文件夹PSDrive:将不可用,但永久X:驱动器将可用
New-PSDrive -Name "PSDrive" -PSProvider "FileSystem" -Root "\\Server01\Public"
New-PSDrive -Persist -Name "X" -PSProvider "FileSystem" -Root "\\Server01\Public" -Scope Global
Get-PSDrive -Name "PSDrive", "X"
Get-PSDrive "PSDrive", "x" | Get-Member
Net Use && Get-CimInstance Win32_LogicalDisk | Format-Table -Property DeviceID
Get-CimInstance Win32_NetworkConnection
# Status Local Remote Network
# --------------------------------------------------------
# OK X: \\contoso-pc\data Microsoft Windows Network
# deviceid
# --------
# C:
# D:
# X:
# LocalName RemoteName ConnectionState Status
# --------- ---------- --------------- ------
# X: \\products\public Disconnected Unavailable

# 6.创建注册表驱动器提供程序(注意默认得注册表提供程序是HKCU和HKLM)而注册表包含的根节点远不止两个此时我们可以采用此cmdlet命令进行创建新的驱动器
# 计算机\HKEY_CLASSES_ROOT = HKCR (事实上 HKEY_CLASSES_ROOT 根节点不是一个独立的根节点而是指向 HKEY_LOCAL_MACHINE\SOFTWARE\Classes )
# 计算机\HKEY_CURRENT_USER = HKCU
# 计算机\HKEY_LOCAL_MACHINE = HKLM
# 计算机\HKEY_USERS = HKUS
# 计算机\HKEY_CURRENT_CONFIG = HKCC (事实上 HKEY_CURRENT_CONFIG 根节点不是一个独立的根节点而是指向 HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Hardware Profiles\Current )
New-PSDrive -name HKCR -PSProvider registry -root HKLM:\SOFTWARE\Classes
New-PSDrive -name HKUS -PSProvider registry -root HKEY_USERS
New-PSDrive -name HKCC -PSProvider registry -root 'HKLM:\SYSTEM\CurrentControlSet\Hardware Profiles\Current'
PS C:\> Set-Location 'HKUS:\' # 采用注册表驱动器别名进入
PS HKUS:\> Get-Item *
# Hive: HKEY_USERS
# Name Property
# ---- --------
# .DEFAULT
# S-1-5-19
# S-1-5-20
# S-1-5-21-923396991-3399624210- WordWrap : 1
# 3190802951-1001
# S-1-5-21-923396991-3399624210-
# 3190802951-1001_Classes
S-1-5-18

# Tips.你可以自由地创建任何额外的驱动器,尤其在你有频繁操作某个特定的注册表区域时。
New-PSDrive -name CV -PSProvider registry -root "HKLM:\Software\Microsoft\Windows NT\CurrentVersion"
# Name Used (GB) Free (GB) Provider Root CurrentLocation
# CV Registry HKEY_LOCAL_MACHINE\Software\Micr...
Get-ItemProperty -Path "CV:\"


Remove-PSDrive 命令 - 删除临时PowerShell驱动器并断开映射的网络驱动器。

描述: 从Windows PowerShell 3.0开始,当外部驱动器连接到计算机时,PowerShell会自动将PSDrive添加到表示新驱动器的文件系统中。您不需要重新启动PowerShell。类似地当外部驱动器与计算机断开连接时,它会自动删除代表已删除驱动器的PSDrive。,包括但不限于使用New PSDrive的Persist参数创建的驱动器。
Tips :该cmdlet无法删除Windows物理或逻辑驱动器。

语法参数:

1
2
3
4
5
6
7
8
Remove-PSDrive
[-Name] <String[]> | [-LiteralName] <String[]>
[-PSProvider <String[]>]
[-Scope <String>]
[-Force]
[-WhatIf]
[-Confirm]
[<CommonParameters>]


简单示例:

1
2
3
4
5
6
7
8
# 1.此命令将删除名为“weiyigeek”的临时文件系统驱动器(操作之前需要确认)。
Remove-PSDrive -Name weiyigeek -Confirm
# 是否确实要执行此操作?
# 正在目标“名称: X 提供程序: Microsoft.PowerShell.Core\FileSystem 根路径: X:\”上执行操作“删除驱动器”。
# [Y] 是(Y) [A] 全是(A) [N] 否(N) [L] 全否(L) [S] 暂停(S) [?] 帮助 (默认值为“Y”):

# 2.此命令用于Remove-PSDrive断开X:和S:映射的网络驱动器。
Get-PSDrive X, S | Remove-PSDrive