代码之家  ›  专栏  ›  技术社区  ›  Brekkjern

PowerShell路径的解析方式因使用时间而异

  •  1
  • Brekkjern  · 技术社区  · 7 年前

    当前文件夹结构如下所示:

    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
    

    奇怪的测试是 Remove-ICDCrawler.tests.ps1

    $ModuleRoot = Split-Path $PSScriptRoot -Parent
    
    . $ModuleRoot\ISPS\Public\Get-ICDCrawler.ps1
    . $ModuleRoot\ISPS\Public\Remove-ICDCrawler.ps1
    

    当它运行时,dot寻源可以正确地用于 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
    +   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    

    这就够直截了当的了。有一个太多了 ISPS ISP Get-ICDCrawler.ps1 Remove-ICDCrawler.ps1 ISP 从第4行的路径来看是可行的,但它违背了我所知道的关于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                
    
    1 回复  |  直到 7 年前
        1
  •  2
  •   Brekkjern    7 年前

    我想出来了,所以我留下这个,这样人们将来可以嘲笑它。

    开始 Get-ICDCrawler 文件如下所示:

    $ModuleRoot = Split-Path $PSScriptRoot -Parent
    
    . $ModuleRoot\Private\Find-XmlNode.ps1
    . $PSScriptRoot\New-ICDCrawler.ps1
    

    所以我导入了这个文件 $ModuleRoot Get-ICDCrawler.ps1 文件我必须确保变量的命名不同,这样名称空间就不会冲突。