如何在同一PowerShell运行空间/会话中多次导入同一模块,而又不共享模块范围的变量?

时间:2018-08-10 04:42:11

标签: powershell

复制步骤

$PSModulePath的目录中,创建一个名为ModuleImportTest的文件夹,并在该文件夹中创建以下文件:

ModuleImportTest.psm1

function Get-ModuleScopedVariable {
    $Script:Variable
}

function Set-ModuleScopedVariable {
    param (
        $Variable
    )
    $Script:Variable = $Variable
}

ModuleImportTest.tests.ps1

Describe "ModuleImportTest" {
    it "Test using -prefix" {
        Import-Module ModuleImportTest
        Import-Module ModuleImportTest -Prefix User1

        Set-ModuleScopedVariable -Variable "test1"
        Set-User1ModuleScopedVariable -Variable "test2"
        Get-ModuleScopedVariable | Should -Be "test1"
    }

    It "Test using -PassThru -AsCustomObject" {
        $Module1 = Import-Module -PassThru -AsCustomObject -Name ModuleImportTest
        $Module2 = Import-Module -PassThru -AsCustomObject -Name ModuleImportTest
        $Module1."Set-ModuleScopedVariable"("test1")
        $Module2."Set-ModuleScopedVariable"("test2")
        $Module1."Get-ModuleScopedVariable"() | Should -Be "test1"
    }

    it "Test using -prefix and -force" {
        Import-Module ModuleImportTest -Force
        Import-Module ModuleImportTest -Prefix User1 -Force

        Set-ModuleScopedVariable -Variable "test1"
        Set-User1ModuleScopedVariable -Variable "test2"
        Get-ModuleScopedVariable | Should -Be "test1"
    }

    It "Test using -PassThru -AsCustomObject -Force" {
        $Module1 = Import-Module -PassThru -AsCustomObject -Name ModuleImportTest -Force
        $Module2 = Import-Module -PassThru -AsCustomObject -Name ModuleImportTest -Force
        $Module1."Set-ModuleScopedVariable"("test1")
        $Module2."Set-ModuleScopedVariable"("test2")
        $Module1."Get-ModuleScopedVariable"() | Should -Be "test1"
        $Module2."Get-ModuleScopedVariable"() | Should -Be "test2"
    }
}

从目录运行Invoke-Pester之后,您将获得以下信息:

Executing all tests in '.'

Executing script /home/hourglasssand/.local/share/powershell/Modules/ModuleImportTest/ModuleImportTest.tests.ps1

  Describing ModuleImportTest
    [-] Test using -prefix 212ms
      Expected strings to be the same, but they were different.
      String lengths are both 5.
      Strings differ at index 4.
      Expected: 'test1'
      But was:  'test2'
      ---------------^
      8:         Get-ModuleScopedVariable | Should -Be "test1"
      at <ScriptBlock>, /home/hourglasssand/.local/share/powershell/Modules/ModuleImportTest/ModuleImportTest.tests.ps1: line 8
    [-] Test using -PassThru -AsCustomObject 134ms
      Expected strings to be the same, but they were different.
      String lengths are both 5.
      Strings differ at index 4.
      Expected: 'test1'
      But was:  'test2'
      ---------------^
      16:         $Module1."Get-ModuleScopedVariable"() | Should -Be "test1"
      at <ScriptBlock>, /home/hourglasssand/.local/share/powershell/Modules/ModuleImportTest/ModuleImportTest.tests.ps1: line 16
    [-] Test using -prefix and -force 169ms
      Expected strings to be the same, but they were different.
      String lengths are both 5.
      Strings differ at index 4.
      Expected: 'test1'
      But was:  'test2'
      ---------------^
      25:         Get-ModuleScopedVariable | Should -Be "test1"
      at <ScriptBlock>, /home/hourglasssand/.local/share/powershell/Modules/ModuleImportTest/ModuleImportTest.tests.ps1: line 25
    [+] Test using -PassThru -AsCustomObject -Force 91ms
Tests completed in 607ms
Tests Passed: 1, Failed: 3, Skipped: 0, Pending: 0, Inconclusive: 0

上下文

我有一些模块,它们需要在其中保留一些状态才能正常运行(例如要在URLS或凭据中使用的子域),因为它们在模块中的许多功能中重复使用此状态,而我不想拥有这些公共状态变量作为参数添加到模块中的每个函数。

我遇到的问题是我需要将同一模块的多个副本导入到相同的PowerShell会话/运行空间中,但具有不同的内部状态(例如,指向api URL的不同域)。

我尝试使用-Prefix的{​​{1}}和-PassThru -AsCustomObject参数,但是如上述测试所示,内部模块状态仍为共享状态。

如何在同一个PowerShell运行空间/会话中多次导入同一个模块,而又不共享模块范围的变量?

编辑

根据PetSerAl的评论,我在上述方案中添加了带有-force参数的测试,并且使用Import-Module可以工作,并允许同一模块的两个副本具有不同的状态。

0 个答案:

没有答案
相关问题