通过引用从Inno Setup传递变量到PowerShell脚本以检索结果

时间:2017-11-30 18:13:16

标签: powershell inno-setup pascalscript

我正在尝试通过引用将errorerror_msg变量传递给PowerShell脚本。但它没有用。它不反映checkHost.ps1内所做的更改。

var
  error_msg: String;
  error: String;
  ok: String;

function NextButtonClick(CurPageID: Integer): Boolean;
begin
  error_msg := 'all well';
  Result := True;
  error := 'false';
  ok := 'true';

  exec('powershell.exe',
    '-noexit -executionpolicy bypass ' +
    '"D:\Hyperv_ins_BM\checkHost.ps1 [ref]error   [ref]error_msg"',
    ExpandConstant('{tmp}'), SW_SHOW, ewWaitUntilTerminated, ResultCode);
end; 

有人可以帮忙吗?

2 个答案:

答案 0 :(得分:0)

这是不可能的。 PowerShell是一个简单的控制台应用程序,它不是一个API。

正如@Bill_Steward建议的那样,让PowerShell脚本将其结果输出到文件并在Inno Setup中读回:

function CheckHost(var ErrorMessage: string): Boolean;
var
  Parameters: string;
  TempPath: string;
  AErrorMessage: AnsiString;
  ResultCode: Integer;
begin
  TempPath := ExpandConstant('{tmp}\checkHost.err');
  Parameters :=
    '-ExecutionPolicy bypass -File "C:\path\to\script\checkHost.ps1" ' +
    '-ErrorFile "' + TempPath + '"';
  Result :=
    Exec('powershell.exe', Parameters, '', SW_HIDE, ewWaitUntilTerminated, ResultCode) and
    (ResultCode = 0);
  if not Result then
  begin
    if LoadStringFromFile(TempPath, AErrorMessage) then
    begin
      ErrorMessage := AErrorMessage;
    end;
  end;
  DeleteFile(TempPath);
end;

以上内容基于How to get an output of an Exec'ed program in Inno Setup?

使用如下功能:

var
  ErrorMessage: string;
begin
  if CheckHost(ErrorMessage) then
  begin
    MsgBox('Host check succeeded', mbInformation, MB_OK);
  end
    else
  begin
    MsgBox(ErrorMessage, mbError, MB_OK);
  end;
end;

PowerShell脚本可能如下所示:

param (
    $errorFile
)

if (checkHost())
{
    exit 0
}
else
{
    Set-Content -Path $errorFile -Value "Host check failed for some reason" 
    exit 1
}

尽管如同@Bill_Steward所提到的那样,很有可能您可以在纯Pascal脚本中实现检查。但这是针对不同的问题。

答案 1 :(得分:-1)

我在下面尝试了示例代码。将创建checkHost.err并在那里添加测试。

; Script generated by the Inno Setup Script Wizard.
; SEE THE DOCUMENTATION FOR DETAILS ON CREATING INNO SETUP SCRIPT    FILES!

#define PUBLISHER "AtoZ Technology, Inc."
#define PUBLISHER_URL "http://www.xyzabc.com/"
#define PRODUCT_NAME "test"



[Setup]
; NOTE: The value of AppId uniquely identifies this application.
; Do not use the same AppId value in installers for other applications.
; (To generate a new GUID, click Tools | Generate GUID inside the IDE.)

AppPublisher={#PUBLISHER}
AppPublisherURL={#PUBLISHER_URL}
AppCopyright=Copyright (C) 2013-2016 {#PUBLISHER}
AppName=test installer
AppId=TestInstaller
AppVerName=Test Installer
DefaultDirName={pf}\Test Install
DefaultGroupName=Test Install
Compression=lzma2
ArchitecturesAllowed=x64
ArchitecturesInstallIn64BitMode=x64
OutputBaseFilename=test_installer
OutputDir="D:\Test"
LicenseFile=lics.txt
SetupLogging=yes
VersionInfoProductName=Test  ]

VersionInfoProductTextVersion= "1.0"

[Files]
Source: "checkHost.ps1"; DestDir: "{app}"; Flags: ignoreversion

[Code]
function CheckHost(var ErrorMessage: string): Boolean;
var
Parameters: string;
TempPath: string;
AErrorMessage: AnsiString;
ResultCode: Integer;
begin                                                              
begin
TempPath := ExpandConstant('{tmp}\checkHost.err');
Parameters := '-noexit -ExecutionPolicy bypass -File     "D:\Test\checkHost.ps1" ' + '-errorFile "' + TempPath + '"';
Result :=
Exec('powershell.exe', Parameters, ExpandConstant('{tmp}'), SW_SHOW,   ewWaitUntilTerminated, ResultCode) and
(ResultCode = 0);
if Result then
begin
if LoadStringFromFile(TempPath, AErrorMessage) then
begin
  ErrorMessage := AErrorMessage;
end;
end;
DeleteFile(TempPath);
end;

var
ModelPage: TInputOptionWizardPage;
models: Variant;
model_info: String;
i: Integer;
procedure InitializeWizard;
begin
ModelPage := CreateInputOptionPage(wpWelcome,
'Model Information', '',
'Please specify the model you would like to install, then click Next.',
 True, False);

 for i:= 0 to 2  do
 begin 
  model_info := 'model' + IntToStr(i);
  model_info := model_info + ' ( HDD ' + '10 GB';
  model_info := model_info + ' SSD ' + '2 GB' + ' )';
  ModelPage.Add(model_info);      
 end;

  ModelPage.SelectedValueIndex := 0;
  end;
  var

  model: String;
  ErrorMessage: string;

  function NextButtonClick(CurPageID: Integer): Boolean;
   begin

   Result := True;

   if CurPageID = ModelPage.ID then
    begin
     if CheckHost(ErrorMessage) <> true then
      begin
      MsgBox(ErrorMessage, mbError, MB_OK);
      end;              
    end;

    end;

但似乎LoadStringFromFile()不起作用导致在msgbox()中打印空值。