Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/powerbi/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Powershell中的自动转义动态字符串_Powershell_Escaping - Fatal编程技术网

Powershell中的自动转义动态字符串

Powershell中的自动转义动态字符串,powershell,escaping,Powershell,Escaping,我有一个从第三方应用程序动态生成的字符串 $somePath = "D:\some\path\name.of - my file [20_32_21].mp4" 我需要能够在函数中验证此路径 $somePath = "D:\some\path\name.of - my file [20_32_21].mp4" Function ValidatePath{ Param($path) if(Test-Path $path){ Write-Host "Worked"

我有一个从第三方应用程序动态生成的字符串

$somePath = "D:\some\path\name.of - my file [20_32_21].mp4"
我需要能够在函数中验证此路径

$somePath = "D:\some\path\name.of - my file [20_32_21].mp4"

Function ValidatePath{
    Param($path)
    if(Test-Path $path){
        Write-Host "Worked"
    } else {
        Write-Host "Didn't Work"
    }
}

ValidatePath $somePath 
# DIDN'T WORK
问题是它在方括号中失败了

如何自动转义方括号以验证文件

# Path needs to look like this
$somePath = "D:\some\path\name.of - my file ``[20_32_21``].mp4"
ValidatePath $somePath 
# WORKED!!!

使用-LiteralPath而不是-Path;e、 g:

if ( test-path -literalpath $path ) {
  ....
}

比尔

你能尝试使用“测试路径-literalpath$path”吗?

简单而漂亮。PS的一些语义在你真正看到它之前似乎是难以捉摸的。我不知道那一点。