26
26
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.
27
27
When the exe is run, this script will first be unzipped and all files are available.
28
28
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:.
31
31
32
32
. PARAMETER x64
33
33
Use the 64-bit iexpress path so that 64-bit PowerShell is consequently called.
44
44
# Prompts the user to select the PowerShell script and supplemental files using an Open File Dialog.
45
45
46
46
. EXAMPLE
47
- .\Create-EXEFrom.ps1 -SupplementalDirectoryPath 'C:\Temp\MyTestDir' -RemoveTempDir $false
47
+ .\Create-EXEFrom.ps1 -SupplementalDirectoryPath 'C:\Temp\MyTestDir' -KeepTempDir
48
48
# Zips MyTestDir and attaches it to the exe. When the exe is run, but before the user's script gets run,
49
49
# it will be extracted to the same directory as the user's script. Temp directory used during exe creation
50
50
# will be left intact for user inspection or debugging purposes.
54
54
55
55
Requires iexpress, which is included in most versions of Windows (https://en.wikipedia.org/wiki/IExpress).
56
56
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
+
57
64
Version 1.4 - 3/29/16
58
65
-Added x64 switch for creating exe using 64-bit iexpress.
59
66
@@ -326,13 +333,13 @@ process {
326
333
Write-Verbose " PowerShell script selected: $PSScriptPath "
327
334
328
335
# Name of the extensionless target, replace spaces with underscores
329
- $target = ($PSScriptName -replace ' .ps1' , ' ' ) -replace " " , ' _'
336
+ $Target = ($PSScriptName -replace ' .ps1' , ' ' ) -replace " " , ' _'
330
337
331
338
# Get the directory the script was found in
332
339
$ScriptRoot = $PSScriptPath.Substring (0 , $PSScriptPath.LastIndexOf (' \' ))
333
340
334
341
# 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
336
343
Write-Verbose " Using temp directory $Temp "
337
344
338
345
# Copy the PowerShell script to our temp directory
@@ -394,37 +401,37 @@ process {
394
401
395
402
# If creating 64-bit exe, append to name to clarify
396
403
if ($x64 ) {
397
- $exe = " $ScriptRoot \$target (x64).exe"
404
+ $EXE = " $ScriptRoot \$Target (x64).exe"
398
405
} else {
399
- $exe = " $ScriptRoot \$target .exe"
406
+ $EXE = " $ScriptRoot \$Target .exe"
400
407
}
401
- Write-Verbose " Target EXE: $exe "
408
+ Write-Verbose " Target EXE: $EXE "
402
409
403
410
# 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
406
413
407
414
# 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 "
428
435
429
436
# If we've zipped a file, we need to modify things
430
437
if (' SelectDirectory' , ' SpecifyDirectory' -contains $PSCmdlet.ParameterSetName ) {
@@ -434,51 +441,49 @@ process {
434
441
Add-Content $UnZipScript $UnZipFunction
435
442
Add-Content $UnZipScript " UnZip-File `' $SupplementalFiles `' "
436
443
# 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 `" "
438
445
# 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 "
442
449
} else {
443
450
$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 "
447
454
}
448
455
449
456
# Add the ps1 and supplemental files
450
457
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 "
455
462
}
456
463
}
457
- Add-Content $sed " [SourceFiles]"
458
- Add-Content $sed " SourceFiles0=$Temp "
464
+ Add-Content $SED " [SourceFiles]"
465
+ Add-Content $SED " SourceFiles0=$Temp "
459
466
460
- Add-Content $sed " [SourceFiles0]"
461
- Add-Content $sed " %FILE0%="
467
+ Add-Content $SED " [SourceFiles0]"
468
+ Add-Content $SED " %FILE0%="
462
469
if (' SelectDirectory' , ' SpecifyDirectory' -contains $PSCmdlet.ParameterSetName ) {
463
470
# We've already specified this file, so leave it blank here
464
- Add-Content $sed " %FILE1%="
471
+ Add-Content $SED " %FILE1%="
465
472
}
466
473
# Add the ps1 and supplemental files
467
474
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 %="
472
479
}
473
480
}
474
481
475
- Write-Verbose " SED file contents: `n $ ( Get-Content $sed | Out-String ) "
482
+ Write-Verbose " SED file contents: `n $ ( Get-Content $SED | Out-String ) "
476
483
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
479
486
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 }
484
489
}
0 commit comments