繁体   English   中英

Powershell WPF可以完美地从ISE中运行,但是在以编译的EXE运行时则不行

[英]Powershell WPF works perfectly from ISE, but not when run as compiled EXE

我在这里有一个小问题,我正在努力弄清楚如何解决:)

我的«脚本»/应用程序应该替换配置文件中特定行的一些文本(PATH + $ TESTalias)我已经创建了一个wpf-form,其中包含一个列表框(显示一些名称)一个OK和一个取消按钮。

我已经为列表框中显示的名称创建了一个包含«别名»的哈希表。 并且使得当单击okay按钮时,列表框中所选项目的别名将存储到变量中,然后该变量将用于“path + $ TESTalias”中。

当通过ISE在本地推出时,一切都很完美。

但是当我将«application»编译成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}}

脚本一直很好用。 问题是使用我用来编译的任何工具都没有正确设置控制台模式。

在从“Scepticalist”轻推之后,我使用-noconsole开关重新尝试了PS2EXE,这一切都完美无缺。

谢谢!

稍微讨厌这个问题就像那个一样简单,但很高兴它被排序:)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM