J'essaie d'introduire un paramètre de chaîne optionnel à ma fonction. Based on this thread should[AllowNull()]
faire l'affaire, mais PowerShell peuple encore mon paramètre avec une chaîne vide (en utilisant PowerShell version 5.1.14393.206).Paramètre de chaîne facultatif (doit être NULL)
La fonction suivante illustre le problème:
function Test-HowToManageOptionsStringParameters() {
Param(
[Parameter(Mandatory)]
[int] $MandatoryParameter,
[Parameter()]
[AllowNull()]
[string] $OptionalStringParameter = $null
)
if ($null -eq $OptionalStringParameter) {
Write-Host -ForegroundColor Green 'This works as expected';
} else {
Write-Host -ForegroundColor Red 'Damit - Parameter should be NULL';
}
}
Pour fait penser encore pire est encore ce code ne fonctionne pas (attribuer $null
au paramètre pour le test), je ne comprends vraiment pas pourquoi cela ne fonctionne pas ...
function Test-HowToManageOptionsStringParameters() {
Param(
[Parameter(Mandatory)]
[int] $MandatoryParameter,
[Parameter()]
[AllowNull()]
[string] $OptionalStringParameter = $null
)
$OptionalStringParameter = $null;
if ($null -eq $OptionalStringParameter) {
Write-Host -ForegroundColor Green 'This works as expected';
} else {
Write-Host -ForegroundColor Red 'Damit - Parameter should be NULL';
}
}