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

使用powershell cmdlets

Use PowerShell Cmdlets!

http://app.en25.com/e/es.aspx?s=1403&e=5964&elq=21da21c4f95a48f79b02e546e7e8d3a5

文中通过

PS> [System.DateTime]::Now

PS> Get-Date

来说明虽然两个方法的结果一样,但是尽量还是使用cmdlet,除非cmdlet不支持的功能再去用.net的方法来实现。

 

处理明文密码

Retrieving Clear Text Password

http://app.en25.com/e/es.aspx?s=1403&e=6054&elq=573dc29f78354bf295f36b7303f02efc

PS> $cred = Get-Credential

PS> $pwd = [Runtime.InteropServices.Marshal]::PtrToStringAuto( [Runtime.InteropServices.Marshal]::SecureStringToBSTR( $cred.Password ))

PS> "Password: $pwd"

默认Get-Credential接收密码是加密形式的,这个方法告诉我们怎么获取明文密码。

 

查找你当前所在域

Finding Your Current Domain

http://app.en25.com/e/es.aspx?s=1403&e=6131&elq=3f8c77788b9a420d9053b19d325f63dc

PS> [ADSI]""

很简单的一个方法,不过当不在域里的时候会触发异常,所以这样写是比较健壮的:

PS> try { [ADSI]"" | Out-Host } catch { "Not connected to a domain" }

 

移除空的实体

Remove Empty Entries

http://app.en25.com/e/es.aspx?s=1403&e=6267&elq=2fc0f3aea747411789e0d1f7bda14956

介绍了用where-object的方法剔除某属性为空的实体。

PS> Get-WmiObjectWin32_NetworkAdapterConfiguration | Where-Object { $_.IPAddress }

上面的语句会列出所有已经配置了IP地址的网络适配器。

 

重命名驱动器卷名

Rename Drive Label

http://app.en25.com/e/es.aspx?s=1403&e=6385&elq=07b02b1905914249a5e054a11e568689

PS> $drive = [wmi]"Win32_logicaldisk='C:'"

PS> $drive.VolumeName = "My Harddrive"

PS> $drive.Put()

还是通过WMI。最后提示下修改完后需要调用Put()提交一下更改才会生效。

 

 

 

 

以上来自powershell.com

2010年七月份19日到23日的PowerTip of the Day

posted @ 2010-07-26 17:04  哥本哈士奇(aspnetx)  阅读(368)  评论(0编辑  收藏  举报