Powershell 测试模块对模块中的环境变量进行测试失败
我有一个内部PowerShell存储库,它利用PowerShell 5.x的包管理功能 我遇到了TestModuleManifest的一个问题,它在运行发布模块时在后台调用 在我的模块中使用$env:ProgramFiles导致了该问题Powershell 测试模块对模块中的环境变量进行测试失败,powershell,powershell-module,Powershell,Powershell Module,我有一个内部PowerShell存储库,它利用PowerShell 5.x的包管理功能 我遇到了TestModuleManifest的一个问题,它在运行发布模块时在后台调用 在我的模块中使用$env:ProgramFiles导致了该问题 Microsoft.PowerShell.Core\Test-ModuleManifest : Cannot find drive. A drive with the name '$env' does not exist. At C:\Program File
Microsoft.PowerShell.Core\Test-ModuleManifest : Cannot find drive. A drive with the name '$env' does not exist.
At C:\Program Files\Modules\PowerShellGet\1.0.0.1\PSModule.psm1:693 char:27
Category Info: Object not found: ($env:String) [Test-ModuleManifest], DriveNotFoundExeception
FullyQualifiedErrorID: DriveNotFound, Microsoft.PowerShell.Commands.TestModuleManifestCommand
Publish-Module: The specified module with path 'E:\Scripts\getAVState' was not published because no valid module was found with that path.
如果我硬编码此值,则模块可以正常发布
Function Get-AvState{
[CmdletBinding()]
Param ([Parameter(Mandatory = $true, ValueFromPipelineByPropertyName)]
[Alias("Name")]
[string]$Hostname)
Begin{
}
Process{
Try {
Invoke-Command -computerName $HOSTNAME -ScriptBlock {Import-Module "$env:ProgramFiles\Microsoft Security Client\MpProvider" -ErrorAction SilentlyContinue;Get-MProtComputerStatus} |
Select-Object @{N="Hostname";E={$hostname}},AntiVirusenabled, OnAccessProtectionEnabled, RealtimeProtectionEnabled
}
Catch
{
$noAntiVirus = New-Object PSObject
Add-Member -inputObject $noAntiVirus -memberType NoteProperty -name "AntiVirusEnabled" -value 'No'
Add-Member -inputObject $noAntiVirus -memberType NoteProperty -name "OnAccessProtectionEnabled" -value 'No'
Add-Member -inputObject $noAntiVirus -memberType NoteProperty -name "RealtimeProtectionEnabled" -value 'No'
$noAntiVirus | Select-Object @{N="Hostname";E={$hostname}},AntiVirusEnabled, OnAccessProtectionEnabled, RealtimeProtectionEnabled
}
}
End{}
}
以下是C:\Program文件的硬编码路径,该路径有效
Invoke-Command -computerName $HOSTNAME -ScriptBlock {Import-Module "C:\Program Files\Microsoft Security Client\MpProvider" -ErrorAction SilentlyContinue;Get-MProtComputerStatus}
要创建本地PowerShell存储库以测试此功能,可以运行以下操作
设置本地存储库
Register PackageSource-Name Local-Location'\\localhost\C$\temp'-ProviderName PowerShellget-Trusted
尝试发布
Publish模块-路径C:\Users\MM\Desktop\getAvState\getAvState-Repository Local-Verbose
最后注销本地回购协议
取消注册PackageSource-源本地
当在清单文件上运行并将模块设置为使用硬编码值“C:\Program Files\Microsoft Security Client\MpProvider”时,这是测试模块最大值
此时模块“$env:ProgramFiles\Microsoft Security Client\MpProvider”中包含以下内容
因此,您正在对一个引用
*.psm1
模块的Test ModuleManifest
文件运行Test ModuleManifest
,该模块包含使用“$env:ProgramFiles\Microsoft Security Client\MpProvider”的函数?请通过直接更新您的问题进行澄清。听起来好像Test ModuleManifest
错误地针对模块本身而不是清单(*.psd1
)运行-如果您将通常是可扩展字符串的内容放入清单中,它将不会被扩展;它被视为一个文本,这可以解释错误。我正在运行发布模块路径“E:\Scripts\Utility\getAvState”-存储库“CorpIT”,上面的错误正在生成。getAVState是一个文件夹,其中包含.psm1和.psd1文件。@mklement0清单实际上只有两行值得注意的内容FunctionsToExport='Get AVState'
和RootModule='getAVState.psm1'
如果针对*.psd1
文件单独运行Test ModuleManifest
是否会看到相同的错误?如果使用-Verbose
和-Debug
运行cmdlet,可以缩小问题范围吗?不,它不喜欢.psd1文件发布模块:指定路径“C:\Users\MM\Desktop\getAvState\getAvState\getAvState.psd1”不是有效目录
它需要一个目录名
Test-ModuleManifest -Path .\getAVState.psd1 -Verbose
VERBOSE: Loading module from path 'E:\Scripts\Utility\getAvState\getAVState.psm1'.
VERBOSE: Loading module from path 'C:\Program Files\Microsoft Security Client\MpProvider\MSFT_MpComputerStatus.cdxml'.
VERBOSE: Loading module from path 'C:\Program Files\Microsoft Security Client\MpProvider\MSFT_MpPreference.cdxml'.
VERBOSE: Loading module from path 'C:\Program Files\Microsoft Security Client\MpProvider\MSFT_MpThreat.cdxml'.
VERBOSE: Loading module from path 'C:\Program Files\Microsoft Security Client\MpProvider\MSFT_MpThreatCatalog.cdxml'.
VERBOSE: Loading module from path 'C:\Program Files\Microsoft Security Client\MpProvider\MSFT_MpThreatDetection.cdxml'.
VERBOSE: Loading module from path 'C:\Program Files\Microsoft Security Client\MpProvider\MSFT_MpScan.cdxml'.
VERBOSE: Loading module from path 'C:\Program Files\Microsoft Security Client\MpProvider\MSFT_MpSignature.cdxml'.
VERBOSE: Loading module from path 'C:\Program Files\Microsoft Security Client\MpProvider\MSFT_MpWDOScan.cdxml'.
ModuleType Version Name ExportedCommands
---------- ------- ---- ----------------
Script 1.8.0 getAVState Get-AVState
Test-ModuleManifest -Path .\getAVState.psd1 -Verbose
VERBOSE: Loading module from path 'E:\Scripts\Utility\getAvState\getAVState.psm1'.
ModuleType Version Name ExportedCommands
---------- ------- ---- ----------------
Script 1.8.0 getAVState Get-AVState