Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.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
Windows 如何将位置与PowerShell中的主文件夹进行比较_Windows_Powershell_Shell - Fatal编程技术网

Windows 如何将位置与PowerShell中的主文件夹进行比较

Windows 如何将位置与PowerShell中的主文件夹进行比较,windows,powershell,shell,Windows,Powershell,Shell,我曾尝试在特定位置运行脚本,但我无法运行,我意识到这与以下代码有关,我假设该代码应返回True,但如果您位于windows 10的主文件夹中,它实际上返回False $loc=Get-Location $loc -eq $HOME =>> Should return True but it is not. 因此,任何评论或回答都将不胜感激 Get Location返回一个对象。必须将$HOME与该对象的正确属性进行比较: (Get-Location).Path -eq $HOM

我曾尝试在特定位置运行脚本,但我无法运行,我意识到这与以下代码有关,我假设该代码应返回True,但如果您位于windows 10的主文件夹中,它实际上返回False

$loc=Get-Location 
$loc -eq $HOME  =>> Should return True but it is not.
因此,任何评论或回答都将不胜感激

Get Location返回一个对象。必须将$HOME与该对象的正确属性进行比较:

(Get-Location).Path -eq $HOME
获取位置返回一个对象。必须将$HOME与该对象的正确属性进行比较:

(Get-Location).Path -eq $HOME
Get Location返回一个Location对象,而$home只是一个字符串。将$loc转换为字符串,或将$home与其路径属性进行比较:

$loc.Path-eq$home 或 $loc-eq$home 由于PowerShell的-eq运算符的行为取决于左侧操作数的类型,因此您也可以翻转这两个操作数,它也可以工作:

$home-eq$loc Get Location返回一个Location对象,而$home只是一个字符串。将$loc转换为字符串,或将$home与其路径属性进行比较:

$loc.Path-eq$home 或 $loc-eq$home 由于PowerShell的-eq运算符的行为取决于左侧操作数的类型,因此您也可以翻转这两个操作数,它也可以工作:

$home-eq$loc 显示Get Location返回实例而不是字符串,以及如何对此进行说明

然而,有一个小小的警告:

虽然在实践中可能很少见,但基于返回对象的.Path属性和通过。。。可能仍会失败,即如果当前位置基于自定义PowerShell驱动器:

.Path然后报告特定于PowerShell的路径,而$HOME始终表示为文件系统本机路径,因此即使这两个路径最终指向相同的文件系统位置,-eq也不会检测到该路径

为此,请改用.ProviderPath属性,该属性与$HOME一样,将目录表示为文件系统本机路径:

注意:不幸的是,需要通过-replace“[\\/]$”删除尾随路径分隔符(如果存在),这是因为自v7.0起,PowerShell没有规范化什么。ProviderPath报告:如果当前基于PS驱动器的路径是该PS驱动器的根,.ProviderPath意外地将类Unix平台上的尾随\或/保留在返回的本机路径中。 这种有问题的行为是我们的主题。 举一个完整的例子:

# Define a custom H: drive whose root is $HOME.
$null = New-PSDrive H FileSystem $HOME

# Change to the root of the new drive, which is effectively the 
# same as $HOME, though from PowerShell's perspective the path is H:\
Set-Location H:\

# !! -> $false, because .Path reports 'H:\'
(Get-Location).Path -eq $HOME

# OK -> $true - .ProviderPath reports the true, file-system-native path.
# Note: 
#   If GitHub issue #12971 gets fixed (see above),
#   the -replace '[\\/]$' part will no longer be needed.
(Get-Location).ProviderPath -replace '[\\/]$' -eq $HOME
此外,如果当前位置有可能基于符号链接/重分析点,则必须进行额外的工作以确定路径相等性。

显示Get location返回实例而不是字符串,以及如何对此进行说明

然而,有一个小小的警告:

虽然在实践中可能很少见,但基于返回对象的.Path属性和通过。。。可能仍会失败,即如果当前位置基于自定义PowerShell驱动器:

.Path然后报告特定于PowerShell的路径,而$HOME始终表示为文件系统本机路径,因此即使这两个路径最终指向相同的文件系统位置,-eq也不会检测到该路径

为此,请改用.ProviderPath属性,该属性与$HOME一样,将目录表示为文件系统本机路径:

注意:不幸的是,需要通过-replace“[\\/]$”删除尾随路径分隔符(如果存在),这是因为自v7.0起,PowerShell没有规范化什么。ProviderPath报告:如果当前基于PS驱动器的路径是该PS驱动器的根,.ProviderPath意外地将类Unix平台上的尾随\或/保留在返回的本机路径中。 这种有问题的行为是我们的主题。 举一个完整的例子:

# Define a custom H: drive whose root is $HOME.
$null = New-PSDrive H FileSystem $HOME

# Change to the root of the new drive, which is effectively the 
# same as $HOME, though from PowerShell's perspective the path is H:\
Set-Location H:\

# !! -> $false, because .Path reports 'H:\'
(Get-Location).Path -eq $HOME

# OK -> $true - .ProviderPath reports the true, file-system-native path.
# Note: 
#   If GitHub issue #12971 gets fixed (see above),
#   the -replace '[\\/]$' part will no longer be needed.
(Get-Location).ProviderPath -replace '[\\/]$' -eq $HOME

此外,如果当前位置有可能基于符号链接/重新分析点,则必须进行额外的工作以确定路径相等性。

此注释适用于无法获取位置的用户。运行新的PSDrive并在不同的scopefunction、ps1等中设置位置,然后ProviderPath返回null。详情请参阅。Get-ChildItem[0]。如果至少有一个文件,则Parent.FullName仍然有效。。。很抱歉重新发布,无法编辑我自己的评论。此评论适用于无法获取位置的用户。运行新的PSDrive并在不同的scopefunction、ps1等中设置位置,然后ProviderPath返回null。详情请参阅。Get-ChildItem[0]。如果至少有一个文件,则Parent.FullName仍然有效。。。抱歉重新发布,无法编辑我自己的评论