Page 1 of 1

Import-Module when EXE-packaged

Posted: Wed Feb 15, 2012 10:56 pm
by mssapien
Import-Module gives access to cmdlets when PS1-scripted, but not when EXE-packaged. What do I have to do so that the module gets imported and I can its cmdlets even when the script or project is packaged and run as an EXE?

Import-Module when EXE-packaged

Posted: Thu Feb 16, 2012 3:12 am
by davidc
The packaged exe should import-modules as long as you call the Import-Module cmdlet and the module isn't host specific. You also have to make sure the Module is installed in whatever system you run the exe. David

Import-Module when EXE-packaged

Posted: Thu Feb 16, 2012 6:25 pm
by mssapien
It does not work. Try exporting the following script package to an EXE and run it on a Windows Server 2008 R2, and you will see that c:command.txt gets created since it is an innate cmdlet to PowerShell, while c:windowsfeatures.txt does not get created since it is dependent on the module ServerManager. Running the code manually in PowerShell creates both files.Import-Module ServerManagerGet-WindowsFeature > c:windowsfeatures.txtGet-Command > c:commands.txt

Import-Module when EXE-packaged

Posted: Thu Feb 16, 2012 6:40 pm
by Alexander Riedel
Are you getting an error message when using Import-Module?
If so, what does it say?

Are you packaging as a 32 bit exe and running in a 64 bit shell otherwise?

Import-Module when EXE-packaged

Posted: Thu Feb 16, 2012 6:51 pm
by mssapien
The EXE just silently runs with no error messages. It did not help to package the EXE as 32-bits instead of 64-bits, or with elevation rights.I updated the code to this:try { Import-Module ServerManager Get-WindowsFeature > c:windowsfeatures.txt Get-Command > c:commands.txt} catch { $_ > c:exception.txt}c:exception.txt gets created with this message, which shows that the Import-Module did not work:The term 'Get-WindowsFeature' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.At line:28 char:20+ Get-WindowsFeature <<<< > c:windowsfeatures.txt + CategoryInfo : ObjectNotFound: (Get-WindowsFeature:String) [], CommandNotFoundException + FullyQualifiedErrorId : CommandNotFoundException

Import-Module when EXE-packaged

Posted: Thu Feb 16, 2012 7:04 pm
by Alexander Riedel
Usually the Import-Module would fail with an error message if the module is not there or cannot be loaded.

If you leave the output redirection out, do you get any output if packaged?

Import-Module ServerManager
Get-WindowsFeature

Import-Module when EXE-packaged

Posted: Thu Feb 16, 2012 7:08 pm
by mssapien
I tried that code, but still no messagebox or something of information appears. test.exe runs silently for a brief moment and then exits, as shown in task manager.Absolute path did not work either:Import-Module C:Windowssystem32WindowsPowerShellv1.0ModulesServerManagerServerManager.psd1I am using PrimalForms 2011 Version 2.0.20 (newest)

Import-Module when EXE-packaged

Posted: Thu Feb 16, 2012 10:40 pm
by mssapien
Sorry, I may have misunderstood your instructions. I thought you were speaking about the 64-bit vs 32-bit choice in the Home tab. I found a similar option in the dialog box when building EXE, and when I selected 64-bit there, it worked.