locked
Scripts run in Powershell; don't run in Powershell ISE RRS feed

  • Question

  • I've seen a lot pf posts from people whose scripts run fine in ISE but not in a Powershell session. I've got the exact opposite problem - scripts run just fine in Powershell, but don't in ISE. For a specific example, I have a script that presents a list of available PS scripts, then requests the user to select one to run. No matter which one is selected, ISE ignores the choice and simply offers to run the default script. If I run the same script in Powershell (specifying Version 2 when starting 'powershell.exe), the script runs the way it should. It accepts the user input, then runs the corresponding script that was selected.

    The server is running Windows 2016.

    If it helps:

    $ScriptName = $MyInvocation.Mycommand.name ;
    $smtpServer = "mymailserver" ;
    $Question = " Okay to continue?" ;
    $Title = "  Verify Continue ..." ;
    function Prompt4OK {
       [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") ;
       $Pick = [System.Windows.Forms.MessageBox]::Show($Question , $Title , 4) ;
    return $Pick } ;
    function Prompt4Answer {
       $Answer = $null ;
       $Answer = Read-Host 'Okay to Continue - Answer Y or N' ;
    return $Answer
    } ;
    function Prompt4Option {
       $FileName = "C:\PS\Master.txt"
       Get-ChildItem  C:\PS\ | Where-Object {$_.Extension -eq ".PS1"} | `
         Select-Object BaseName  | Format-Table -HideTableHeaders | out-file $FileName  -Force ;
    #
       $Pick = "NONE" ;
       $Option = $null ;
       [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
       [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing") 
    #
       $objForm = New-Object System.Windows.Forms.Form 
       $objForm.Text = "Select a PowerShell Script"
       $objForm.Size = New-Object System.Drawing.Size(250,400) 
       $objForm.Topmost = $True 
       $objForm.StartPosition = "CenterScreen"
    #
       $objForm.KeyPreview = $True
       $objForm.Add_KeyDown({if ($_.KeyCode -eq "Enter") 
        {$x=$objListBox.SelectedItem;$objForm.Close()}})
       $objForm.Add_KeyDown({if ($_.KeyCode -eq "Escape") 
         {$objForm.Close()}})
    #
       $OKButton = New-Object System.Windows.Forms.Button
       $OKButton.Location = New-Object System.Drawing.Size(25,320)
       $OKButton.Size = New-Object System.Drawing.Size(75,23)
       $OKButton.Text = "OK"
       $OKButton.Add_Click({$Pick=$objListBox.SelectedItem;$objForm.Close()})
       $objForm.Controls.Add($OKButton)
    #
       $CancelButton = New-Object System.Windows.Forms.Button
       $CancelButton.Location = New-Object System.Drawing.Size(100,320)
       $CancelButton.Size = New-Object System.Drawing.Size(75,23)
       $CancelButton.Text = "Cancel"
       $CancelButton.Add_Click({$Pick="CANCEL";$objForm.Close()})
       $objForm.Controls.Add($CancelButton)
    #
       $objLabel = New-Object System.Windows.Forms.Label
       $objLabel.Location = New-Object System.Drawing.Size(10,20) 
       $objLabel.Size = New-Object System.Drawing.Size(280,20) 
       $objLabel.Text = "Please select a PowerShell Script:"
       $objForm.Controls.Add($objLabel) 
    #
       $objListBox = New-Object System.Windows.Forms.ListBox 
       $objListBox.Location = New-Object System.Drawing.Size(10,40) 
       $objListBox.Size = New-Object System.Drawing.Size(180,120) 
       $objListBox.Height = 250
    #
       $Option = Get-Content $FileName ;
       foreach ($line in $Option){
          $Line = $Line.Trim() ;
          IF ($Line.Length -gt 1) { [void] $objListBox.Items.Add("$line" )} ;
         } ;
       $objForm.Controls.Add($objListBox) 
       $objForm.Topmost = $True
       $objForm.Add_Shown({$objForm.Activate()})
       [void] $objForm.ShowDialog()
    # 
       IF ($Pick.length -lt 2)  { $Pick = "NONE" } 
    #  Write-Host "* You Picked: $Pick " ;
       IF ($Pick -eq "NONE") {$Powershell = "ListPowershellScripts" } ELSE {$Powershell = $Pick.Trim() } ;
    return $Powershell } ;
    CLS
    Write-host "* BEGIN * * * * * * * * * * * * * * * * * *  $ScriptName  * " ;
    Write-host "*  " ;
    Write-host "* Please select the Powershell Script to Run. " ;
    Start-Sleep -s 1 ;
    Write-host "*  " ;
    $Powershell = Prompt4Option ;
    IF ($Powershell -eq "CANCEL") {Write-host "* $Powershell * * * * * * * * * * * * * * * * * *  $ScriptName  *" ; Break} ;
    CLS
    Write-host "* BEGIN * * * * * * * * * * * * * * * * * *  $ScriptName  * " ;
    Write-host "*  " ;
    
    $PS1Filename = "C:\PS\"  + $Powershell + ".PS1" ;
    $PS1FileInfo = "C:\PS\"  + $Powershell + ".TXT" ;
    Write-host "* Powershell Script:  $Powershell" ;
    $PS1Num = $PS1File.Length ;
    Write-host "* " ;
    Write-host "* Powershell Length: $PS1Num" ;
    Write-host "* " ;
    Write-host "* * * * * * * * * * * * * * * * * * Powershell $Powershell Info * * * * * * * " ;
    # Write-host "*  " ;
    $PS1File = Get-Content $PS1Filename ;
    Get-Content $PS1Filename -totalcount 10  ;
    Write-host "* # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #  " ;
    # Write-host "* * * * * * * * * * * * * * * * * * Powershell $Powershell Info * * * * * * * " ;
    Write-host "* "
    Write-host "* Do you want to Run the Powershell Script: $Powershell ? ";
    Write-host "* "
    $Question = " Okay to Run Powershell Script: $Powershell ?" ;
    $Answer = Prompt4OK ;
    IF ($Answer -eq "Yes") {
       Write-host "* " ;
       Write-host "* Will Run the Script in a few moments... "
       $env:PSMASTERCALLED = $Powershell + ".ps1" ;
       Start-Sleep -s 2 ;
       $PSScript = $PS1Filename  ;
       Invoke-Expression $PSScript ; }
       ELSE { Write-host "* No Powershell Script will run" -Foregroundcolor Red } ;
    Write-host "* " ;
    Write-host "* All Done "
    Write-host "* "
    Write-host "* * END * * * * * * * * * * * * * * * * * *  $ScriptName  * " ;



    • Edited by fggariepy Thursday, March 12, 2020 3:35 PM
    Tuesday, March 10, 2020 8:25 PM

All replies

  • Please post code correctly.  Edit you original post and fix the code. It is unreadable and cannot be copied correctly as posted.

    How to post code in Technet Forums 


    \_(ツ)_/

    Tuesday, March 10, 2020 9:07 PM
  • I'm going to let you dig, but it's possible it's an MTA vs STA issue.  Sounds like you might have older versions of PowerShell.

    I'm sure there's better references, but it might help you still:

    https://www.sapien.com/blog/2015/03/09/powershell-studio-knowing-when-to-use-sta-mode/

    Tuesday, March 10, 2020 11:18 PM
  • I think this is what you are trying to do. More functionality and way less code.

    Script runs in ISE and STA has nothing to do with the issues.  The issues are just bad code.

    The code you copied was very old from PowerShell V1 . PS has evolved and many old things become issues.

    Also this way of using forms allows double-click to select a list item.

    function Show-MyForm{
        param(
            $Path = $PWD,
            $Extension = 'PS1'
        )
        
        Add-Type -AssemblyName System.Windows.Forms
        
        #
        $include = "*.$Extension"
        $Path = Join-Path $Path *
        if(-Not ($files = Get-ChildItem -Path $Path -Include $include)){
            return
        }
    
        #
        $formPickScript = New-Object System.Windows.Forms.Form
        $formPickScript.Text = "Select a PowerShell Script"
        $formPickScript.Size = '250,400'
        $formPickScript.Topmost = $True
        $formPickScript.Add_Shown({$formPickScript.Activate()})
        $formPickScript.StartPosition = "CenterScreen"
        
        #
        $buttonOK = New-Object System.Windows.Forms.Button
        $formPickScript.Controls.Add($buttonOK)
        $buttonOK.Location = '25,320'
        $buttonOK.Size = '75,23'
        $buttonOK.Text = 'OK'
        $buttonOK.DialogResult = 'OK'
        $formPickScript.AcceptButton = $buttonOK
    
        #
        $buttonCancel = New-Object System.Windows.Forms.Button
        $formPickScript.Controls.Add($buttonCancel)
        $buttonCancel.Location = '100,320'
        $buttonCancel.Size = '75,23'
        $buttonCancel.Text = 'Cancel'
        $buttonCancel.DialogResult = 'Cancel'
        $formPickScript.CancelButton = $buttonCancel
        
        #
        $labelListbox = New-Object System.Windows.Forms.Label
        $labelListbox.Location = '10,20'
        $labelListbox.Size = '280,20'
        $labelListbox.Text = "Please select a PowerShell Script:"
        $formPickScript.Controls.Add($labelListbox)
    
        #
        $listboxFiles = New-Object System.Windows.Forms.ListBox
        $formPickScript.Controls.Add($listboxFiles)
        $listboxFiles.Location = '10,40'
        $listboxFiles.Size = '180,250'
        $listboxFiles.Items.AddRange($files)
        $listboxFiles.DataSource = [System.Collections.ArrayList]$files
        $listboxFiles.DisplayMember = 'Basename'
        $listboxFiles.add_MouseDoubleClick({
                $formPickScript.DialogResult = 'OK'
                $listboxFiles.FindForm().Close()
        })
    
        #
        if('Ok' -eq $formPickScript.ShowDialog()){
            $listboxFiles.SelectedItem.Fullname
        }
    }
    Show-MyForm
    Show-MyForm -Path c:\scripts
    
    # get file and execute
    if($filename = Show-MyForm c:\scripts){
        & $filename
    }else{
        # no file selected
    }


    \_(ツ)_/




    • Edited by jrv Tuesday, March 10, 2020 11:36 PM
    Tuesday, March 10, 2020 11:29 PM
  • I'm going to let you dig, but it's possible it's an MTA vs STA issue.  Sounds like you might have older versions of PowerShell.

    I'm sure there's better references, but it might help you still:

    https://www.sapien.com/blog/2015/03/09/powershell-studio-knowing-when-to-use-sta-mode/

    The issue has nothing to do with PSISE.  PSISE runs as STA by default.  Very few controls require MTA mode so the form is not an issue.  The overall approach is copy and paste with little experience with PowerShell or programming.

    \_(ツ)_/

    Tuesday, March 10, 2020 11:35 PM
  • Here is a nice free site that lets you design forms in a web based form so you can easily get the control layout done easily.  It does a fair job of generating the code.

    https://poshgui.com/


    \_(ツ)_/

    Tuesday, March 10, 2020 11:45 PM
  • Thanks!
    Wednesday, March 11, 2020 3:10 PM
  • Thank you!
    Wednesday, March 11, 2020 3:10 PM
  • Thanks for the reply!

    I put this into ISE, modified the 'C:\scripts' directory to point to 'C:\PS' (where the scripts are located), then ran it. The script menu appeared. After selecting a script (findaddress.ps1), the menu appeared again. I selected the script a second time. It began to run, and prompted me for an email address to search for. When I entered the address and clicked OK, the script responded with 'No Email Address Entered!' - then ended (which is what the original script is doing).

    Thursday, March 12, 2020 2:24 PM
  • What did you put where?  My code does none of that but replaces 99% of you code.  If you changed the code orr added anything then there is no way to know what you are seeing or why.

    To ask for help in a technical forum you must leaarn enough anout technology to ask a clear technical quesiotns.  Just stating that a whole lot of thigs happen is not helpful.

    Please read teh following and rethink your quesiton:


    \_(ツ)_/

    Thursday, March 12, 2020 2:37 PM