作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
给定一个 ps1 文件作为具有以下代码的模块的一部分:
function Get-Greeting {
'Hello {0}' -f $Env:Username
}
Export-ModuleMember -Function:Get-Greeting
Export-ModuleMember : The Export-ModuleMember cmdlet can only be called from inside a module.
$PSScriptRoot
编写一个 hack。 ,但这只是一个 hack,在他们“修复”的版本 3 中不起作用
$PSScriptRoot
永远不会为空。我试过查看
$MyInvocation
中的各种项目,但要么我错过了一些东西,要么它没有任何用处。
Get-Variable
内部和外部一个模块,但再次发现没有差异。
Import-Module
运行时,我错过了什么?对比
. myscript.ps1
?
最佳答案
看来我找到了答案。我在 $MyInvocation
中遗漏了一些东西,因为我看错了范围。给定以下文件:
# .\moduleDetection\moduleDetection.ps1
$ErrorActionPreference = 'SilentlyContinue';
'=== Parent Invocation.MyCommand: [{0}]' -f (Get-Variable -Name:MyInvocation -Scope:1 -ValueOnly | Select -Expand MyCommand) | Out-Host;
# .\moduleDotSource\moduleDotSource.psm1
. "$PSScriptRoot\..\moduleDetection\moduleDetection.ps1"
# .\module-test.ps1
$Error.Clear()
Write-Host "Powershell Version 3:"
Write-Host "Powershell -Command Import-Module (direct)"
powershell -nologo -noprofile -Command { Import-Module .\moduledetection }
Write-Host "Powershell -Command Import-Module (dot-source)"
powershell -nologo -noprofile -Command { Import-Module .\moduleDotSource }
Write-Host "Powershell -File ...moduledetection.ps1"
powershell -nologo -noprofile -File .\moduledetection\moduleDetection.ps1
Write-Host "Powershell Dot-Source"
powershell -nologo -noprofile -Command { . .\moduledetection\moduleDetection.ps1 }
Write-Host ""
Write-Host "Powershell Version 2:"
Write-Host "Powershell -Version 2 -Command Import-Module"
powershell -version 2.0 -nologo -noprofile -Command { Import-Module .\moduledetection }
Write-Host "Powershell -Version 2 -Command Import-Module (dot-source)"
powershell -version 2.0 -nologo -noprofile -Command { Import-Module .\moduleDotSource }
Write-Host "Powershell -Version 2 -File ...moduledetection.ps1"
powershell -version 2.0 -nologo -noprofile -File .\moduledetection\moduleDetection.ps1
Write-Host "Powershell -Version 2 Dot-Source"
powershell -version 2.0 -nologo -noprofile -Command { . .\moduledetection\moduleDetection.ps1 }
cmd /c mklink .\moduledetection\moduleDetection.ps1 .\moduledetection\moduleDetection.psm1
Powershell Version 3:
Powershell -Command Import-Module (direct)
=== Parent Invocation.MyCommand: [ Import-Module .\moduledetection ]
Powershell -Command Import-Module (dot-source)
=== Parent Invocation.MyCommand: [ Import-Module .\moduleDotSource ]
Powershell -File ...moduledetection.ps1
Powershell Dot-Source
Powershell Version 2:
Powershell -Version 2 -Command Import-Module
=== Parent Invocation.MyCommand: [ Import-Module .\moduledetection ]
Powershell -Version 2 -Command Import-Module (dot-source)
=== Parent Invocation.MyCommand: [ Import-Module .\moduleDotSource ]
Powershell -Version 2 -File ...moduledetection.ps1
Powershell -Version 2 Dot-Source
$MyInvocation.MyCommand
包含导入模块语句。我还没有测试它,但我从中推断,如果它是通过链式点源的多重间接,我们可以继续采用父作用域,直到我们得到 Null 或 Import-Module。
关于powershell - 如何检测脚本是点源的,还是使用 powershell v2+ 作为模块的一部分加载的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17867953/
我的项目结构如下: MyScript.ps1 classes\ Car.ps1 Tesla.ps1 Car.ps1 是 Tesla.ps1 的基类。我试图在 Tesla.ps1 中这样
下面是一些 PowerShell 代码: 测试.ps1: . C:\path\to\test2.ps1 exit 5 测试2.ps1: exit 7 从标准命令提示符运行 test.ps1,但您喜欢运
我是一名优秀的程序员,十分优秀!