Ending a script after calling another script

by cre8tivspirit at 2013-03-15 09:01:19

I have a GUI script that calls a separate installation script. I haven’t been able to get the GUI form to close till after the other script has completed running even though I no longer need the GUI.

What I want to happen is, once the "OK" button is clicked and the other script called, the GUI form will close and only the installation script to run.

Here is the code for the action once the button is clicked;

$button_OK_OnClick= {
if ($checkBox1.Checked) {
$chkBox1 = $true
}else {
if ($checkBox2.Checked) {
$ChkBox2 = $true
}
if ($checkBox4.Checked) {
$ChkBox4 = $true
}
}
& "H:\PowerShell_Scripts\DevDskTopTools_Build_74_TEST.ps1" @Chk_Box
$form1.Close()
}
by DonJ at 2013-03-15 09:34:30
You would need to get the script running on a different thread. I expect the easiest way to do that would be Start-Process, starting a new PowerShell.exe instance and having it run the specified file.

(FYI, you can use the CODE button to format your code here, so it looks a bit more readable)
by cre8tivspirit at 2013-03-15 10:24:00
Okay, that works except that I’m passing a hash table as a parameter and the start-process call is having an issue with that. I’m sure it’s something simple but being fairly new to Powershell presents me with issues from time to time.

Start-Process powershell 'h:\PowerShell_Scripts\DevDskTopTools_Build_74_TEST.ps1' @Chk_Box

I am getting the following error message;

Start-Process : A parameter cannot be found that matches parameter name ‘key13’
.
At H:\PowerShell_Scripts\DeveloperToolSelection.ps1:93 char:16
+ Start-Process <<<< powershell ‘h:\PowerShell_Scripts\DevDskTopTools_
Build_74_TEST.ps1’ @Chk_Box
+ CategoryInfo : InvalidArgument: (:slight_smile: [Start-Process], ParameterB
indingException
+ FullyQualifiedErrorId : NamedParameterNotFound,Microsoft.PowerShell.Comm
ands.StartProcessCommand
by DonJ at 2013-03-15 10:37:21
Well, no, you’re not passing a hashtable, you’re splatting it. Your keys are being taken as parameter names. If you just need to pass a value, you use $ in front of the variable, not @. In other words, you PREVIOUSLY would splat this to your script, which DID accept those keys as parameters. Now, you’re splatting to Start-Process, which has no idea what to do with them. You’ll probably have to devise a different way of passing the data since you’re starting a whole new process. Maybe convert to an XML string, and then have your script accept that and convert it from XML back into an object. Or whatever.
by cre8tivspirit at 2013-03-15 11:01:17
Well that’s not good. There are 30 keys in that hash table that need to be passed to the called script. Okay, I guess I’m going to get a lesson in converting my hash table to XML.
by DonJ at 2013-03-15 11:24:15
ConvertTo-XML :slight_smile:
by cre8tivspirit at 2013-03-15 12:07:02
Yeah, I’m trying that but there is a problem with the hash table.

$chkBox1 = $false
$chkBox2 = $true
$chkBox3 = $false
$chkBox4 = $false

$Chk_Box = @{key1 = $chkBox1;
key2 = $chkBox2;
key3 = $chkBox3;
key4 = $chkBox4
}

(Get-ChildItem $Chk_Box | ConvertTo-XML -NoTypeInformation).Save("D]

Results in the following error;

Get-ChildItem : Cannot find path ‘C:\Windows\system32\System.Collections.Hashta
ble’ because it does not exist.
At H:\PowerShell_Scripts\work.ps1:12 char:15
+ (Get-ChildItem <<<< $Chk_Box | ConvertTo-XML -NoTypeInformation).Save("D:\Sc
ripts\Test.xml")
+ CategoryInfo : ObjectNotFound: (C:\Windows\syst…tions.Hashtab
le:String) [Get-ChildItem], ItemNotFoundException
+ FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetCh
ildItemCommand
by DonJ at 2013-03-15 12:13:29
Wait… maybe I’m not grasping what you want to do.

If I have a hashtable in $h, and I want to convert it to XML, I run $h | ConvertTo-XML. I’m not following where Get-ChildItem snuck in, which makes me wonder if I’m not actually answering your question :).

If the goal is to persist the XML to a file - a great idea, BTW - $h | Export-CliXML filename.xml does it. You can then read in that file using Import-CliXML, which is super E-Z.

Get-ChildItem is, basically, the Dir command… so wondering if I missed a step in what you want to do…?
by cre8tivspirit at 2013-03-18 06:47:04
No, you got it right and I’m the one who messed it up, sorry for the confusion. I’m still trying to learn all the ways of coding in Powershell.
by cre8tivspirit at 2013-03-18 09:04:25
Not sure what I’m doing wrong here. It doesn’t look like the entire xml file is is being received in order to convert back to a hash table.

$chkBox1 = $false
$chkBox2 = $true
$chkBox3 = $false
$chkBox4 = $false

$Chk_Box = @{key1 = $chkBox1;
key2 = $chkBox2;
key3 = $chkBox3;
key4 = $chkBox4
}

($Chk_Box | ConvertTo-XML -NoTypeInformation).Save("D:\Scripts\Test.xml")
$TestXML = [xml] (Get-Content -Path "D:\Scripts\Test.xml")
$TestXML.hash.entry | % { $Chk_Box1 = @{} } { $Chk_Box1 += @{$.key = $.value} }
$Chk_Box1
by ArtB0514 at 2013-03-18 09:38:58
If I understand correctly and you are trying to save a hash table to an XML file and then read it back in restored to a hash table, then the easiest way is to use Export-Clixml and Import-Clixml.

$chkBox1 = $false
$chkBox2 = $true
$chkBox3 = $false
$chkBox4 = $false
$Chk_Box = @{key1 = $chkBox1;key2 = $chkBox2;key3 = $chkBox3;key4 = $chkBox4}
$Chk_Box | Export-Clixml "D:\Scripts\Test.xml"
$TestXML = Import-Clixml "D:\Scripts\Test.xml"
$Chk_Box1 = @{}
$TestXML.GetEnumerator() | foreach {$Chk_Box1.Add($.Key,$.Value)}
$Chk_Box1


By the way, you are creating a new empty $Chk_Box1 in the foreach clause. That means that when the loop completes, $Chk_Box1 will have only the last Key/Value pair.
by mjolinor at 2013-03-18 11:07:26
Does the script you’re using (h:\PowerShell_Scripts\DevDskTopTools_Build_74_TEST.ps1) use named parameters, or are they all positional?
by cre8tivspirit at 2013-03-18 11:22:15
Originally it was another hash table that received the passed one but my GUI form wouldn’t close till the called program completed. The 30 different parameters all represent the checked/unchecked status of the software selected in the calling script (GUI form). These correspond to "if" statements that check the Boolean value and install or ignore the corresponding piece of software. The goal is to check the software to install, click "OK" and pass the values to the installing script while closing the GUI form.