当我使用环境变量时,为什么Test-Path cmdlet错误地报告false? [英] Why does Test-Path cmdlet incorrectly report false when I use an environment variable?

查看:91
本文介绍了当我使用环境变量时,为什么Test-Path cmdlet错误地报告false?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用Powershell 4.0的Win 7计算机上

I'm on a Win 7 machine using Powershell 4.0

PS C:\> $psversiontable

Name                           Value                                                                                                                                                               
----                           -----                                                                                                                                                               
PSVersion                      4.0                                                                                                                                                                 
WSManStackVersion              3.0                                                                                                                                                                 
SerializationVersion           1.1.0.1                                                                                                                                                             
CLRVersion                     4.0.30319.42000                                                                                                                                                     
BuildVersion                   6.3.9600.18728                                                                                                                                                      
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0}                                                                                                                                                
PSRemotingProtocolVersion      2.2 

请参阅下面的命令行练习.该dll存在.环境变量存在并且具有正确的路径.当我将环境变量与Test-Path一起使用时,它错误地报告了$false.当我在Test-Path中使用字符串文字时,它会正确报告$true

See command line exercise below. The dll exists. The environment variable exists and has the correct path. When I use the the environment variable with Test-Path, it incorrectly reports $false. When I use a string literal with Test-Path, it correctly reports $true

为什么使用环境变量时Test-Path错误地报告false?

Why does Test-Path incorrectly report false when I use the environment variable?

PS C:\> dir "C:\Program Files (x86)\Gemalto\NET Smartcard Framework SDK\v2.3.0\bin\SmartCard.Runtime.dll"

    Directory: C:\Program Files (x86)\Gemalto\NET Smartcard Framework SDK\v2.3.0\bin

Mode                LastWriteTime     Length Name                                                                                                                                                  
----                -------------     ------ ----                                                                                                                                                  
-a---         1/13/2012   6:57 PM     131072 SmartCard.Runtime.dll                                                                                                                                 

PS C:\> Write-Host $env:GEMALTO_SMARTCARD_DLL
"C:\Program Files (x86)\Gemalto\NET Smartcard Framework SDK\v2.3.0\bin\SmartCard.Runtime.dll"

PS C:\> Test-Path $env:GEMALTO_SMARTCARD_DLL

False

PS C:\> Test-Path "C:\Program Files (x86)\Gemalto\NET Smartcard Framework SDK\v2.3.0\bin\SmartCard.Runtime.dll"
True    

推荐答案

在打印Write-Host $env:GEMALTO_SMARTCARD_DLL时,您可以看到环境变量GEMALTO_SMARTCARD_DLL包括文字"作为其值的一部分.那是不对的.例如,您应该先删除它们,然后再传递到Test-Path:$env:GEMALTO_SMARTCARD_DLL.Trim('"').

As you print Write-Host $env:GEMALTO_SMARTCARD_DLL, then you can see that environment variable GEMALTO_SMARTCARD_DLL includes literal " as part of its value. That is incorrect. You should remove them before passing to Test-Path: $env:GEMALTO_SMARTCARD_DLL.Trim('"'), for example.

注意:尽管"对于文件系统路径是无效字符,但对于PowerShell路径也不是无效字符,因为它不仅限于文件系统.

Note: although " is invalid character for file system path, it is not invalid character for PowerShell path, as it is not limited for file system only.

New-PSDrive -Name '"C' -PSProvider Variable -Root \
${Program Files (x86)\Gemalto\NET Smartcard Framework SDK\v2.3.0\bin\SmartCard.Runtime.dll"} = 'Value'
Test-Path $env:GEMALTO_SMARTCARD_DLL

这篇关于当我使用环境变量时,为什么Test-Path cmdlet错误地报告false?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆