为什么此PowerShell脚本会产生错误?

Rom*_*man 8 powershell

我有一个目录中调用Eric Clapton - Nothing But The Blues - Full Concert (1994) [HQ].URL的文件C:\Hans\Hans4\.

创建此代码的代码如下(尽管无论文件内容如何都会发生错误)

$fileContents = @"
[{000214A0-0000-0000-C000-000000000046}]
Prop3=19,2
[InternetShortcut]
URL=http://example.com/
IDList=
"@

New-Item -Path "C:\Hans\Hans4" `
         -Name "Eric Clapton - Nothing But The Blues - Full Concert (1994) [HQ].URL" `
         -ItemType "file" `
         -Value $fileContents `
         -Force
Run Code Online (Sandbox Code Playgroud)

当我运行以下内容时出现错误

Get-ChildItem 'C:\Hans' -Recurse | Resolve-ShortcutFile > Output.txt
Run Code Online (Sandbox Code Playgroud)

代码引用了下面的函数

function Resolve-ShortcutFile {         
    param(
        [Parameter(
            ValueFromPipeline=$true,
            ValueFromPipelineByPropertyName=$true,
            Position = 0)]
        [Alias("FullName")]
        [string]$fileName
    )
    process {
        if ($fileName -like "*.url") {
            Get-Content $fileName | Where-Object {
                $_ -like "url=*"
            } |
            Select-Object @{
                Name='ShortcutFile'
                Expression = {Get-Item $fileName}
            }, @{
                Name='Url'
                Expression = {$_.Substring($_.IndexOf("=") + 1 )}
            } 
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

这是错误消息

Get-Content : An object at the specified path C:\Hans\Hans4\Eric Clapton - Nothing But 
The Blues - Full Concert (1994) [HQ].URL does not exist, or has been filtered by 
the -Include or -Exclude parameter.
At line:33 char:28
+             Get-Content $fileName | Where-Object {
+             ~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (System.String[]:String[]) [Get-Content], Exception
    + FullyQualifiedErrorId : ItemNotFound,Microsoft.PowerShell.Commands.GetContentCommand

为什么我会收到此错误?

Mar*_*ith 15

问题是通过管道输入函数并传递给的文件名 Get-Content

C:\ Hans\Hans4\Eric Clapton - Nothing but the Blues - Full Concert(1994)[HQ] .URL

您遇到了此处描述的问题.

它包含方括号,它们被解释为具有集合H和Q范围运算符

因此,模式意味着它尝试使用以下任一名称读取文件的内容......

  • Eric Clapton - Nothing But The Blues - Full Concert(1994)H .URL
  • Eric Clapton - Nothing But The Blues - Full Concert(1994)Q .URL

......但不会匹配文本[HQ]

  • Eric Clapton - Nothing But The Blues - Full Concert(1994)[HQ] .URL

您可以使用该-literalPath参数来避免此问题并按字面处理文件名.

function Resolve-ShortcutFile {         
    param(
        [Parameter(
            ValueFromPipeline=$true,
            ValueFromPipelineByPropertyName=$true,
            Position = 0)]
        [Alias("FullName")]
        [string]$fileName
    )
    process {
        if ($fileName -like "*.url") {
            Get-Content  -literalPath $fileName | Where-Object {
                $_ -like "url=*"
            } |
            Select-Object @{
                Name='ShortcutFile'
                Expression = {Get-Item  -literalPath $fileName}
            }, @{
                Name='Url'
                Expression = {$_.Substring($_.IndexOf("=") + 1 )}
            } 
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

  • @Roman - 我无法回答这个问题.我不知道你的文件的历史和它们的创建时间,但是"Get-Content"H:\ Hans\Hans4\Eric Clapton - Nothing But The Blues - Full Concert(1994)[HQ] .URL"`永远不会按你的意愿工作. (8认同)
  • @Roman - 是[HQ]`一个新的命名约定,你(或你频繁出现的网站和创建互联网快捷方式)已经采用了?因为这会导致现有代码出现问题.大多数情况下,除非遇到带方括号的文件名,否则现有代码将正常工作. (2认同)