NSIS脚本将PS1换行到EXE并以管理员身份运行

时间:2015-12-29 23:23:10

标签: powershell nsis registrykey runas

我有以下NSIS(.nsi)脚本将PowerShell脚本包装到exe。

此外,我希望exe以管理员身份运行,因为脚本需要更新注册表项。

NSIS脚本是:

!include x64.nsh

RequestExecutionLevel admin ;Require admin rights on NT6+ (When UAC is turned on)
OutFile "file.exe"
SilentInstall silent

Section
    SetOutPath $EXEDIR
    File "file.ps1"
    # Run the script to update
    ExecWait "powershell -ExecutionPolicy Bypass -WindowStyle Hidden -File .\file.ps1"
SectionEnd

Function .onInstSuccess
    Delete "file.ps1"
FunctionEnd

PowerShell脚本是:

$registryPath = "HKLM:\SOFTWARE\Test"
$Name = "keyName"
$value = "keyValue"
$preRegVer = (Get-ItemProperty $registryPath).Version
#log "Pre registry value: $preRegVer"   
If(!(Test-Path $registryPath))
{
    # log "Path does not exist"
    New-Item -Path $registryPath -Force | Out-Null
    # log "Path created"
    New-ItemProperty -Path $registryPath -Name $name -Value $value -PropertyType String -Force | Out-Null
    # log "Value created"
}
Else {
    # log "Path exist"
    $val = Get-ItemProperty -Path $registryPath
    if($val.Version -eq $null)
    {
        # log "Value does not exist"
        New-ItemProperty -Path $registryPath -Name $name -Value $value -PropertyType String -Force | Out-Null
        # log "Value created"
    }
    Else {
        # log "Value exist"
        Remove-ItemProperty -path $registryPath -Name Version -Force
        # log "Value removed"
        New-ItemProperty -Path $registryPath -Name $name -Value $value -PropertyType String -Force | Out-Null
        # log "Value created"
    } 
}

当我运行.exe文件时,它会要求提升权限,但不会更新密钥。

我知道powershell脚本有效,因为我使用PowerGUI将其编译为exe,并更新了密钥。

只有PowerGUI的问题在于它没有以管理员身份运行的选项。

3 个答案:

答案 0 :(得分:1)

我怀疑你是在64位计算机上运行而且与位数有冲突。

没有试过这个,但试试这个,看看是否有用。

${If} ${RunningX64}
    ${DisableX64FSRedirection}
${EndIf}

ExecWait "powershell -ExecutionPolicy Bypass -WindowStyle Hidden -File .\file.ps1"


${If} ${RunningX64}
   ${EnableX64FSRedirection}
${EndIf}

答案 1 :(得分:0)

您可以在NSIS中执行相同的任务,而不是使用power shell脚本。您可以修改并使用nsis创建自己的新注册表项。 例如,您可以使用以下命令来编写和读取注册表

/sdcard/

此处a link

答案 2 :(得分:0)

我们构建过程的最终结果是调用NSIS为我们正在构建的产品创建可执行文件。我们也试图调用类似于上面列出的示例的Powershell来在部署时运行,调用powershell脚本来管理与IIS相关的服务器上的特殊职责。

在NSIS文件中,我们尝试了以下变体但没有成功:

nsExec::ExecToStack 'powershell -inputformat none -ExecutionPolicy bypass -File "C:\HardCodedLocation_Instance.ps1"  '

nsExec::ExecToStack 'powershell.exe "& "C:\HardCodedLocation_Instance.ps1"' $0

ExecWait  'powershell -ExecutionPolicy Bypass -WindowStyle Hidden -File C:\HardCodedLocation_Instance.ps1' $0

${PowerShellExec} "C:\HardCodedLocation_Instance.ps1"

“C:\ HardCodedLocation_Instance.ps1”在部署时由NSIS运行,但脚本中需要管理权限的任务尚未完成。

“C:\ HardCodedLocation_Instance.ps1”中的前两行:

Set-ExecutionPolicy -ExecutionPolicy Bypass
Import-Module webadministration

我正在以管理员身份运行以服务器身份登录的可执行文件。然后我可以转身,右键单击“C:\ HardCodedLocation_Instance.ps1”和“使用PowerShell运行”,它可以正常工作。

相关问题