ویژگی AllowEmptyString() با متغیر رشته و AllowEmptyCollection() با آرایه انواع داده های مختلف (Collection) کار می کند.
مثال زیر را در نظر بگیرید. در اینجا، از یک متغیر اجباری $name که یک رشته است و $stringarray که یک آرایه رشته ای است استفاده می کنیم .
function print_String{ [cmdletbinding()] param( [parameter(Mandatory=$True)] [string]$name, ) Write-Output "Writing a single string" $name }
اگر خروجی متغیر بالا را دریافت کنیم، در زیر خطا ایجاد می کند.
PS C:WINDOWSsystem32> print_String cmdlet print_String at command pipeline position 1 Supply values for the following parameters: name: print_String : Cannot bind argument to parameter 'name' because it is an empty string. At line:1 char:1 + print_String + ~~~~~~~~~~~~ + CategoryInfo : InvalidData: (:) [print_String], ParameterBindin g ValidationException + FullyQualifiedErrorId : ParameterArgumentValidationErrorEmptyStringNotAl l owed,print_String
همانطور که در خروجی بالا مشاهده می کنید، اسکریپت رشته خالی را نمی پذیرد. برای اجازه دادن به رشته خالی، باید از آرگومان AllowEmptyString() استفاده کنید ، بنابراین خطا ایجاد نخواهد شد.
function print_String{ [cmdletbinding()] param( [parameter(Mandatory=$True)] [AllowEmptyString()] [string]$name ) Write-Output "Writing a single string" $name }
خروجی –
PS C:WINDOWSsystem32> print_String cmdlet print_String at command pipeline position 1 Supply values for the following parameters: name: Writing a single string
در مثال بالا، می بینید که با اضافه کردن آرگومان AllowEmptyString()،
برنامه رشته Empty را می پذیرد. به طور مشابه، وقتی آرگومان ()AllowEmptyCollection را اضافه می کنید،
PowerShell مقادیر خالی یک آرایه را می پذیرد.
function print_String{ [cmdletbinding()] param( [parameter(Mandatory=$True)] [AllowEmptyCollection()] [string[]]$stringarray ) Write-Output "Writing a string array" $stringarray }
خروجی
[/membership]PS C:WINDOWSsystem32> print_String cmdlet print_String at command pipeline position 1 Supply values for the following parameters: stringarray[0]: Writing a string array>