3

我有一个内部 PowerShell 存储库,它利用 PowerShell 5.x 的包管理功能。

我遇到了 Test-ModuleManifest 的问题,它在运行 Publish-Module 时在后台调用。

在我的模块中使用 $env:ProgramFiles 会导致问题。

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 Files 的硬编码路径

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-Module -Path C:\Users\MM\Desktop\getAvState\getAvState -Repository Local -Verbose

最后注销本地仓库 Unregister-PackageSource -Source Local

这是 Test-ModuleManiest 在清单文件上运行时,模块设置为使用“C:\Program Files\Microsoft Security Client\MpProvider”的硬编码值

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

这是当模块“$env:ProgramFiles\Microsoft Security Client\MpProvider”中包含以下内容时

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
4

0 回答 0