3

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
  • So you're running `Test-ModuleManifest` against a `*.psd1` file that references the `*.psm1` module that contains the `Get-AvState` function that uses `"$env:ProgramFiles\Microsoft Security Client\MpProvider"`? Please clarify by updating your question directly. It sounds like `Test-ModuleManifest` is mistakenly running against the _module itself_ rather than the _manifest_ (`*.psd1`) - if you put what would normally be an expandable string in a manifest, it will _not_ be expanded; it's treated as a _literal_, which would explain the error. – mklement0 Feb 21 '17 at 22:11
  • I am running Publish-Module -Path 'E:\Scripts\Utility\getAvState'-Repository 'CorpIT' and the error above is being generated. getAVState is a folder with the .psm1 and .psd1 files in it. – Michael Maher Feb 23 '17 at 09:53
  • @mklement0 The manifest really only has two noteworthy lines. `FunctionsToExport = 'Get-AVState' ` and `RootModule = 'getAVState.psm1'` – Michael Maher Feb 23 '17 at 09:57
  • Do you see the same error if you run `Test-ModuleManifest` stand-alone against the `*.psd1` file? If you run the cmdlets with `-Verbose` and possibly also `-Debug`, can you narrow down the problem? – mklement0 Feb 24 '17 at 23:08
  • No it doesn't like the .psd1 file `Publish-Module : The specified path 'C:\Users\MM\Desktop\getAvState\getAvState\getAVState.psd1' is not a valid directory` it wants a directory name – Michael Maher Feb 25 '17 at 14:43
  • Funny thing is I set-up the local repo as I don't have access to my job network. Now testing it myself it works. – Michael Maher Feb 25 '17 at 15:01
  • That's intriguing - and great to know how to create a local repository ad-hoc. I meant passing the `*.psd1` directly to _`Test-ModuleManifest`_, not `Publish-Module`, which indeed only takes a _directory_ path. Does `-Verbose` / `-Debug` provide any additional insights? – mklement0 Feb 25 '17 at 22:46

0 Answers0