• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell
您的位置:首页 > 脚本语言 >PowerShell > Windows Powershell 变量的幕后管理

Windows Powershell 变量的幕后管理

作者:hebedich 字体:[增加 减小] 来源:互联网

hebedich 通过本文主要向大家介绍了windows powershell,windowspowershell2.0,windows powershell64,windows10 powershell,windows7 powershell等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

在Powershell中创建一个变量,会在后台生成一个PSVariable对象,这个对象不仅包含变量的值,也包含变量的其它信息,例如”只写保护”这样的描述。
如果在Powershell中输出一个变量,只会输出这个变量的值。不能够显示它的其它信息,如果想查看一个变量的其它保留信息,就需要变量的基类PSVariable对象,这个可以通过Get-Variable命令得到,下面的例子演示如何查看一个变量的全部信息。

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 : {}
</div>

修改变量的选项设置
Powershell处理一个变量的PSVariable对象,主要是为了能够更新变量的选项设置。既可以使用命令Set-Variable,也可以在获取PSvariable对象后直接更改。比如更改一个变量的描述:

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                             变量的描述已更改;
</div>

激活变量的写保护
可以操作一个变量的选项设置 ,比如给一个变量加上写保护,需要将Option设置为“ReadOnly”

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
</div>

变量的选项
变量的选项是一个枚举值,包含:
“None”:默认设置
“ReadOnly”:变量只读,但是可以通过-Force 选项更新。
“Constant”:常量一旦声明,在当前控制台不能更新。
“Private”:只在当前作用域可见,不能贯穿到其它作用域
“AllScope”:全局,可以贯穿于任何作用域

变量的类型规范
每个变量的都有自己的类型,这个具体的类型存放在PsVariable对象的Attributes[System.Management.Automation.PSVariableAttributeCollection]属性,如果这个Attributes为空,可以给这个变量存放任何 类型的数据,Powershell会自己选择合适的类型。一旦这个Attributes属性确定下来,就不能随意存放数据了。例如给$var存放一个整数,属于弱类型,所以Attributes属性为空,这时还可以给它赋值一个字符串。但是如果给$var增加强类型,存放一个整数,再给它赋值一个其它类型,解释器会自动尝试转换,如果不能转换就会抛出异常。这时如果你非得更新$var变量的类型,可以使用 (Get-Variable var).Attributes.Clear(),清空Attributes,这样强类型就又转换成弱类型了。

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
</div>

验证和检查变量的内容
变量PSVariable对象的Attributes属性能够存储一些附件条件,例如限制变量的长度,这样在变量重新赋值时就会进行验证,下面演示如何限制一个字符串变量的长度为位于2-5之间。

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
</div>

常用的变量内容验证还有5种,分别为:
ValidateNotNullAttribute:限制变量不能为空
ValidateNotNullOrEmptyAttribute:限制变量不等为空,不能为空字符串,不能为空集合
ValidatePatternAttribute:限制变量要满足制定的正则表达式
ValidateRangeAttribute:限制变量的取值范围
ValidateSetAttribute:限制变量的取值集合

ValidateNotNullAttribute 例子

PS> $a=123
PS> $con=New-Object System.Management.Automation.ValidateNotNullAttribute
PS> (Get-Variable a).Attributes.Add($con)
PS> $a=8964
PS> $a=$null
</div>


无法验证此变量,因为值  不是变量 a 的有效值。
所在位置 行:1 字符: 3

+ $a <<<< =$null
  + CategoryInfo     : MetadataError: (:) [], ValidationMetadataException
  + FullyQualifiedErrorId : ValidateSetFailure
ValidateNotNullOrEmptyAttribute 
</div>

例子,注意@()为一个空数组。

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.Automa
  


 

您可能想查找下面的文章:

  • PowerShell使用match操作符来筛选数组
  • PowerShell连接SQL SERVER数据库进行操作的实现代码
  • powershell远程管理服务器磁盘空间的实现代码
  • Powershell 查询 Windows 日志的方法
  • Powershell 查找用户的主SMTP地址
  • Powershell 获取特定的网页信息的代码
  • Powershell错误处理之what-if
  • PowerShell 4.0实现自动化设置服务器
  • 揭秘PowerShell 5.0新特性和新功能
  • 简单谈谈PowerShell 4.0中的新命令

相关文章

  • PowerShell查看进程的所属用户
  • PowerShell调用Web测试工具Selenium实例
  • Windows Powershell 定义函数
  • Powershell Profiles配置文件的存放位置介绍
  • 详解Linux使用shell+expect远程登录主机
  • PowerShell比较文本文件的两个方法
  • Powershell展开对象数据结构并以JSON格式输出
  • Powershell实现按类型排序
  • 探索PowerShell (三) PowerShell下使用Aliases
  • 在cmd中直接运行PowerShell脚本文件的方法

文章分类

  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell

最近更新的内容

    • PowerShell中定义哈希散列(Hash)和调用例子
    • Powershell小技巧之去除多余的空格
    • Powershell脚本中包含文件资源的例子
    • WMI入门教程之怎么使用WMI?
    • 使用PowerShell修改注册表
    • Powershell小技巧之使用Jint引擎在PowerShell中执行Javascript函数
    • PowerShell中的特殊变量$null介绍和创建多行注释小技巧
    • Powershell ISE的抽象语法树编程示例
    • PowerShell实现按条件终止管道的方法
    • powershell远程管理服务器磁盘空间的实现代码

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有