I have an internal PowerShell repository which is utilises the Package Management capabilities of PowerShell 5.x.
I hit an issue with Test-ModuleManifest which is called behind the scenes when running Publish-Module.
The use of $env:ProgramFiles in my module causing the problem.
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.
If I hardcode this value the module is published fine.
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{}
}
Here is the hard coded path for C:\Program Files that works
Invoke-Command -computerName $HOSTNAME -ScriptBlock {Import-Module "C:\Program Files\Microsoft Security Client\MpProvider" -ErrorAction SilentlyContinue;Get-MProtComputerStatus}
To create a local PowerShell repository to test this you can run the following.
Setup a local repository
Register-PackageSource -Name Local -Location '\\localhost\C$\temp' –ProviderName PowerShellget -Trusted
Try publish
Publish-Module -Path C:\Users\MM\Desktop\getAvState\getAvState -Repository Local -Verbose
Finally unregister the local repo
Unregister-PackageSource -Source Local
This is Test-ModuleManiest when run on the manifest file with the module set to use the hardcoded value of "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
This is when the following is included in the module "$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