PowerShell.iss 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. ; -- PowerShell.iss --
  2. ; Demonstrates calling Powershell at compile time and at run time.
  3. ; At compile time it first generates a random password and then it shows it and copies it to the clipboard.
  4. ; At run time it shows the serial number of the system.
  5. ; SEE THE DOCUMENTATION FOR DETAILS ON CREATING .ISS SCRIPT FILES!
  6. #define PowerShellExe "powershell.exe"
  7. #define PowerShellCommandParam "-ExecutionPolicy Bypass -Command"
  8. #define ExecPowerShell(str Command) \
  9. Local[0] = PowerShellCommandParam + " " + AddQuotes(Command), \
  10. Message("Executing PowerShell command: " + Local[0]), \
  11. ExecAndGetFirstLine(PowerShellExe, Local[0])
  12. #define Password ExecPowerShell( \
  13. "Add-Type -AssemblyName 'System.Web';" + \
  14. "[System.Web.Security.Membership]::GeneratePassword(12, 4);")
  15. #expr ExecPowerShell( \
  16. "$Password = '" + Password + "';" + \
  17. "Set-Clipboard -Value $Password;" + \
  18. "Add-Type -AssemblyName System.Windows.Forms;" + \
  19. "[System.Windows.Forms.MessageBox]::Show(" + \
  20. "'The generated password (copied to clipboard) is: ' + $Password + '" + NewLine + NewLine + \
  21. "Click OK to continue.', 'ISPP')")
  22. [Setup]
  23. AppName=My Program
  24. AppVersion=1.5
  25. WizardStyle=modern
  26. DefaultDirName={autopf}\My Program
  27. DefaultGroupName=My Program
  28. UninstallDisplayIcon={app}\MyProg.exe
  29. Compression=lzma2
  30. SolidCompression=yes
  31. OutputDir=userdocs:Inno Setup Examples Output
  32. Password={#Password}
  33. [Files]
  34. Source: "MyProg.exe"; DestDir: "{app}"
  35. Source: "MyProg.chm"; DestDir: "{app}"
  36. Source: "Readme.txt"; DestDir: "{app}"; Flags: isreadme
  37. [Icons]
  38. Name: "{group}\My Program"; Filename: "{app}\MyProg.exe"
  39. [Code]
  40. var
  41. Line: String;
  42. procedure ExecAndGetFirstLineLog(const S: String; const Error, FirstLine: Boolean);
  43. begin
  44. if not Error and (Line = '') and (Trim(S) <> '') then
  45. Line := S; { First non-empty line found, store it }
  46. Log('Exec output: ' + S);
  47. end;
  48. function ExecAndGetFirstLine(const Filename, Params, WorkingDir: String; var ResultCode: Integer): String;
  49. begin
  50. Line := '';
  51. try
  52. ExecAndLogOutput(Filename, Params, WorkingDir, SW_SHOWNORMAL, ewWaitUntilTerminated, ResultCode, @ExecAndGetFirstLineLog);
  53. except
  54. Log(GetExceptionMessage);
  55. end;
  56. Result := Line;
  57. end;
  58. function ExecPowerShell(const Command: String): String;
  59. var
  60. FullCommand: String;
  61. ResultCode: Integer;
  62. begin
  63. FullCommand := '{#PowerShellCommandParam} ' + AddQuotes(Command);
  64. Log('Executing PowerShell command: ' + FullCommand);
  65. Result := ExecAndGetFirstLine('{#PowerShellExe}', FullCommand, '', ResultCode);
  66. end;
  67. function InitializeSetup: Boolean;
  68. var
  69. SerialNumber: String;
  70. begin
  71. SerialNumber := ExecPowerShell('Get-WmiObject -Class Win32_BIOS | Select-Object -ExpandProperty SerialNumber');
  72. MsgBox(Format('Serial number: %s'#10#10'Click OK to continue.', [SerialNumber]), mbInformation, MB_OK);
  73. Result := True;
  74. end;