【发布时间】:2017-05-02 11:01:26
【问题描述】:
我遇到了以下问题:
我正在尝试在 NSIS 中使用 powershell 脚本,但是如果我启动安装程序,则会弹出一个 powershell 窗口,但会立即关闭。
我只能看到 powershell 打印了一个错误(一些红色字母),但由于它立即关闭,我看不到错误的内容。
我的脚本所做的只是替换模板 .xml 文件中的占位符,方法是替换 "CURRENTPC\CURRENTUSERNAME" 和命令 'whoami' 的输出,最后我将每个 'schtask' 的这个 XML 文件导入到任务计划程序中。
如果我单独运行我的脚本,它会按需要运行,但如果我使用已编译的设置,则会出现我描述的问题。
我当然会花时间寻找解决方案。
我尝试了以下方法:
我在我的脚本中构建了一个检查,以查看它是否以 32 位运行。如果是,请在 64 位中重新启动
我为我的 .xml 模板所在的路径做了一个硬编码的“cd”
我通过“ExecWait”、“ns::ExecToLog”和“ns::ExecToStack”在 NSIS 中执行了 powershell
我不知道该怎么办了。希望有人能帮忙。
对于那些想阅读代码的人:
这就是我在 NSIS 中使用脚本的方式:
File ProcessControlTemplateFill.ps1
ExecWait 'powershell -inputformat none -ExecutionPolicy RemoteSigned -File ".\ProcessControlTemplateFill.ps1"'
这是我的 .ps1 脚本:
#############################################################################
#If Powershell is running the 32-bit version on a 64-bit machine, we
#need to force powershell to run in 64-bit mode .
#############################################################################
if ($env:PROCESSOR_ARCHITEW6432 -eq "AMD64") {
write-warning "Y'arg Matey, we're off to 64-bit land....."
if ($myInvocation.Line) {
&"$env:WINDIR\sysnative\windowspowershell\v1.0\powershell.exe" -NonInteractive -NoProfile $myInvocation.Line
}else{
&"$env:WINDIR\sysnative\windowspowershell\v1.0\powershell.exe" -NonInteractive -NoProfile -file "$($myInvocation.InvocationName)" $args
}
exit $lastexitcode
}
write-host "Main script body"
#############################################################################
#End
#############################################################################
cd C:\R15
$WERBINICH = whoami
ForEach($Datei in ls './sources/ProcessControlTemplate.xml') {
$Zeilen = Get-Content -Path $Datei.Fullname
$ZeilenZähler = 1
foreach($Zeile in $Zeilen)
{
$Zeile = $Zeile -replace 'CURRENTPC\\CURRENTUSERNAME', "$WERBINICH"
Write-Host $Zeile
If($ZeilenZähler -eq 1 ) {
Out-File -FilePath $Datei.Fullname -InputObject $Zeile -Force
}
Else {
Out-File -FilePath $Datei.Fullname -InputObject $Zeile -Force -Append
}
$ZeilenZähler++
}
}
schtasks /create /f /XML .\sources\ProcessControlTemplate.xml /tn ProcessControl
【问题讨论】:
-
NSIS 安装程序的目的是什么?为什么不直接运行 PowerShell 脚本?
-
@Bill_Stewart 安装程序是主要项目的一部分,未来使用我的设置的“客户”不应该使用设置并随后手动启动脚本
-
对不起,我无法理解你在问什么。
-
@Bill_Stewart 我有一个大程序,我想把它带到其他电脑上。安装程序安装了我的这个程序,但程序必须在用户登录时启动,因此是计划任务。计划任务必须在某个目录中启动。所以在 NSIS 安装程序中正常执行的 schtask 将无法正常工作......所以我想我可以使用脚本将信息修改为我需要的内容并通过 xml 导入它们......我希望这能以更好的方式解释它
-
错误信息是什么? ExecToLog 应该会显示给你。
标签: xml powershell nsis 32-bit windows-task-scheduler