Quantcast
Channel: Windows PowerShell forum
Viewing all articles
Browse latest Browse all 21975

Module returns different result from the script version [Test-Path]

$
0
0

introduction

I've written my first PowerShell script aimed for retrieving detailed information from a Windows Setup ISO file. Once the basic features achieved, I've started to convert theps1 script into a psm1 module. I hoped the result would be the module just work like the script but I'm facing issue I'm not able to solve.

You can download my work here: https://goo.gl/9716Te

Script version and module (roughly translated to English from French).

I successfully installed the module in PSModulePath in:

[Environment]::GetFolderPath("mydocuments")\WindowsPowerShell\Modules

Command usage is very simple. You call it like that:

WinIsoInfo[[-Path]<String>][<CommonParameters>]

Help is provided by module: man WinIsoInfo

Usage Example:

WinIsoInfo-Path"E:\Win 10\Installation\ISO\Windows 10 x64 fr.iso"

The ps1 script version is the exact same code as the psm1 module but there are commands examples at the end of the file that you can un-comment and edit before running the script.

Current Status

All the tests are and need to be run as admin, in console or PowerShell ISE.

The ps1 script works as expected but the psm1 module doesn't produce the same result.

At line 108 of the code, there is a Test-Path in a Switch statement:

{(Test-Path"$wimPath\sources\install.wim")-or (Test-Path"$wimPath\sources\install.esd")}

In the ps1 script, this Test-Path return True and user get the expected info.

But in the psm1, it seems to return False since Switch statement jump to the next test after this one. So at the end the user gets that the ISO doesn't contain windows setup. I can assure that theTest-Path should return True because I manually checked it while the function was paused by breakpoints.

Hint and lead

There are 2 cases where I manage to get the module work as expected. But only using inPowerShell ISE, NOT in console.

Using Automatic Variable $? in console pane while debugging module

Step to reproduce:

  1. PowerShell ISE is not running.
  2. Open PowerShell ISE as admin.
  3. In console pane, run import-module Get-WinIsoInfo -Force -Global -Verbose orimport-module -path X:\Path\To\Modules\Get-WinIsoInfo -Force -Global -Verbose
  4. In console pane, run WinIsoInfo -Path "X:\path\to\AnyWindowsSetup.iso"
  5. In my case, at this point, the command returns there is no Windows Setup in ISO file.
  6. Now open the Get-WinIsoInfo.psm1 and put a breakpoints anywhere between line 90-108.
  7. do step 4 again
  8. While the script is paused at breakpoints, run $? in the console pane then pressF10 then F5

And "voilà !" the module return the expected result and will keep working but only duringPowerShell ISE session and inside PowerShell ISE. Command run in console still won't work. And the next time I runPowerShell ISE, the module won't find the setup image path again.

Previously run the ps1 script version in PowerShell ISE

Step to reproduce:

  1. PowerShell ISE is not running.
  2. Open PowerShell ISE as admin.
  3. In console pane, run import-module Get-WinIsoInfo -Force -Global -Verbose orimport-module -path X:\Path\To\Modules\Get-WinIsoInfo -Force -Global -Verbose
  4. In console pane, run WinIsoInfo -Path "X:\path\to\AnyWindowsSetup.iso"
  5. In my case, at this point, the command returns there is no Windows Setup in ISO file.
  6. Now open the Get-WinIsoInfo.ps1 script, edit a valid command at the end of the code then pressF5 to run it. Note: Since the command in script has the same name as the module previously imported, at this point I don't know if the triggered function is the one from theps1 script or the one from the module. Tell me if you know.
  7. The script returns the expected result as Windows Setup info.
  8. Close the ps1 file (it is no longer needed in PowerShell ISE for the next to work)
  9. do step 4 again

And "voilà !" the module return the expected result and will keep working but only duringPowerShell ISE session and inside PowerShell ISE. Command run in console still won't work. And the next time I runPowerShell ISE, the module won't find the setup image path again.

Conclusion

After the Hint and lead tests, I found out that they were some differences from modules imported in session before and after success. These key modules loaded byPowerShell ISE are Storage and Microsoft.WSMan.Management. I thought I found the solution and added this line to manifest:

RequiredModules=@("Storage";"Microsoft.PowerShell.Management";"Microsoft.PowerShell.Security";"Microsoft.PowerShell.Utility";"Microsoft.WSMan.Management")

I added all the modules that was present after the module works as expected, just to be sure.

I did the same for assemblies but 2 of them could not be imported: Microsoft.Management.Infrastructure.UserFilteredExceptionHandling andMicrosoft.Management.Infrastructure.resources

Resulting in this new manifest line:

RequiredAssemblies=@("Microsoft.Management.Infrastructure.Native";"Microsoft.WSMan.Runtime";"System.Security")

Unfortunately, it seems it is not enough to solve the issue.

Maybe other things has to be imported or it's a wrong lead.

I really hope you could reproduce the bug or at least I hope the Hint and lead section will lead you to find the cause and a solution. I'm too novice to understand why this happens on my system.

My setup uses PowerShell v5.0 with Win 8.1 Pro.


Viewing all articles
Browse latest Browse all 21975

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>