Unexpected behavior

I have this xml interface:

$inputXML = @" <Window x:Class="WpfApplication1.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:WpfApplication1" mc:Ignorable="d" Title="Dustin's Powershell Tool" Height="475" Width="800"> <Grid> <Button x:Name="ButtonA" Content="Lineup Verification" HorizontalAlignment="Left" Height="50" Margin="50,50,0,0" VerticalAlignment="Top" Width="120"/> <Button x:Name="ButtonB" Content="Night Lineup Check" HorizontalAlignment="Left" Height="50" Margin="200,50,0,0" VerticalAlignment="Top" Width="120"/> </Grid> </Window> "@ $inputXML = $inputXML -replace 'mc:Ignorable="d"','' -replace "x:N",'N' -replace '^<Win.*', '<Window' [void][System.Reflection.Assembly]::LoadWithPartialName('presentationframework') [xml]$XAML = $inputXML $reader=(New-Object System.Xml.XmlNodeReader $xaml) try{$Form=[Windows.Markup.XamlReader]::Load( $reader )} catch{Write-Warning "Unable to parse XML, with error: $($Error[0])`n Ensure that there are NO SelectionChanged properties (PowerShell cannot process them)" throw} $xaml.SelectNodes("//*[@Name]") | %{"trying item $($_.Name)"; try {Set-Variable -Name "WPF$($_.Name)" -Value $Form.FindName($_.Name) -ErrorAction Stop} catch{throw} } Function Get-FormVariables{ if ($global:ReadmeDisplay -ne $true){Write-host "If you need to reference this display again, run Get-FormVariables" -ForegroundColor Yellow;$global:ReadmeDisplay=$true} write-host "Found the following interactable elements from our form" -ForegroundColor Cyan get-variable WPF* } Get-FormVariables $script1 = "\Documentstestsscript-toolboxworking.ps1" $script2 = "\Documentstestsscript-toolboxnot-workingps1" $buttona = $Form.FindName("ButtonA") $buttona.Add_Click({&$script1}) $buttona = $Form.FindName("ButtonB") $buttona.Add_Click({&$script2}) $Form.ShowDialog() | out-null 

and this not-working.ps1:

$bay=0 $bayclm = “{0:d4}” -f $bay While($bay-lt97){ $bay++ $bayclm = “{0:d4}” -f $bay $1conn = New-Object System.Data.OleDb.OleDbConnection $1conn.ConnectionString = " Provider=IBMDADB2; DSN=testdsn; Uid=admin; Pwd=pass; DBALIAS=testdsn; DefaultSchema=testschema; " $1QuerySQL = "SELECT * FROM testschema.table1 WHERE (column1 IS NULL) AND (date>='07/10/2018') AND (BAY_NR=$bayclm)" $1ds = new-object "System.Data.DataSet" $1da= new-object System.Data.OleDb.OleDbDataAdapter($1QuerySQL,$1conn) $1conn.Open() $1da.fill($1ds) $1all = $1ds.tables[0].rows | select * -ExcludeProperty RowError, RowState, HasErrors, Name, Table, ItemArray $1conn.Close() $trailer=$1all.number.Trim() $2conn = New-Object System.Data.OleDb.OleDbConnection $2conn.ConnectionString = " Provider=IBMDADB2; DSN=testdsn; Uid=admin; Pwd=pass; DBALIAS=testdsn; DefaultSchema=testschema; " $2QuerySQL = "SELECT * FROM testschema.table2 WHERE (number='$trailer') AND (date>={d 07/10/2018}) ORDER BY timestamp DESC" $2ds = new-object "System.Data.DataSet" $2da= new-object System.Data.OleDb.OleDbDataAdapter($2QuerySQL,$2conn) $2conn.Open() $2da.fill($2ds) $2all = $2ds.tables[0].rows | select * -ExcludeProperty RowError, RowState, HasErrors, Name, Table, ItemArray $2conn.Close() $2all | Add-Member -MemberType NoteProperty "BAY_NR" -Value $1all.BAY_NR if($2all.BAY_NR[0]-eq'0002'){if(($2all.LD_DTN_SAB_NA[0]+$2all.LD_DTN_SRT_TYP_CD[0])-ne'COLOHN'){[System.Windows.Forms.MessageBox]::Show("Please check 0002")}} } 

I then use a shortcut to launch the xml script:

Target: C:WindowsSystem32WindowsPowerShellv1.0powershell.exe -WindowStyle Hidden -file "\Documentstestsdustins-powershell-gui.ps1" 

If I don’t use the shortcut, and launch it either of these ways, it works normally:

– Right-click, Run with PowerShell

– Open with PowerShell ISE and click Run

If I double-click the shortcut.lnk to run this gui, it opens normally, and runs working.ps1 just as it should with the button.

If I try clicking the button for not-working.ps1, it stops for a moment like it’s going to launch, but does nothing.

If not-working.ps1 doesn’t work, I can just click the working.ps1’s button, and then quickly click the not-working.ps1 button, and it will work as desired.

If I replace not-working.ps1 with another working script, this doesn’t happen, so I thought it was possibly a problem with the not-working.ps1, but it also only happens when using the shortcut.lnk to start the GUI…

Does anyone have ideas for why this is happening or steps I can take to resolve this?

submitted by /u/dustinross4
[link] [comments]

Leave a Reply