我已经
PowerShell Module
(我们称之为
PSModule
)其中我在.psd1中定义如下
# Script files (.ps1) that are run in the caller's environment prior to importing this module.
ScriptsToProcess = @('Enums\MessageType.ps1')
它被添加到那里以便文件
MessageType.ps1
那就是正义
Enums
可以跨多个文件重复使用。
此文件如下:
enum MessageType {
None
Something
}
一个例子是
PSWriteWord
-
https://github.com/EvotecIT/PSWriteWord
模块。这应该将枚举保存在它自己的文件夹中,并且在模块启动时仍然加载它们。
这在大多数情况下都很有效。我运行的脚本没有问题。现在我有了一个奇怪的场景,我创建了:script1.ps1-我称之为。
script1.ps1的内容
Import-Module PSModule -Force
Import-Module PSOtherModule -Force
Call-Me -Parameters <params> # part of PSOtherModule
现在在我调用的psoThermodule的call me函数中
Do-Something
do-Something
Call-OtherFunction # function from PSModule
它将在ISE或VScode中运行时工作…
现在,如果我重新运行
Task Scheduler
它不会加载
MessageType
在某种程度上会失败。似乎它只是跳过了
ScriptsToProcess
.
现在,如果你这样做:
Do-Something
do-Something
Import-Module PSModule
Call-OtherFunction # function from PSModule
它仍然不起作用…但这会…
Do-Something
do-Something
Import-Module PSModule -Force
Call-OtherFunction # function from PSModule
所以现在我正在努力寻找一种方法,将枚举作为单独的文件添加到我的模块中,并在调用psmodule之前,在不使用import模块的情况下保持运行。
我发现了这个:
https://d-fens.ch/2014/11/26/bug-powershell-scripts-in-scriptstoprocess-attribute-appear-as-loaded-modules/
它照它所说的做,效果很好,但不能解决我的问题。
有人知道解决这个问题的方法吗?我尝试将完整路径放入scriptstoprocess,认为scriptstoprocess中的枚举路径可能被某种方式覆盖…但没有。
my.psm1文件如下:
#Get public and private function definition files.
$Public = @( Get-ChildItem -Path $PSScriptRoot\Public\*.ps1 -ErrorAction SilentlyContinue )
$Private = @( Get-ChildItem -Path $PSScriptRoot\Private\*.ps1 -ErrorAction SilentlyContinue )
$Enums = @( Get-ChildItem -Path $PSScriptRoot\Enums\*.ps1 -ErrorAction SilentlyContinue )
#Dot source the files
Foreach ($import in @($Public + $Private + $Enums)) {
Try {
. $import.fullname
} Catch {
Write-Error -Message "Failed to import function $($import.fullname): $_"
}
}
Export-ModuleMember -Function '*'
[string] $ManifestFile = '{0}.psd1' -f (Get-Item $PSCommandPath).BaseName;
$ManifestPathAndFile = Join-Path -Path $PSScriptRoot -ChildPath $ManifestFile;
if ( Test-Path -Path $ManifestPathAndFile) {
$Manifest = (Get-Content -raw $ManifestPathAndFile) | iex;
foreach ( $ScriptToProcess in $Manifest.ScriptsToProcess) {
$ModuleToRemove = (Get-Item (Join-Path -Path $PSScriptRoot -ChildPath $ScriptToProcess)).BaseName;
if (Get-Module $ModuleToRemove) {
Remove-Module $ModuleToRemove;
}
}
}
我刚刚添加了枚举,但它没有改变任何东西…导出模块之后的事情是从模块中删除脚本(如链接中所示)。我的问题无关紧要。
可以在这里看到: