Skip to content

Commit b74d009

Browse files
Nick RodriguezNick Rodriguez
Nick Rodriguez
authored and
Nick Rodriguez
committed
1 parent 487dd06 commit b74d009

File tree

1 file changed

+62
-57
lines changed

1 file changed

+62
-57
lines changed

Create-EXEFrom.ps1

+62-57
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@
2626
Use this flag to be prompted to select a directory in an Open File Dialog that will be zipped and added as a supplementary file.
2727
When the exe is run, this script will first be unzipped and all files are available.
2828
29-
.PARAMETER RemoveTempDir
30-
Set this to false to keep the temp directory around after the exe is created. It is available at the root of C:.
29+
.PARAMETER KeepTempDir
30+
Keep the temp directory around after the exe is created. It is available at the root of C:.
3131
3232
.PARAMETER x64
3333
Use the 64-bit iexpress path so that 64-bit PowerShell is consequently called.
@@ -44,7 +44,7 @@
4444
# Prompts the user to select the PowerShell script and supplemental files using an Open File Dialog.
4545
4646
.EXAMPLE
47-
.\Create-EXEFrom.ps1 -SupplementalDirectoryPath 'C:\Temp\MyTestDir' -RemoveTempDir $false
47+
.\Create-EXEFrom.ps1 -SupplementalDirectoryPath 'C:\Temp\MyTestDir' -KeepTempDir
4848
# Zips MyTestDir and attaches it to the exe. When the exe is run, but before the user's script gets run,
4949
# it will be extracted to the same directory as the user's script. Temp directory used during exe creation
5050
# will be left intact for user inspection or debugging purposes.
@@ -54,6 +54,13 @@
5454
5555
Requires iexpress, which is included in most versions of Windows (https://en.wikipedia.org/wiki/IExpress).
5656
57+
Version 1.6 - 4/25/16
58+
-Changed name of RemoveTempDir param to be a switch named KeepTempDir
59+
-Added ability to use the exe's root path in your PS script with "Split-Path -Parent $Args[0]"
60+
61+
Version 1.5 - 4/6/16
62+
-Added RunAs flag so iexpress is started as admin
63+
5764
Version 1.4 - 3/29/16
5865
-Added x64 switch for creating exe using 64-bit iexpress.
5966
@@ -326,13 +333,13 @@ process {
326333
Write-Verbose "PowerShell script selected: $PSScriptPath"
327334

328335
# Name of the extensionless target, replace spaces with underscores
329-
$target = ($PSScriptName -replace '.ps1', '') -replace " ", '_'
336+
$Target = ($PSScriptName -replace '.ps1', '') -replace " ", '_'
330337

331338
# Get the directory the script was found in
332339
$ScriptRoot = $PSScriptPath.Substring(0, $PSScriptPath.LastIndexOf('\'))
333340

334341
# Create temp directory to store all files
335-
$Temp = New-Item "C:\$target$(Get-Date -Format "HHmmss")" -ItemType Directory -Force
342+
$Temp = New-Item "C:\$Target$(Get-Date -Format "HHmmss")" -ItemType Directory -Force
336343
Write-Verbose "Using temp directory $Temp"
337344

338345
# Copy the PowerShell script to our temp directory
@@ -394,37 +401,37 @@ process {
394401

395402
# If creating 64-bit exe, append to name to clarify
396403
if ($x64) {
397-
$exe = "$ScriptRoot\$target (x64).exe"
404+
$EXE = "$ScriptRoot\$Target (x64).exe"
398405
} else {
399-
$exe = "$ScriptRoot\$target.exe"
406+
$EXE = "$ScriptRoot\$Target.exe"
400407
}
401-
Write-Verbose "Target EXE: $exe"
408+
Write-Verbose "Target EXE: $EXE"
402409

403410
# create the sed file used by iexpress
404-
$sed = "$Temp\$target.sed"
405-
New-Item $sed -ItemType File -Force | Out-Null
411+
$SED = "$Temp\$Target.sed"
412+
New-Item $SED -ItemType File -Force | Out-Null
406413

407414
# populate the sed with config info
408-
Add-Content $sed "[Version]"
409-
Add-Content $sed "Class=IEXPRESS"
410-
Add-Content $sed "sedVersion=3"
411-
Add-Content $sed "[Options]"
412-
Add-Content $sed "PackagePurpose=InstallApp"
413-
Add-Content $sed "ShowInstallProgramWindow=0"
414-
Add-Content $sed "HideExtractAnimation=1"
415-
Add-Content $sed "UseLongFileName=1"
416-
Add-Content $sed "InsideCompressed=0"
417-
Add-Content $sed "CAB_FixedSize=0"
418-
Add-Content $sed "CAB_ResvCodeSigning=0"
419-
Add-Content $sed "RebootMode=N"
420-
Add-Content $sed "TargetName=%TargetName%"
421-
Add-Content $sed "FriendlyName=%FriendlyName%"
422-
Add-Content $sed "AppLaunched=%AppLaunched%"
423-
Add-Content $sed "PostInstallCmd=%PostInstallCmd%"
424-
Add-Content $sed "SourceFiles=SourceFiles"
425-
Add-Content $sed "[Strings]"
426-
Add-Content $sed "TargetName=$exe"
427-
Add-Content $sed "FriendlyName=$target"
415+
Add-Content $SED "[Version]"
416+
Add-Content $SED "Class=IEXPRESS"
417+
Add-Content $SED "sedVersion=3"
418+
Add-Content $SED "[Options]"
419+
Add-Content $SED "PackagePurpose=InstallApp"
420+
Add-Content $SED "ShowInstallProgramWindow=0"
421+
Add-Content $SED "HideExtractAnimation=1"
422+
Add-Content $SED "UseLongFileName=1"
423+
Add-Content $SED "InsideCompressed=0"
424+
Add-Content $SED "CAB_FixedSize=0"
425+
Add-Content $SED "CAB_ResvCodeSigning=0"
426+
Add-Content $SED "RebootMode=N"
427+
Add-Content $SED "TargetName=%TargetName%"
428+
Add-Content $SED "FriendlyName=%FriendlyName%"
429+
Add-Content $SED "AppLaunched=%AppLaunched%"
430+
Add-Content $SED "PostInstallCmd=%PostInstallCmd%"
431+
Add-Content $SED "SourceFiles=SourceFiles"
432+
Add-Content $SED "[Strings]"
433+
Add-Content $SED "TargetName=$EXE"
434+
Add-Content $SED "FriendlyName=$Target"
428435

429436
# If we've zipped a file, we need to modify things
430437
if ('SelectDirectory', 'SpecifyDirectory' -contains $PSCmdlet.ParameterSetName) {
@@ -434,51 +441,49 @@ process {
434441
Add-Content $UnZipScript $UnZipFunction
435442
Add-Content $UnZipScript "UnZip-File `'$SupplementalFiles`'"
436443
# If we're dealing with a zip file, we need to set the primary command to unzip the user's files
437-
Add-Content $sed "AppLaunched=cmd /c PowerShell -ExecutionPolicy Bypass -File `"$UnZipScript`""
444+
Add-Content $SED "AppLaunched=cmd /c PowerShell -ExecutionPolicy Bypass -File `"$UnZipScript`""
438445
# After we've staged our files, run the user's script
439-
Add-Content $sed "PostInstallCmd=cmd /c PowerShell -ExecutionPolicy Bypass -File `"$PSScriptName`""
440-
Add-Content $sed "FILE0=UnZip.ps1"
441-
Add-Content $sed "FILE1=$PSScriptName"
446+
Add-Content $SED "PostInstallCmd=cmd /c for /f `"skip=1 tokens=1* delims=`" %i in (`'wmic process where `"name=`'$target.exe`'`" get ExecutablePath`') do PowerShell -ExecutionPolicy Bypass -Command Clear-Host; `".\$PSScriptName`" `"%i`" & exit"
447+
Add-Content $SED "FILE0=UnZip.ps1"
448+
Add-Content $SED "FILE1=$PSScriptName"
442449
} else {
443450
$IndexOffset = 1
444-
Add-Content $sed "AppLaunched=cmd /c PowerShell -ExecutionPolicy Bypass -File `"$PSScriptName`""
445-
Add-Content $sed "PostInstallCmd=<None>"
446-
Add-Content $sed "FILE0=$PSScriptName"
451+
Add-Content $SED "AppLaunched=cmd /c for /f `"skip=1 tokens=1* delims=`" %i in (`'wmic process where `"name=`'$target.exe`'`" get ExecutablePath`') do PowerShell -ExecutionPolicy Bypass -Command Clear-Host; `".\$PSScriptName`" `"%i`" & exit"
452+
Add-Content $SED "PostInstallCmd=<None>"
453+
Add-Content $SED "FILE0=$PSScriptName"
447454
}
448455

449456
# Add the ps1 and supplemental files
450457
If ($SupplementalFiles) {
451-
$index = $IndexOffset
452-
ForEach ($file in $SupplementalFiles) {
453-
$index++
454-
Add-Content $sed "FILE$index=$file"
458+
$Index = $IndexOffset
459+
ForEach ($File in $SupplementalFiles) {
460+
$Index++
461+
Add-Content $SED "FILE$Index=$File"
455462
}
456463
}
457-
Add-Content $sed "[SourceFiles]"
458-
Add-Content $sed "SourceFiles0=$Temp"
464+
Add-Content $SED "[SourceFiles]"
465+
Add-Content $SED "SourceFiles0=$Temp"
459466

460-
Add-Content $sed "[SourceFiles0]"
461-
Add-Content $sed "%FILE0%="
467+
Add-Content $SED "[SourceFiles0]"
468+
Add-Content $SED "%FILE0%="
462469
if ('SelectDirectory', 'SpecifyDirectory' -contains $PSCmdlet.ParameterSetName) {
463470
# We've already specified this file, so leave it blank here
464-
Add-Content $sed "%FILE1%="
471+
Add-Content $SED "%FILE1%="
465472
}
466473
# Add the ps1 and supplemental files
467474
If ($SupplementalFiles) {
468-
$index = $IndexOffset
469-
ForEach ($file in $SupplementalFiles) {
470-
$index++
471-
Add-Content $sed "%FILE$index%="
475+
$Index = $IndexOffset
476+
ForEach ($File in $SupplementalFiles) {
477+
$Index++
478+
Add-Content $SED "%FILE$Index%="
472479
}
473480
}
474481

475-
Write-Verbose "SED file contents: `n$(Get-Content $sed | Out-String)"
482+
Write-Verbose "SED file contents: `n$(Get-Content $SED | Out-String)"
476483

477-
# Call IExpress to create exe from the sed we just created
478-
Start-Process $IExpress "/N $sed" -Wait
484+
# Call IExpress to create exe from the sed we just created (run as admin)
485+
Start-Process $IExpress "/N $SED" -Wait -Verb RunAs
479486

480-
if ($RemoveTempDir) {
481-
# Clean up
482-
Remove-Item $Temp -Recurse -Force
483-
}
487+
# Clean up unless user specified not to
488+
if (-not $KeepTempDir) { Remove-Item $Temp -Recurse -Force }
484489
}

0 commit comments

Comments
 (0)