我有一个目录中调用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
您遇到了此处描述的问题.
因此,模式意味着它尝试使用以下任一名称读取文件的内容......
......但不会匹配文本[HQ]中
您可以使用该-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)
| 归档时间: |
|
| 查看次数: |
986 次 |
| 最近记录: |