Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/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
Variables Powershell-尝试针对类似于变量名的字符串测试路径_Variables_Powershell - Fatal编程技术网

Variables Powershell-尝试针对类似于变量名的字符串测试路径

Variables Powershell-尝试针对类似于变量名的字符串测试路径,variables,powershell,Variables,Powershell,我为之工作的公司在其所有管理级别帐户的前面加上了$符号。麻烦,我知道 我正在尝试使用测试路径查看是否存在这样的文件夹: $username = read-host "Enter Login ID:" 我在框中键入$adminsdb,然后点击ok ##### Find TS Profile ##### $TSProfile_exist = test-path "\\server\tsprofiles$\$username" 文件夹确实存在,但$TSProfile_exist出现错误 如何处理用

我为之工作的公司在其所有管理级别帐户的前面加上了$符号。麻烦,我知道

我正在尝试使用
测试路径
查看是否存在这样的文件夹:

$username = read-host "Enter Login ID:"
我在框中键入
$adminsdb
,然后点击ok

##### Find TS Profile #####
$TSProfile_exist = test-path "\\server\tsprofiles$\$username"
文件夹确实存在,但<代码>$TSProfile_exist出现错误


如何处理用户名中的
$
?我正在构建这个应用程序来快速显示环境中用户的统计信息。我们还有前缀为#符号的服务帐户。

在PowerShell中处理特殊字符的方法是使用`(倒勾)

变成

$TSProfile_exist = test-path "\\server\tsprofiles`$\$username"

小心测试路径的一个技巧是,它的语义不是目录存在,而是目录可读。换句话说,如果您没有访问测试目录的权限,即使该目录存在,您也会收到
false

另一种选择是使用单引号字符串;在这种情况下,powershell不会展开变量

test-path '\\server\path\$username'

-Oisin

那么,有没有一种方法可以测试路径,而不必枚举整个目录树和按匹配操作进行搜索?您可以使用
Get ChildItem-path Where to start Search-Include“Searched.File”-Recurse
在列出目录时,这也需要很长的等待时间。。。有什么测试路径比get childitem更快吗?我不明白。是否要测试cetain路径中是否存在文件?您也许可以在这里问另一个问题,我们不在同一主题中。您先前建议使用get-childitem,但这非常缓慢,因为get-childitem cmdlet列出了给定位置中的所有目录。测试路径似乎通过直接测试路径绕过了这一点。有没有其他方法可以在没有目录权限限制的情况下实现与测试路径相同的速度?
test-path '\\server\path\$username'