我这里有一个小问题,我正在努力寻找解决方法:)
我的《脚本》 /应用程序应该替换配置文件(PATH + $ TESTalias)中特定行上的某些文本 我制作了一个wpf表单,其中包含一个列表框(显示一些名称),确定和取消按钮。
我为列表框中显示的名称制作了一个包含“别名”的哈希表。 这样,当单击“确定”按钮时,会将列表框中所选项目的别名存储到变量中,该变量随后将在“ path + $ TESTalias”中使用
通过ISE在本地启动时,一切都完美运行。
但是,当我将“应用程序”编译为EXE文件(通过ISESteroids或PS2EXE)时,仅路径的第一部分被更改,而$ TESTalias变量则未更改。
我也尝试过将其声明为全局脚本变量,但结果相同。
无法在此处发布实际的姓名/位置,但现在发布了略有修改的脚本,以不泄露任何敏感信息。
让我感到困惑的是,它在从ISE运行时完美运行,但在exe编译后却无法运行
# Find currently logged on user
$Loggedon = Get-WmiObject -ComputerName $env:COMPUTERNAME -Class Win32_Computersystem | Select-Object UserName
# Split domain and username.
$Domain,$User = $Loggedon.Username.split('\',2)
# NOTE: When you use a SPECIFIC catch block, exceptions thrown by -ErrorAction Stop MAY LACK
# some InvocationInfo details such as ScriptLineNumber.
# REMEDY: If that affects you, remove the SPECIFIC exception type [System.Management.Automation.RuntimeException] in the code below
# and use ONE generic catch block instead. Such a catch block then handles ALL error types, so you would need to
# add the logic to handle different error types differently by yourself.
# catch [System.Management.Automation.RuntimeException]
# NOTE: When you use a SPECIFIC catch block, exceptions thrown by -ErrorAction Stop MAY LACK
# some InvocationInfo details such as ScriptLineNumber.
# REMEDY: If that affects you, remove the SPECIFIC exception type [System.Management.Automation.CommandNotFoundException] in the code below
# and use ONE generic catch block instead. Such a catch block then handles ALL error types, so you would need to
# add the logic to handle different error types differently by yourself.
{
# get error record
[Management.Automation.ErrorRecord]$e = $_
# retrieve information about runtime error
$info = [PSCustomObject]@{
Exception = $e.Exception.Message
Reason = $e.CategoryInfo.Reason
Target = $e.CategoryInfo.TargetName
Script = $e.InvocationInfo.ScriptName
Line = $e.InvocationInfo.ScriptLineNumber
Column = $e.InvocationInfo.OffsetInLine
}
# output information. Post-process collected info, and log info (optional)
$info
}
{
# get error record
[Management.Automation.ErrorRecord]$e = $_
# retrieve information about runtime error
$info = [PSCustomObject]@{
Exception = $e.Exception.Message
Reason = $e.CategoryInfo.Reason
Target = $e.CategoryInfo.TargetName
Script = $e.InvocationInfo.ScriptName
Line = $e.InvocationInfo.ScriptLineNumber
Column = $e.InvocationInfo.OffsetInLine
}
# output information. Post-process collected info, and log info (optional)
$info#>
}
# Oppretter WindowsForm / GUI for applikasjonen.
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
# Heading
$form = New-Object -TypeName System.Windows.Forms.Form
$form.Text = 'Test-Heading'
$form.Size = New-Object -TypeName System.Drawing.Size -ArgumentList (400, 600)
$form.StartPosition = 'CenterScreen'
#OK-knapp
$OKButton = New-Object -TypeName System.Windows.Forms.Button
$OKButton.Location = New-Object -TypeName System.Drawing.Point -ArgumentList (170, 485)
$OKButton.Size = New-Object -TypeName System.Drawing.Size -ArgumentList (100, 50)
$OKButton.Text = 'OK'
$OKButton.DialogResult = [Windows.Forms.DialogResult]::OK
$form.AcceptButton = $OKButton
$form.Controls.Add($OKButton)
#Avbryt-knapp
$CancelButton = New-Object -TypeName System.Windows.Forms.Button
$CancelButton.Location = New-Object -TypeName System.Drawing.Point -ArgumentList (270, 485)
$CancelButton.Size = New-Object -TypeName System.Drawing.Size -ArgumentList (100, 50)
$CancelButton.Text = 'Avbryt'
$CancelButton.DialogResult = [Windows.Forms.DialogResult]::Cancel
$form.CancelButton = $CancelButton
$form.Controls.Add($CancelButton)
# Tekst-label
$label = New-Object -TypeName System.Windows.Forms.Label
$label.Location = New-Object -TypeName System.Drawing.Point -ArgumentList (10, 20)
$label.Size = New-Object -TypeName System.Drawing.Size -ArgumentList (280, 20)
$label.Text = 'Test-Label'
$form.Controls.Add($label)
# Liste-boks
$listBox = New-Object -TypeName System.Windows.Forms.ListBox
$listBox.Location = New-Object -TypeName System.Drawing.Point -ArgumentList (10, 40)
$listBox.Size = New-Object -TypeName System.Drawing.Size -ArgumentList (360, 20)
$listBox.Height = 400
# Hashtable made to be able to show one text in the listbox, but return a different value.
$TESThash = @{
'TEST1' = '1'
'TEST2' = '2'
'TEST3' = '3'
}
# Listbox-objects, one listbox-item pr. line.
$null = $listBox.Items.Add('TEST1')
$null = $listBox.Items.Add('TEST2')
$null = $listBox.Items.Add('TEST3')
$form.Controls.Add($listBox)
$form.Topmost = $true
$result = $form.ShowDialog()
if ($result -eq [Windows.Forms.DialogResult]::OK)
{
$TESTalias = $TESThash[$listBox.SelectedItem]
# File to change
$file = "C:\Users\$User\ist.ini"
# Get file content and store it into $content variable
$content = Get-Content -Path $file
# Endrer tekst på linje 33 og linje 53
$content[32] = 'PICTUREPATH=K:\Opplaering\Bilete\'+"$TESTalias"
$content[52] = 'PICTUREPATH=K:\Opplaering\Bilete\'+"$TESTalias"
# Set the new content
$content | Set-Content -Path $file
if(!(Test-Path -Path "HKLM:\SOFTWARE\Microsoft\Windows\Currentversion\Uninstall\ExtensBildeFix")){
New-Item -Path "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\ExtensBildeFix" -Value "1"}
$form.Controls|%{$_.Text}}
答案 0 :(得分:0)
脚本一直运行良好。问题是使用我用来编译的两种工具都无法正确设置控制台模式。
从“ Scepticalist”轻推后,我使用-noconsole开关重试了PS2EXE,并且一切正常。
谢谢!
有点烦人的是,问题竟然如此简单,但很高兴得到解决:)