Ich möchte einer Variablen mit dem Namen $dec
einen Nullwert zuweisen, aber es gibt mir Fehler. Hier ist mein Code:
import-module activedirectory
$domain = "domain.example.com"
$dec = null
Get-ADComputer -Filter {Description -eq $dec}
Dies sind automatische Variablen wie $null
, $true
, $false
usw.
about_Automatic_Variables
, siehe https://technet.Microsoft.com/de-de/library/hh847768.aspx?f=255&MSPPError=-2147217396
$NULL
$null is an automatic variable that contains a NULL or empty value. You
can use this variable to represent an absent or undefined value in commands
and scripts.
Windows PowerShell treats $null as an object with a value, that is, as an
explicit placeholder, so you can use $null to represent an empty value in a
series of values.
For example, when $null is included in a collection, it is counted as one of
the objects.
C:\PS> $a = ".dir", $null, ".pdf"
C:\PS> $a.count
3
If you pipe the $null variable to the ForEach-Object cmdlet, it generates a
value for $null, just as it does for the other objects.
PS C:\ps-test> ".dir", "$null, ".pdf" | Foreach {"Hello"}
Hello
Hello
Hello
As a result, you cannot use $null to mean "no parameter value." A parameter
value of $null overrides the default parameter value.
However, because Windows PowerShell treats the $null variable as a placeholder,
you can use it scripts like the following one, which would not work if $null
were ignored.
$calendar = @($null, $null, “Meeting”, $null, $null, “Team Lunch”, $null)
$days = Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"
$currentDay = 0
foreach($day in $calendar)
{
if($day –ne $null)
{
"Appointment on $($days[$currentDay]): $day"
}
$currentDay++
}
Appointment on Tuesday: Meeting
Appointment on Friday: Team lunch
Wenn das Ziel einfach ist, alle Computerobjekte mit einem leeren Beschreibungsattribut aufzulisten, versuchen Sie dies
import-module activedirectory
$domain = "domain.example.com"
Get-ADComputer -Filter '*' -Properties Description | where { $_.Description -eq $null }
Verwenden Sie $dec = $null
Aus der Dokumentation :
$ null ist eine automatische Variable, die einen NULL- oder leeren Wert enthält. Sie können diese Variable verwenden, um einen fehlenden oder undefinierten Wert in Befehlen und Skripts darzustellen.
PowerShell behandelt $ null als ein Objekt mit einem Wert, d. H. Als expliziten Platzhalter. Sie können also $ null verwenden, um einen leeren Wert in einer Reihe von Werten darzustellen.