PS> $a=get-date
PS> Get-Variable a
Name Value
---- -----
a 2011/12/8 17:52:02
PS> Get-Variable a | fl *
Name : a
Description :
Value : 2011/12/8 17:52:02
Visibility : Public
Module :
ModuleName :
Options : None
Attributes : {}
PS> $str="我是一个变量"
PS> $var=Get-Variable str
PS> $var
Name Value
---- -----
str 我是一个变量
PS> $var | fl *
Name : str
Description :
Value : 我是一个变量
Visibility : Public
Module :
ModuleName :
Options : None
Attributes : {}
PS> $var.Description="我知道你是一个变量"
PS> $var | fl *
Name : str
Description : 我知道你是一个变量
Value : 我是一个变量
Visibility : Public
Module :
ModuleName :
Options : None
Attributes : {}如果你不想多加一个临时变量$var来存储PSVariable,可以使用Powershell子表达式
PS> (Get-Variable str).Description="变量的描述已更改;"
PS> Get-Variable str | Format-Table Name,Description
Name Description
---- -----------
str 变量的描述已更改;
PS> $var="mossfly" PS> Set-Variable var -Option "ReadOnly" PS> (Get-Variable var).Options ReadOnly PS> Set-Variable var -Option "None" -Force PS> (Get-Variable var).Options None
PS> $var=123 PS> (Get-Variable var).Attributes PS> $var.GetType().FullName System.Int32 PS> $var="字符串" PS> (Get-Variable var).Attributes PS> $var.GetType().FullName System.String PS> [int]$var=123 PS> (Get-Variable var).Attributes TypeId ------ System.Management.Automation.ArgumentTypeConverterAttribute PS> $var.GetType().FullName System.Int32 PS> $var="2012" PS> $var 2012 PS> $var.GetType().FullName System.Int32 PS> $var="2012世界末日" Cannot convert value "2012世界末日" to type "System.Int32". Error: "Input string was not in a correct format." At line:1 char:5 + $var <<<< ="2012世界末日" + CategoryInfo : MetadataError: (:) [], ArgumentTransformationMetadataException + FullyQualifiedErrorId : RuntimeException PS> (Get-Variable var).Attributes.Clear() PS> (Get-Variable var).Attributes PS> $var="2012世界末日" PS> $var.GetType().FullName System.String
PS> $var="限制变量" PS> $condition= New-Object System.Management.Automation.ValidateLengthAttribute -ArgumentList 2,5 PS> (Get-Variable var).Attributes.Add($condition) PS> $var="限制" PS> $var="射雕英雄传" PS> $var="看射雕英雄传" The variable cannot be validated because the value 看射雕英雄传 is not a valid value for the var variable. At line:1 char:5 + $var <<<< ="看射雕英雄传" + CategoryInfo : MetadataError: (:) [], ValidationMetadataException + FullyQualifiedErrorId : ValidateSetFailure
PS> $a=123 PS> $con=New-Object System.Management.Automation.ValidateNotNullAttribute PS> (Get-Variable a).Attributes.Add($con) PS> $a=8964 PS> $a=$null
+ $a <<<< =$null + CategoryInfo : MetadataError: (:) [], ValidationMetadataException + FullyQualifiedErrorId : ValidateSetFailure ValidateNotNullOrEmptyAttribute
PS> $con=New-Object System.Management.Automation.ValidateNotNullOrEmptyAttribute
PS> (Get-Variable a).Attributes.clear()
PS> (Get-Variable a).Attributes.add($con)
PS> $a=$null
The variable cannot be validated because the value is not a valid value for the a variable.
At line:1 char:3
+ $a <<<< =$null + CategoryInfo : MetadataError: (:) [], ValidationMetadataException + FullyQualifiedErrorId : ValidateSetFailure PS> $a=""
The variable cannot be validated because the value is not a valid value for the a variable.
At line:1 char:3
+ $a <<<< ="" + CategoryInfo : MetadataError: (:) [], ValidationMetadataException + FullyQualifiedErrorId : ValidateSetFailure PS> $a=@()
The variable cannot be validated because the value System.Object[] is not a valid value for the a variable.
At line:1 char:3
+ $a <<<< =@()
+ CategoryInfo : MetadataError: (:) [], ValidationMetadataException
+ FullyQualifiedErrorId : ValidateSetFailureValidatePatternAttribute 例子,验证Email格式
PS> $email="test@mossfly.com"
PS> $con=New-Object System.Management.Automation.ValidatePatternAttribute "b[A-Z0-9._%+-]+@[A-Z0-9.-]+.[A-Z]{2,4}b"
PS> (Get-Variable email).Attributes.Add($con)
PS> $email="abc@abc.com"
PS> $email="abc@mossfly.com"
PS> $email="author@gmail.com"
PS> $email="www@mossfly"
The variable cannot be validated because the value www@mossfly is not a valid value for the email variable.
At line:1 char:7
+ $email <<<< ="www@mossfly"
+ CategoryInfo : MetadataError: (:) [], ValidationMetadataException
+ FullyQualifiedErrorId : ValidateSetFailureValidateRangeAttribute 例子,验证月份1-12
PS> $month=1
PS> (Get-Variable month).Attributes.Add( $( New-Object System.Management.Automation.ValidateRangeAttribute -ArgumentList 1,12) )
PS> $month=10
PS> $month=12
PS> $month=18
The variable cannot be validated because the value 18 is not a valid value for the month variable.
At line:1 char:7
+ $month <<<< =18 + CategoryInfo : MetadataError: (:) [], ValidationMetadataException + FullyQualifiedErrorId : ValidateSetFailure ValidateSetAttribute 例子,验证性别 PS> $sex="男"
PS> $con=New-Object System.Management.Automation.ValidateSetAttribute -ArgumentList "男","女","保密"
PS> (Get-Variable sex).Attributes.Add($con)
PS> $sex="女"
PS> $sex="不男不女"
The variable cannot be validated because the value 不男不女 is not a valid value for the sex variable.
At line:1 char:5
+ $sex <<<< ="不男不女"
+ CategoryInfo : MetadataError: (:) [], ValidationMetadataException
+ FullyQualifiedErrorId : ValidateSetFailure
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有