Test-ModuleManifest在模块中的环境变量上失败

时间:2017-02-21 19:45:42

标签: powershell powershell-module

我有一个内部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

0 个答案:

没有答案