0

I have a functions that only works on some scensarios.

  • It works on Powershell ISE, but when I save the same thing in a .Ps1 file and run it, it doest not work.
  • I have the function as part of a big script. It works and open the Window when I run it on Windows 7 but doest not run on Windows Server 2008 R2.

Why?

The script with the function and its calling is:

Function Get-SaveFileTxt($initialDirectory) 
{ 
[System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms") | 
Out-Null 

$SaveFileDialog = New-Object System.Windows.Forms.SaveFileDialog 
$SaveFileDialog.initialDirectory = $initialDirectory 
$SaveFileDialog.AddExtension = $true 
$SaveFileDialog.DefaultExt = "txt" 
$SaveFileDialog.filter = "Text Files (*.txt)| *.txt" 
$SaveFileDialog.ShowDialog() | Out-Null 
$SaveFileDialog.filename 
} 

Get-SaveFileTxt
2
  • "it does not work"? Be more specific on what error you get, please. Commented Aug 8, 2012 at 7:09
  • @tbergstedt Try it. The script remains execution without error, apparently nothing happens. Commented Aug 8, 2012 at 13:20

1 Answer 1

1

Not sure if this is the case here but I remember a bug that the opened dialog doesn't take focus and appear behind other opened windows. Can you confirm?

UPDATE:

Set the ShowHelp property to $true.

$SaveFileDialog.ShowHelp = $true

It works in the ISE because ISE's apartment state is STA by default and your powershell mode is MTA, you can check it with:

[System.Threading.Thread]::CurrentThread.ApartmentState

Your code will work if you open powershell in STA mode:

powershell.exe -STA
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you @Shay. It does not look to open an reamain behind, looks like nothing happens.
interesting. What do ShowHelp propertly exactly? It totally changes the interface, With Showhelp in false the form looks like this: dl.dropbox.com/u/300838/false.png with true look like this dl.dropbox.com/u/300838/true.png
It's hard for me to explain, this is something that relates to developers (which I'm not), from what I can tell it is required when you work with GUI.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.