2017-01-01 6 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:\ HansではなくC:\ Roman – 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個あり、何も通過しませんでした。 約2ヶ月前に同じスクリプトで動作しました。 しかし、私が家に帰るとすぐにあなたの解決策を試していきます。 – Roman

+2

@Roman - あなた(またはインターネットのショートカットを頻繁に作成して作成するウェブサイト)が採用した新しい命名規則である[HQ]ですか?既存のコードに問題が起こっているからです。ほとんどの場合、大カッコでファイル名が見つからない限り、既存のコードは正常に動作します。 –

+0

これはyoutubeからのリンクです。 私はちょうどそれが何のために働いたのか今はもうそれを理解できません。 おそらく彼らはpowershellを更新しました。 – Roman

関連する問題