如何在PowerShell中为变量分配空值?

Shi*_*wan 36 powershell scripting null

我想为一个被调用的变量赋一个空值$dec,但它给了我错误.这是我的代码:

import-module activedirectory
$domain = "domain.example.com"
$dec = null
Get-ADComputer -Filter {Description -eq $dec}
Run Code Online (Sandbox Code Playgroud)

dhc*_*cgn 48

这些都是自动变量,如$null,$true,$false等.

about_Automatic_Variables,请参阅https://technet.microsoft.com/en-us/library/hh847768.aspx?f=255&MSPPError=-2147217396

C:\PS> $a = ".dir", $null, ".pdf"
C:\PS> $a.count
3
Run Code Online (Sandbox Code Playgroud)


小智 5

使用 $dec = $null

文档中

$ null是一个包含NULL或空值的自动变量。您可以使用此变量来表示命令和脚本中缺少或未定义的值。

PowerShell将$ null视为具有值的对象,即作为显式占位符,因此您可以使用$ null表示一系列值中的空值。


not*_*tme 3

如果目标只是列出具有空描述属性的所有计算机对象,请尝试此操作

import-module activedirectory  
$domain = "domain.example.com" 
Get-ADComputer -Filter '*' -Properties Description | where { $_.Description -eq $null }
Run Code Online (Sandbox Code Playgroud)