PowerShell路径的解析方式因时间而异';过去的
我正在开发一个PowerShell模块,其中一个测试中的路径解析非常奇怪 当前文件夹结构如下所示:PowerShell路径的解析方式因时间而异';过去的,powershell,path,filepath,pester,Powershell,Path,Filepath,Pester,我正在开发一个PowerShell模块,其中一个测试中的路径解析非常奇怪 当前文件夹结构如下所示: ISPS/ ├── ISPS/ │ ├── Private/ │ ├── Public/ │ │ ├── Add-ICDCrawler.ps1 │ │ ├── Get-ICDCrawler.ps1 │ │ ├── New-ICDCrawler.ps1 │ │ └── Remove-ICDCrawler.ps1 │ ├── ISPSCrawler.psd1
ISPS/
├── ISPS/
│ ├── Private/
│ ├── Public/
│ │ ├── Add-ICDCrawler.ps1
│ │ ├── Get-ICDCrawler.ps1
│ │ ├── New-ICDCrawler.ps1
│ │ └── Remove-ICDCrawler.ps1
│ ├── ISPSCrawler.psd1
│ └── ISPSCrawler.psm1
├── tests/
│ ├── Add-ICDCrawler.tests.ps1
│ ├── Get-ICDCrawler.tests.ps1
│ ├── New-ICDCrawler.tests.ps1
│ └── Remove-ICDCrawler.tests.ps1
$ModuleRoot = Split-Path $PSScriptRoot -Parent
. $ModuleRoot\ISPS\Public\Get-ICDCrawler.ps1
. $ModuleRoot\ISPS\Public\Remove-ICDCrawler.ps1
$ModuleRoot = Split-Path $PSScriptRoot -Parent
. $ModuleRoot\Private\Find-XmlNode.ps1
. $PSScriptRoot\New-ICDCrawler.ps1
行为怪异的测试是删除ICDCrawler.tests.ps1
。文件的开头和相关部分如下所示:
ISPS/
├── ISPS/
│ ├── Private/
│ ├── Public/
│ │ ├── Add-ICDCrawler.ps1
│ │ ├── Get-ICDCrawler.ps1
│ │ ├── New-ICDCrawler.ps1
│ │ └── Remove-ICDCrawler.ps1
│ ├── ISPSCrawler.psd1
│ └── ISPSCrawler.psm1
├── tests/
│ ├── Add-ICDCrawler.tests.ps1
│ ├── Get-ICDCrawler.tests.ps1
│ ├── New-ICDCrawler.tests.ps1
│ └── Remove-ICDCrawler.tests.ps1
$ModuleRoot = Split-Path $PSScriptRoot -Parent
. $ModuleRoot\ISPS\Public\Get-ICDCrawler.ps1
. $ModuleRoot\ISPS\Public\Remove-ICDCrawler.ps1
$ModuleRoot = Split-Path $PSScriptRoot -Parent
. $ModuleRoot\Private\Find-XmlNode.ps1
. $PSScriptRoot\New-ICDCrawler.ps1
当它运行时,点源程序对Get ICDCrawler
行正常工作,但对Remove ICDCrawler
行无效。这是出现的错误消息:
. : The term 'D:\Projects\ISPS\ISPS\ISPS\Public\Remove-ICDCrawler.ps1' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At D:\Projects\ISPS\tests\Remove-ICDCrawler.tests.ps1:4 char:3
+ . $ModuleRoot\ISPS\Public\Remove-ICDCrawler.ps1
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
这就够直截了当的了。路径中的isp
文件夹太多。如果我从测试的第4行中删除一个isp
文件夹,然后再试一次,它就可以正常工作。问题是Get ICDCrawler.ps1
和Remove ICDCrawler.ps1
文件都存储在同一个目录中,据我所知,路径应该解析到正确的目录。正如我所提到的,从第4行的路径中删除isp
是可行的,但这与我所知道的有关PowerShell的一切都背道而驰。有人知道为什么会这样吗
Name Value
---- -----
PSVersion 5.1.15063.608
PSEdition Desktop
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...}
BuildVersion 10.0.15063.608
CLRVersion 4.0.30319.42000
WSManStackVersion 3.0
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
我知道了,所以我留下这个,这样人们将来可以嘲笑它
Get ICDCrawler
文件的开头如下所示:
ISPS/
├── ISPS/
│ ├── Private/
│ ├── Public/
│ │ ├── Add-ICDCrawler.ps1
│ │ ├── Get-ICDCrawler.ps1
│ │ ├── New-ICDCrawler.ps1
│ │ └── Remove-ICDCrawler.ps1
│ ├── ISPSCrawler.psd1
│ └── ISPSCrawler.psm1
├── tests/
│ ├── Add-ICDCrawler.tests.ps1
│ ├── Get-ICDCrawler.tests.ps1
│ ├── New-ICDCrawler.tests.ps1
│ └── Remove-ICDCrawler.tests.ps1
$ModuleRoot = Split-Path $PSScriptRoot -Parent
. $ModuleRoot\ISPS\Public\Get-ICDCrawler.ps1
. $ModuleRoot\ISPS\Public\Remove-ICDCrawler.ps1
$ModuleRoot = Split-Path $PSScriptRoot -Parent
. $ModuleRoot\Private\Find-XmlNode.ps1
. $PSScriptRoot\New-ICDCrawler.ps1
因此,我导入了该文件,$ModuleRoot
变量被更改为相对于Get ICDCrawler.ps1
文件。我必须确保变量的命名不同,这样名称空间就不会冲突