PowerShell 技能连载 - 禁止 Write-Host 语句输出

Write-Host 是将信息输出给用户的非常有用的 cmdlet,因为此输出不能被丢弃:

1
2
3
4
5
6
7
8
9
10
11
function Invoke-Test
{
"Regular Output"
Write-Host "You always see me!"
}

# both show
Invoke-Test

# Write-Host still shows
$result = Invoke-Test

不过从 PowerShell 5开始,引擎发生了悄然的变化。Write-Host 产生的输出现在也由流系统控制,并且 Write-HostWrite-Information 共享新的信息流。

如果要隐藏 Write-Host 发出的消息,只需将 #6 流重定向到 $null

1
PS> $result = Invoke-Test 6>$null

有关流和重定向的更多信息,请访问 https://powershell.one/code/9.html

PowerShell 技能连载 - 丢弃数据流

PowerShell 通过不同的流输出信息。警告写入到与输出不同的流中,而错误也写入不同的流。每个流都有一个唯一的数字标识符:

IDStream
1Output
2Error
3Warning
4Verbose
5Debug
6Information

如果要丢弃某个流,可以使用重定向运算符(“>”)并将流重定向到 $null。此行代码将丢弃任何错误或警告消息:

1
Get-Process -FileVersionInfo 2>$null 3>$null

PowerShell 技能连载 - 忽略(任何)输出

无论您做什么,PowerShell中都有(少量)命令可将信息输出到控制台。无论流重定向或赋值给 $null 都不能禁止这类命令输出,例如:

1
PS> $null = Get-WindowsUpdateLog *>&1

即使所有输出流都被丢弃,Get-WindowsUpdateLog cmdlet 仍会将大量信息写入控制台。

如果遇到这种情况,最后的方法是暂时禁用内部命令 Out-Default,如下所示:

1
2
3
4
5
6
7
8
9
10
11
# temporarily overwrite Out-Default
function Out-Default {}

# run your code (guaranteed no output)
Get-WindowsUpdateLog

# test any other direct console write
[Console]::WriteLine("Hello")

# restore Out-Default
Remove-Item -Path function:Out-Default

PowerShell 技能连载 - 识别操作系统详细信息

当您查询操作系统详细信息时,WMI 会返回一个数字:

1
2
3
PS> Get-CimInstance -ClassName Win32_OperatingSystem |
Select-Object -ExpandProperty SuiteMask
272

SuiteMask 实际上是一个位掩码,其中每个位代表一个特定的细节。要将其转换为可读的文本,请使用标志枚举:

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
$SuiteMask = @{
Name = 'SuiteMaskText'
Expression = {
[Flags()] Enum EnumSuiteMask
{
SmallBusinessServer = 1
Server2008Enterprise = 2
BackOfficeComponents = 4
CommunicationsServer = 8
TerminalServices = 16
SmallBusinessServerRestricted = 32
WindowsEmbedded = 64
DatacenterEdition = 128
TerminalServicesSingleSession = 256
HomeEdition = 512
WebServerEdition = 1024
StorageServerEdition = 8192
ComputeClusterEdition = 16384
}

[EnumSuiteMask][int]$_.SuiteMask
}
}

Get-CimInstance -ClassName Win32_OperatingSystem | Select-Object -Property Caption, SuiteMask, $SuiteMask

这将添加一个计算得出的 SuiteMaskText 属性,该属性列出了已安装的操作系统扩展:

Caption                  SuiteMask                                   SuiteMaskText
-------                  ---------                                   -------------
Microsoft Windows 10 Pro       272 TerminalServices, TerminalServicesSingleSession

PowerShell 技能连载 - 识别 Windows 类型

WMI 返回每个不同的 Windows SKU 的密码编号:

1
2
PS> Get-CimInstance -ClassName Win32_OperatingSystem | Select-Object -ExpandProperty OperatingSystemSKU
48

要将此数字转换为有意义的文本(例如出于报告目的),请尝试以下操作:

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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
$OperatingSystemSKU = @{
Name = 'OperatingSystemSKUText'
Expression = {
$value = $_.OperatingSystemSKU

switch([int]$value)
{
0 {'PRODUCT_UNDEFINED'}
1 {'PRODUCT_ULTIMATE'}
2 {'PRODUCT_HOME_BASIC'}
3 {'PRODUCT_HOME_PREMIUM'}
4 {'PRODUCT_ENTERPRISE'}
5 {'PRODUCT_HOME_BASIC_N'}
6 {'PRODUCT_BUSINESS'}
7 {'PRODUCT_STANDARD_SERVER'}
8 {'PRODUCT_DATACENTER_SERVER'}
9 {'PRODUCT_SMALLBUSINESS_SERVER'}
10 {'PRODUCT_ENTERPRISE_SERVER'}
11 {'PRODUCT_STARTER'}
12 {'PRODUCT_DATACENTER_SERVER_CORE'}
13 {'PRODUCT_STANDARD_SERVER_CORE'}
14 {'PRODUCT_ENTERPRISE_SERVER_CORE'}
15 {'PRODUCT_ENTERPRISE_SERVER_IA64'}
16 {'PRODUCT_BUSINESS_N'}
17 {'PRODUCT_WEB_SERVER'}
18 {'PRODUCT_CLUSTER_SERVER'}
19 {'PRODUCT_HOME_SERVER'}
20 {'PRODUCT_STORAGE_EXPRESS_SERVER'}
21 {'PRODUCT_STORAGE_STANDARD_SERVER'}
22 {'PRODUCT_STORAGE_WORKGROUP_SERVER'}
23 {'PRODUCT_STORAGE_ENTERPRISE_SERVER'}
24 {'PRODUCT_SERVER_FOR_SMALLBUSINESS'}
25 {'PRODUCT_SMALLBUSINESS_SERVER_PREMIUM'}
26 {'PRODUCT_HOME_PREMIUM_N'}
27 {'PRODUCT_ENTERPRISE_N'}
28 {'PRODUCT_ULTIMATE_N'}
29 {'PRODUCT_WEB_SERVER_CORE'}
30 {'PRODUCT_MEDIUMBUSINESS_SERVER_MANAGEMENT'}
31 {'PRODUCT_MEDIUMBUSINESS_SERVER_SECURITY'}
32 {'PRODUCT_MEDIUMBUSINESS_SERVER_MESSAGING'}
33 {'PRODUCT_SERVER_FOUNDATION'}
34 {'PRODUCT_HOME_PREMIUM_SERVER'}
35 {'PRODUCT_SERVER_FOR_SMALLBUSINESS_V'}
36 {'PRODUCT_STANDARD_SERVER_V'}
37 {'PRODUCT_DATACENTER_SERVER_V'}
38 {'PRODUCT_ENTERPRISE_SERVER_V'}
39 {'PRODUCT_DATACENTER_SERVER_CORE_V'}
40 {'PRODUCT_STANDARD_SERVER_CORE_V'}
41 {'PRODUCT_ENTERPRISE_SERVER_CORE_V'}
42 {'PRODUCT_HYPERV'}
43 {'PRODUCT_STORAGE_EXPRESS_SERVER_CORE'}
44 {'PRODUCT_STORAGE_STANDARD_SERVER_CORE'}
45 {'PRODUCT_STORAGE_WORKGROUP_SERVER_CORE'}
46 {'PRODUCT_STORAGE_ENTERPRISE_SERVER_CORE'}
47 {'PRODUCT_STARTER_N'}
48 {'PRODUCT_PROFESSIONAL'}
49 {'PRODUCT_PROFESSIONAL_N'}
50 {'PRODUCT_SB_SOLUTION_SERVER'}
51 {'PRODUCT_SERVER_FOR_SB_SOLUTIONS'}
52 {'PRODUCT_STANDARD_SERVER_SOLUTIONS'}
53 {'PRODUCT_STANDARD_SERVER_SOLUTIONS_CORE'}
54 {'PRODUCT_SB_SOLUTION_SERVER_EM'}
55 {'PRODUCT_SERVER_FOR_SB_SOLUTIONS_EM'}
56 {'PRODUCT_SOLUTION_EMBEDDEDSERVER'}
57 {'PRODUCT_SOLUTION_EMBEDDEDSERVER_CORE'}
58 {'PRODUCT_PROFESSIONAL_EMBEDDED'}
59 {'PRODUCT_ESSENTIALBUSINESS_SERVER_MGMT'}
60 {'PRODUCT_ESSENTIALBUSINESS_SERVER_ADDL'}
61 {'PRODUCT_ESSENTIALBUSINESS_SERVER_MGMTSVC'}
62 {'PRODUCT_ESSENTIALBUSINESS_SERVER_ADDLSVC'}
63 {'PRODUCT_SMALLBUSINESS_SERVER_PREMIUM_CORE'}
64 {'PRODUCT_CLUSTER_SERVER_V'}
65 {'PRODUCT_EMBEDDED'}
66 {'PRODUCT_STARTER_E'}
67 {'PRODUCT_HOME_BASIC_E'}
68 {'PRODUCT_HOME_PREMIUM_E'}
69 {'PRODUCT_PROFESSIONAL_E'}
70 {'PRODUCT_ENTERPRISE_E'}
71 {'PRODUCT_ULTIMATE_E'}
72 {'PRODUCT_ENTERPRISE_EVALUATION'}
76 {'PRODUCT_MULTIPOINT_STANDARD_SERVER'}
77 {'PRODUCT_MULTIPOINT_PREMIUM_SERVER'}
79 {'PRODUCT_STANDARD_EVALUATION_SERVER'}
80 {'PRODUCT_DATACENTER_EVALUATION_SERVER'}
84 {'PRODUCT_ENTERPRISE_N_EVALUATION'}
85 {'PRODUCT_EMBEDDED_AUTOMOTIVE'}
86 {'PRODUCT_EMBEDDED_INDUSTRY_A'}
87 {'PRODUCT_THINPC'}
88 {'PRODUCT_EMBEDDED_A'}
89 {'PRODUCT_EMBEDDED_INDUSTRY'}
90 {'PRODUCT_EMBEDDED_E'}
91 {'PRODUCT_EMBEDDED_INDUSTRY_E'}
92 {'PRODUCT_EMBEDDED_INDUSTRY_A_E'}
95 {'PRODUCT_STORAGE_WORKGROUP_EVALUATION_SERVE'}
96 {'PRODUCT_STORAGE_STANDARD_EVALUATION_SERVER'}
97 {'PRODUCT_CORE_ARM'}
98 {'PRODUCT_CORE_N'}
99 {'PRODUCT_CORE_COUNTRYSPECIFIC'}
100 {'PRODUCT_CORE_SINGLELANGUAGE'}
101 {'PRODUCT_CORE'}
103 {'PRODUCT_PROFESSIONAL_WMC'}
105 {'PRODUCT_EMBEDDED_INDUSTRY_EVAL'}
106 {'PRODUCT_EMBEDDED_INDUSTRY_E_EVAL'}
107 {'PRODUCT_EMBEDDED_EVAL'}
108 {'PRODUCT_EMBEDDED_E_EVAL'}
109 {'PRODUCT_NANO_SERVER'}
110 {'PRODUCT_CLOUD_STORAGE_SERVER'}
111 {'PRODUCT_CORE_CONNECTED'}
112 {'PRODUCT_PROFESSIONAL_STUDENT'}
113 {'PRODUCT_CORE_CONNECTED_N'}
114 {'PRODUCT_PROFESSIONAL_STUDENT_N'}
115 {'PRODUCT_CORE_CONNECTED_SINGLELANGUAGE'}
116 {'PRODUCT_CORE_CONNECTED_COUNTRYSPECIFIC'}
117 {'PRODUCT_CONNECTED_CAR'}
118 {'PRODUCT_INDUSTRY_HANDHELD'}
119 {'PRODUCT_PPI_PRO'}
120 {'PRODUCT_ARM64_SERVER'}
121 {'PRODUCT_EDUCATION'}
122 {'PRODUCT_EDUCATION_N'}
123 {'PRODUCT_IOTUAP'}
124 {'PRODUCT_CLOUD_HOST_INFRASTRUCTURE_SERVER'}
125 {'PRODUCT_ENTERPRISE_S'}
126 {'PRODUCT_ENTERPRISE_S_N'}
127 {'PRODUCT_PROFESSIONAL_S'}
128 {'PRODUCT_PROFESSIONAL_S_N'}
129 {'PRODUCT_ENTERPRISE_S_EVALUATION'}
130 {'PRODUCT_ENTERPRISE_S_N_EVALUATION'}
135 {'PRODUCT_HOLOGRAPHIC'}
138 {'PRODUCT_PRO_SINGLE_LANGUAGE'}
139 {'PRODUCT_PRO_CHINA'}
140 {'PRODUCT_ENTERPRISE_SUBSCRIPTION'}
141 {'PRODUCT_ENTERPRISE_SUBSCRIPTION_N'}
143 {'PRODUCT_DATACENTER_NANO_SERVER'}
144 {'PRODUCT_STANDARD_NANO_SERVER'}
145 {'PRODUCT_DATACENTER_A_SERVER_CORE'}
146 {'PRODUCT_STANDARD_A_SERVER_CORE'}
147 {'PRODUCT_DATACENTER_WS_SERVER_CORE'}
148 {'PRODUCT_STANDARD_WS_SERVER_CORE'}
149 {'PRODUCT_UTILITY_VM'}
159 {'PRODUCT_DATACENTER_EVALUATION_SERVER_CORE'}
160 {'PRODUCT_STANDARD_EVALUATION_SERVER_CORE'}
161 {'PRODUCT_PRO_WORKSTATION'}
162 {'PRODUCT_PRO_WORKSTATION_N'}
164 {'PRODUCT_PRO_FOR_EDUCATION'}
165 {'PRODUCT_PRO_FOR_EDUCATION_N'}
168 {'PRODUCT_AZURE_SERVER_CORE'}
169 {'PRODUCT_AZURE_NANO_SERVER'}
171 {'PRODUCT_ENTERPRISEG'}
172 {'PRODUCT_ENTERPRISEGN'}
175 {'PRODUCT_SERVERRDSH'}
178 {'PRODUCT_CLOUD'}
179 {'PRODUCT_CLOUDN'}
180 {'PRODUCT_HUBOS'}
182 {'PRODUCT_ONECOREUPDATEOS'}
183 {'PRODUCT_CLOUDE'}
184 {'PRODUCT_ANDROMEDA'}
185 {'PRODUCT_IOTOS'}
186 {'PRODUCT_CLOUDEN'}
default {"$value"}
}

}
}

Get-CimInstance -ClassName Win32_OperatingSystem | Select-Object -Property Caption, OperatingSystemSKU, $OperatingSystemSKU

输出将添加一个具有友好名称的新 OperatingSystemSKUText 属性:

Caption                  OperatingSystemSKU OperatingSystemSKUText
-------                  ------------------ ----------------------
Microsoft Windows 10 Pro                 48 PRODUCT_PROFESSIONAL

PowerShell 技能连载 - 操作系统的启动和安装时间

WMI 类 Win32_OperatingSystem 提供了有关许多日期时间信息的丰富信息,包括上次启动的日期和安装时间:

1
2
3
$dateTimeProps = 'InstallDate', 'LastBootupTime', 'LocalDateTime', 'CurrentTimeZone', 'CountryCode'

Get-CimInstance -ClassName Win32_OperatingSystem | Select-Object -Property $dateTimeProps

结果看起来像这样:

InstallDate     : 03.09.2019 12:42:41
LastBootupTime  : 03.05.2020 12:15:45
LocalDateTime   : 04.05.2020 10:43:55
CurrentTimeZone : 120
CountryCode     : 49

如果您想知道系统运行了多少分钟,或者自安装以来已经过去了几天,请使用 New-TimeSpan:

1
2
3
4
5
6
7
$os = Get-CimInstance -ClassName Win32_OperatingSystem

$installedDays = (New-TimeSpan -Start $os.InstallDate).Days
$runningMinutes = [int](New-TimeSpan -Start $os.LastBootupTime).TotalMinutes

"Your copy of Windows was installed $installedDays days ago."
"Your system is up for {0:n0} minutes." -f $runningMinutes

结果看起来像这样:

Your copy of Windows was installed 243 days ago.
Your system is up for 1.353 minutes.

Get-CimInstance cmdlet可用于在本地和远程计算机上查询信息(前提是您具有适当的权限)。有关如何远程使用Get-CimInstance的更多信息,请访问https://powershell.one/wmi/remote-access

PowerShell 技能连载 - 改变操作系统描述

每个 Windows 操作系统都有一个描述,您可以使用以下命令查看(和更改)该描述:

1
PS> control sysdm.cpl

要通过 PowerShell 自动执行此操作,请使用以下命令:

1
2
3
4
5
6
7
8
9
10
11
12
13
# change operating system description
# (requires admin privileges)
$values = @{
Description = 'My Computer'
}
Set-CimInstance -Query 'Select * from Win32_OperatingSystem' -Property $values

# read description
# (no admin privileges required)
$description = Get-CimInstance -ClassName Win32_OperatingSystem |
Select-Object -ExpandProperty Description

"OS Description: $description"

PowerShell 技能连载 - 在网格视图窗口中将对象数据显示为表格

通常,当将单个对象输出到 Out-GridView 时,将得到一行数据,并且每个属性都以一列的形式出现:

1
Get-ComputerInfo | Select-Object -Property * | Out-GridView

这样难以查看和过滤特定信息。只需将对象转换为有序哈希表,即可将其显示为网格视图窗口中的表。此外,您现在还可以消除空属性并确保对属性进行排序:

1
2
3
4
5
6
# make sure you have exactly ONE object
$info = Get-ComputerInfo
# find names of non-empty properties
$filledProperties = $info.PSObject.Properties.Name.Where{![string]::IsNullOrWhiteSpace($info.$_)} | Sort-Object
# turn object into a hash table and show in a grid view window
$filledProperties | ForEach-Object { $hash = [Ordered]@{} } { $hash[$_] = $info.$_ } { $hash } | Out-GridView

只要 $info 恰好包含一个对象,该方法就可以完美地工作。例如,您可以调整代码,并使用 “Get-AdUser -Identify SomeName -Properties *“ 代替 “Get-ComputerInfo“ 来列出给定用户的所有 Active Directory 属性。只要确保您精确地指定了一个用户即可。

由于此方法将对象转换为键值对,因此不适用于多个对象。

PowerShell 技能连载 - 移除空白的属性

WMI 和 Get-CimInstance 可以为您提供许多有用的信息,但是返回的对象通常包含许多空属性:

1
PS> Get-CimInstance -ClassName Win32_OperatingSystem | Select-Object -Property *

另外,属性不一定要排序。您可以通过识别和排序不为空的属性来进行修复:

1
2
3
4
5
6
# get all WMI information
$os = Get-CimInstance -ClassName Win32_OperatingSystem
# find names of non-empty properties
$filledProperties = $os.PSObject.Properties.Name.Where{![string]::IsNullOrWhiteSpace($os.$_)} | Sort-Object
# show non-empty properties only
$os | Select-Object -Property $filledProperties

PowerShell 技能连载 - 使用断言

通常,您的代码需要声明某些先决条件。例如,您可能要确保给定的文件夹存在,并使用如下代码:

1
2
3
4
5
6
7
8
9
10
11
# path to download files to
$OutPath = "$env:temp\SampleData"

# does it already exist?
$exists = Test-Path -Path $OutPath -PathType Container

# no, create it
if (!$exists)
{
$null = New-Item -Path $OutPath -ItemType Directory
}

您可以开始使用断言函数库,而不必一遍又一遍地写代码。这是确保文件夹存在的一种:

1
2
3
4
5
6
7
8
filter Assert-FolderExists
{
$exists = Test-Path -Path $_ -PathType Container
if (!$exists) {
Write-Warning "$_ did not exist. Folder created."
$null = New-Item -Path $_ -ItemType Directory
}
}

使用此函数,您的代码将变得更加整洁。这些代码将文件夹路径分配给变量,并同时确保文件夹存在:

1
2
3
4
5
# making sure a bunch of folders exist
'C:\test1', 'C:\test2' | Assert-FolderExists

# making sure the path assigned to a variable exists
($Path = 'c:\test3') | Assert-FolderExists

在此处阅读有关此技术的更多信息:https://powershell.one/code/10.html