PowerShell 技能连载 - 安全使用 UNC 路径

当您在 PowerShell 中使用 UNC 路径时,您的脚本可能会中断。由于 UNC 路径没有驱动器号,所以 PowerShell 将会在当前目录下查找,并且使用当前路径对应的 PSProvider。

所以如果您的当前目录不是一个文件路径(例如是一个注册表驱动器),那么您的 UNC 路径将会被解释成一个注册表路径。

要安全地使用路径,请一定在 UNC 路径前面加上正确的提供器名称:

# UNC-Paths have no drive letter
# so PowerShell uses the current directory instead to find the PSProvider
# for UNC paths, to be safe, always add the provider name manually
$exists = Test-Path -Path 'FileSystem::\\server12\fileshare'

$exists

PowerShell 技能连载 - 使用 Splatting 技术

通过 splatting 技术,您可以调用 cmdlet,并可以控制提交的参数。

要实现该目标,先向一个哈希表插入参数和值,然后将哈希表传给 cmdlet。这种方法适用于任意 cmdlet。

以下是一个例子:

# classic:
Get-ChildItem -Path c:\windows -Filter *.ps1 -Recurse -ErrorAction SilentlyContinue


# Splatting
$params = @{}
$params.Path = 'c:\windows'
$params.Filter = '*.ps1'
$params.Recurse = $true
$params.ErrorAction = 'SilentlyContinue'
Get-ChildItem @params

PowerShell 技能连载 - 读取 RSS 频道

我们可以通过 XML 对象来读取 RSS 频道信息,然而 XML 对象不支持代理服务器。

这个例子用 Invoke-WebRequest 通过代理服务器来获取 RSS 数据(如果忽略 -Proxy 参数则直接获取),然后将结果转换为 XML。

#requires -Version 3


$url = 'http://blogs.msdn.com/b/powershell/rss.aspx'

$page = Invoke-WebRequest -Uri $url <#-Proxy 'http://proxy...:8080' -ProxyUseDefaultCredentials#>
$content = $page.Content


$xml = [XML]$content
$xml.rss.channel.item  | Out-GridView

这段代码将显示 PowerShell 团队博客数据。

PowerShell 技能连载 - 读取注册表键值和值类型

Get-ItemProperty 可以方便地读取注册表键值,但是无法获得注册表键值的数据类型。

Get-ItemProperty -Path 'HKLM:\Software\Microsoft\Windows NT\CurrentVersion'

以下是通过 .NET 方法的另一种实现,可以获得所有的信息:

PS> Get-RegistryValue 'HKLM\Software\Microsoft\Windows NT\CurrentVersion'

Name                     Type Value
----                     ---- -----
CurrentVersion         String 6.1
CurrentBuild           String 7601
SoftwareType           String System
CurrentType            String Multiprocessor Free
InstallDate             DWord 1326015519
RegisteredOrganization String
RegisteredOwner        String Tobias
SystemRoot             String C:\Windows
InstallationType       String Client
EditionID              String Ultimate
ProductName            String Windows 7 Ultimate
ProductId              String 0042xxx657
DigitalProductId       Binary {164, 0, 0, 0...}
DigitalProductId4      Binary {248, 4, 0, 0...}
CurrentBuildNumber     String 7601
BuildLab               String 7601.win7sp1_gdr.150202-1526
BuildLabEx             String 7601.18741.amd64fre.win7sp1_gdr.150202-1526
BuildGUID              String f974f16b-3e62-4136-a6fb-64fccddecde3
CSDBuildNumber         String 1130
PathName               String C:\Windows

我们需要开发一个 Get-RegistryValue 函数来实现该功能。请注意该函数可传入任意合法的注册表键,并且不需要使用 PowerShell 驱动器号。

function Get-RegistryValue
{
    param
    (
        [Parameter(Mandatory = $true)]
        $RegistryKey
    )

    $key = Get-Item -Path "Registry::$RegistryKey"
    $key.GetValueNames() |
    ForEach-Object {
        $name = $_
        $rv = 1 | Select-Object -Property Name, Type, Value
        $rv.Name = $name
        $rv.Type = $key.GetValueKind($name)
        $rv.Value = $key.GetValue($name)
        $rv
    }
}

PowerShell 技能连载 - 查找电视剧信息

PowerShell 可以查询基于 XML 内容的网站,以下是一个查询电影数据库的例子:

只需要输入您感兴趣的电视剧名称即可。如果您不能直接访问 Internet,可以用 -Proxy 参数指定代理服务器。

#requires -Version 3

$name = 'stargate'
$url = "http://thetvdb.com/api/GetSeries.php?seriesname=$name&language=en"

$page = Invoke-WebRequest -Uri $url <#-Proxy 'http://proxy....:8080' -ProxyUseDefaultCredentials#>
$content = $page.Content


$xml = [XML]$content
$xml.Data.Series | Out-GridView

ionic 问答

Q: 使用 CSS 还是 ion- 指令

A: Ion vs div directives - Ionic

CSS 优势:

  • 性能
  • DOM 干净

ion- 指令优势:

  • 功能更强,例如 ion-listshow-reorder 等 API

结论:只是要样式,用 CSS需要增强的 API 功能,用 ion- 指令。

PowerShell 技能连载 - 比较文件夹内容

要快速比较文件夹内容并且找出只在一个文件夹中存在的文件,请试试以下代码:

$list1 = Get-ChildItem c:\Windows\system32 | Sort-Object -Property Name

$list2 = Get-ChildItem \\server12\c$\windows\system32 | Sort-Object -Property Name


Compare-Object -ReferenceObject $list1 -DifferenceObject $list2 -Property Name |
  Sort-Object -Property Name

该代码属两个文件夹列表,一个来自本机,另一个来自远程计算机。接下来 Compare-Object 命令会挑出只在一个文件夹中存在的文件。

PowerShell 技能连载 - 批量重命名文件

假设您有一整个文件夹的图片文件,并希望它们的名字标准化。

这个脚本演示了如何批量重命名图片文件:

$i = 0

Get-ChildItem -Path c:\pictures -Filter *.jpg |
ForEach-Object {
    $extension = $_.Extension
    $newName = 'pic_{0:d6}{1}' -f  $i, $extension
    $i++
    Rename-Item -Path $_.FullName -NewName $newName
}

文件夹中所有的 JPG 文件都被重命名了。新的文件名是“pic_”加上四位数字。

您可以很容易地修改脚本来重命名其它类型的文件,或是使用其它文件名模板。

PowerShell 技能连载 - 对密码加密

如果您确实需要在脚本中保存一个凭据对象,以下是将一个安全字符串转换为加密文本的方法:

$password = Read-Host -Prompt 'Enter Password' -AsSecureString
$encrypted = $password | ConvertFrom-SecureString
$encrypted | clip.exe
$encrypted

当运行这段代码时,会要求您输入密码。接下来密码会被转换为一系列字符并存入剪贴板中。加密的密钥是您的身份标识加上您的机器标识,所以只能用相同机器的相同用户对密码解密。

下一步,用这段代码可以将您的密码密文转换为凭据对象:

$secret = '01000000d08c9ddf0115d1118c7a00c04fc297eb01000000d4a6c6bfcbbb75418de6e9672d85e73600...996f8365c8c82ea61f94927d3e3b14000000c6aecec683717376f0fb18519f326f6ac9cd89dc'
$username = 'test\user'

$password = $secret | ConvertTo-SecureString

$credential = New-Object -TypeName System.Management.Automation.PSCredential($username, $password)

# example call
Start-Process notepad -Credential $credential -WorkingDirectory c:\

将加密的密码字符串写入脚本中,然后使用指定的用户名来验证身份。

现在,$cred 中保存的凭据对象可以在任何支持 -Credential 参数的 cmdlet 或函数中使用了。