Generate EXE files from ps1

I have just started packaging my scripts into EXE files from which smaller utilities which I then put in our remote connection ‘toolbox’ so our technicians can run these scripts on remote computers.

One issue I noticed is that I can’t create scripts with Get-ADUser, convert them to an EXE file and then have that script run when I launch the EXE on a server.

As a test, I confirmed the following ps1 file with this command into an EXE file:

Get-ADComputer -Filter * | select name | Export-Csv C:\utility\inventorytest.csv -NoTypeInformation

This command runs fine from the server (I am logged in as Domain admin) and creates the obvious csv file. However, when I convert to an EXE file, it will not run create the file (still logged in as domain admin).

I ran the exe directly from the command prompt and it is flagging the ‘get-adcomputer’ as unrecognised. I added ‘import-module activedirectory’ but I receive the same error.

Is what I want to do possible? I would before an EXE file that our technicians can just launch rather than copy and pasting a PS script to a server.

`C:\Users\Administrator\Documents\TechPro Control\Temp>Test-inventory.exe</code>
<code>ERROR: import-module : The ‘C:\Windows\system32\WindowsPowerShell\v1.0\Modules\activedirectory\activedirectory.psd1’ module cannot</code>
<code>ERROR: be imported because its manifest contains one or more members that are not valid. The valid manifest members are (‘ModuleToP</code>
<code>ERROR: rocess’, ‘NestedModules’, ‘GUID’, ‘Author’, ‘CompanyName’, ‘Copyright’, ‘ModuleVersion’, ‘Description’, ‘PowerShellVersion’,</code>
<code>ERROR: ‘PowerShellHostName’, ‘PowerShellHostVersion’, ‘CLRVersion’, ‘DotNetFrameworkVersion’, ‘ProcessorArchitecture’, ‘RequiredMo</code>
<code>ERROR: dules’, ‘TypesToProcess’, ‘FormatsToProcess’, ‘ScriptsToProcess’, ‘PrivateData’, ‘RequiredAssemblies’, ‘ModuleList’, ‘FileLi</code>
<code>ERROR: st’, ‘FunctionsToExport’, ‘VariablesToExport’, ‘AliasesToExport’, ‘CmdletsToExport’). Remove the members that are not valid</code>
<code>ERROR: (‘HelpInfoUri’), then try to import the module again.</code>
<code>ERROR: At line:7 char:14</code>
<code>ERROR: + import-module <<<< activedirectory</code>
<code>ERROR: + CategoryInfo : InvalidData: (C:\Windows\syst…edirectory.psd1:String) [Import-Module], InvalidOperationExce</code>
<code>ERROR: ption</code>
<code>ERROR: + FullyQualifiedErrorId : Modules_InvalidManifestMember,Microsoft.PowerShell.Commands.ImportModuleCommand</code>
<code>ERROR: Get-ADComputer : The term ‘Get-ADComputer’ is not recognized as the name of a cmdlet, function, script file, or operable pro</code>
<code>ERROR: gram. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.</code>
<code>ERROR: At line:8 char:15</code>
<code>ERROR: + Get-ADComputer <<<< -Filter * | select name | Export-Csv C:\utility\inventorytest.csv -NoTypeInformation</code>
<code>ERROR: + CategoryInfo : ObjectNotFound: (Get-ADComputer:String) , CommandNotFoundException</code>
<code>ERROR: + FullyQualifiedErrorId : CommandNotFoundException</code>

<code> `

One question I would have is the method you chose for converting to an EXE? I use ISESteroids, converted your sample code and it ran just fine as an EXE.

Looks like the cause is Powershell is running as version 2.0 which most likely defined in the EXE wrapper. Are there settings to manipulate the Powershell session for the EXE?

How you are converting ps1 to exe ? Are you using Ps2Exe ?

PowerShell Studio.

I did choose V2 by default since this might have to run on older version of Windows servers…it failed on Windows 2008 R2 server.

I ran into a similar issue as I needed to run some converted scripts on XP (dont laugh). I worked with the vendor of the tool I was using (ISESteroids) to eventually get it done. I had to go back to a much older version of the conversion tool. I would think you will need to contact PowerShell Studio to get it done.