您的位置:首页 > 运维架构 > Shell

PowerTip of the Day from powershell.com上周汇总(九)

2010-09-25 10:41 120 查看
辨别64位环境


Identifying 64-Bit-Environments

http://powershell.com/cs/blogs/tips/archive/2010/09/20/identifying-64-bit-environments.aspx

根据判断变量类型IntPtr的长度,如果为8就是64位系统,4为32位系统。

if ([IntPtr]::Size -eq 8) { "Ein 64-Bit-System" } else { "Ein 32-Bit-System" }

 

 

查找静态方法


Finding Static Methods

http://powershell.com/cs/blogs/tips/archive/2010/09/21/finding-static-methods.aspx

一个类型通常会包含很多有用的方法,通过Get-Member就可以显示其全部,另外通过-Static参数可以查看所有静态方法。如果不加是无法看到静态方法的,而是只能看到实例方法:

[DateTime] | Get-Member –Static

[DateTime]::isLeapYear(2010)

 

 

提示输入密码


Prompting for Passwords

http://powershell.com/cs/blogs/tips/archive/2010/09/22/prompting-for-passwords.aspx

通过Get-Credential方法,会以可视化的界面提示输入密码。这个方法返回一个凭证对象,包含加密之后的密码信息。只有通过GetNetworkCredential方法才可以查看明文信息:

$cred = Get-Credential SomeUserName

$cred.Password

#到这里发现信息是加密了的

$cred.GetNetworkCredential()

#通过这个方法,可以返回其明文信息

$cred.GetNetworkCredential().Password

#以及明文方式的密码

 

 

通过控制台的方式提示输入密码


Prompting for Secret Passwords via Console with Powershell

http://powershell.com/cs/blogs/tips/archive/2010/09/23/prompting-for-secret-passwords-via-console-with-powershell.aspx

可以通过Read-Host方法隐藏用户输入的密码信息:

$pwd = Read-Host -AsSecureString 'Enter Password'

密码是以加密的方式保存的,如果需要查看明文信息只需要转换成一个凭证对象即可:

(New-Object System.Management.Automation.PSCredential('dummy',$pwd)).GetNetworkCredential().Password

 

 

通过控制台提示输入密码


Prompting for Secret Passwords via Console

http://powershell.com/cs/blogs/tips/archive/2010/09/24/prompting-for-secret-passwords-via-console.aspx

使用低等级的PS API实现:

$c = $host.ui.PromptForCredential('Log on', $null, 'test\user', 'target')

$c

这种方法允许定义对话框的标题等信息。

 

 

获取字幕列表


Getting Alphabetical Listings

http://powershell.com/cs/blogs/tips/archive/2010/09/13/getting-alphabetical-listings.aspx

通过..的方法,powershell只支持数字类型,比如:

1..10

但是这里可以通过类型转换的方法实现:

$OFS = ","

[string][char[]](65..90)

这样就可以获得一个从A到Z的列表。

 

 

获取短日期型


Getting Short Dates

http://powershell.com/cs/blogs/tips/archive/2010/09/14/getting-short-dates.aspx

本篇主要描述使用对象里的方法。

(Get-Date).toShortDateString()

实际上是CLR的方法。

另外查看所有支持的方法的方式:

Get-Date | Get-Member -MemberType *method

 

 

查找闰年


Finding Leap Years

http://powershell.com/cs/blogs/tips/archive/2010/09/15/finding-leap-years.aspx

日期类型包含很多有用的静态方法,比如,可以检查指定的年是否为闰年:

[DateTime]::IsLeapYear(1904)

 

 

查看一个月有几天


Finding Days in Month

http://powershell.com/cs/blogs/tips/archive/2010/09/16/finding-days-in-month.aspx

通过日期类型的静态方法来实现,比如:

[DateTime]::DaysInMonth(2009, 2)

#结果:28

[DateTime]::DaysInMonth(2008, 2)

#结果:29

 

 

查看最大值


Finding Maximum Values

http://powershell.com/cs/blogs/tips/archive/2010/09/17/finding-maximum-values.aspx

数字类型的存储都有一个范围,比如一个byte类型存储的范围是0到255,那么Int32和Int64类型呢?可以通过Powershell:

[Int32]::MaxValue

#结果:2147483647

[Int64]::MaxValue

#结果:9223372036854775807

[Byte]::MaxValue

#结果:255

通过这个方法也可以看到无符号类型的区别:

[Int32]::MaxValue
2147483647
[UInt32]::MaxValue
4294967295
[Int32]::MinValue
-2147483648
[UInt32]::MinValue
0

由于无符号没有复数,所以其最大值是带符号类型的一倍。

 

 

 

以上来自powershell.com

2010年九月份13日到24日的PowerTip of the Day
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: