2017-01-01 117 views
8

我在C:\Hans\Hans4\目錄中有一個名爲Eric Clapton - Nothing But The Blues - Full Concert (1994) [HQ].URL的文件。爲什麼此PowerShell腳本會產生錯誤?

代碼來創建,這是低於(儘管無論發生文件內容的錯誤)

$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 

當我運行的我得到一個錯誤

Get-ChildItem 'C:\Hans' -Recurse | Resolve-ShortcutFile > Output.txt 

的代碼引用下面

功能如下
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)} 
      } 
     } 
    } 
} 

這是錯誤消息

 
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 

爲什麼我得到這個錯誤?

+14

對於遊客到這個問題:http://meta.stackoverflow.com/questions/340658/can-someone-help-a-new-guy-to-be-good-in-asking-questions-這裏 –

+0

它不應該是C:\漢斯,但C:\羅馬 – ManOnAMission

回答

15

的問題是,用管道輸送到函數並傳遞給Get-Content

C中的文件名:\漢斯\ Hans4 \埃裏剋剋萊普頓 - 只不過是藍調 - 全音樂會 (1994)[HQ] .URL

您所遇到的問題描述here

它包含了被解釋爲range operator with the set H and Q

所以這個模式將意味着它會嘗試用下面的名字來讀取文件的內容方括號...

  • 埃裏剋剋萊普頓 - 不過布魯斯 - 全演唱會(1994)^h .URL
  • 埃裏剋剋萊普頓 - 不過是藍軍 - 全演唱會(1994)Q .URL

...但不會匹配字面在

  • 埃裏克·克萊普頓[HQ] - 不過是藍軍 - 全演唱會(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)} 
      } 
     } 
    } 
} 
+0

我有大約200這些文件,沒有通過。 大約兩個月前,它使用了相同的腳本。 但是我一定會在你回家後立即嘗試你的解決方案。 – Roman

+2

@Roman - [HQ]是一種新的命名約定,您(或您經常使用並創建互聯網快捷方式的網站)已採用?因爲這是導致現有代碼的問題。大多數情況下,現有代碼將正常工作,除非遇到帶方括號的文件名。 –

+0

這些是來自YouTube的鏈接。 我只是不明白它如何工作,現在不再。 也許他們更新了PowerShell。 – Roman

相關問題