none
PowerShell works with ISE but not just running it as a PowerShell.exe

    Question

  • Below is a script that I have created (some of the items are cut out but you get a majority of what it is attempting to do.  The script runs great if I am in ISE mode but not if I am trying to run just as a PowerShell.exe. When I run it just with PowerShell it just sits there doing nothing. Do I need to change any variables to make it work?  Elevated mode?

    Any help would be greatly appreciated. Thank you!

    Add-Type -AssemblyName System.Windows.Forms
    Add-Type -AssemblyName PresentationFramework
    Import-Module ActiveDirectory
    
    
        #Build Form
        $Form1 = New-Object System.Windows.Forms.Form
        $Form1.FormBorderStyle = [System.Windows.Forms.FormBorderStyle]::None
        $Form1.Text = "Update using Active Directory"
        $Form1.Size = New-Object System.Drawing.Size(700,500)
        $Form1.StartPosition = "CenterScreen"
        $Form1.Toplevel = "True"
        $Form1.backcolor = "#9D9D9C"
        $Form1.forecolor = "black"
        $Font = New-Object System.Drawing.Font("Times New Roman",18,[System.Drawing.FontStyle]::Italic)
        $Form1.Font = $Font
    
        #Add Picture Box
        $PB1 = New-Object System.Windows.Forms.Picturebox 
        $PB1.Image = [system.drawing.image]::FromFile("Resources/update.jpg")
        $PB1.Autosize = "True"
        $PB1.Location = New-Object System.Drawing.Size(185,30)
        $Form1.Controls.Add($PB1)
    
        $PB2 = New-Object System.Windows.Forms.Picturebox 
        $PB2.Image = [system.drawing.image]::FromFile("Resources/exit.jpg")
        $PB2.Autosize = "True"
        $PB2.Cursor = "hand"
        $PB2.Location = New-Object System.Drawing.Size(650,15)
        $Form1.Controls.Add($PB2)
    
        #Add Label
        $Label = New-Object System.Windows.Forms.Label
        $Label.Text = "You are about to update.  Please select a target location!"
        $Label.Autosize = "True"
        $Label.Location =New-Object System.Drawing.Size (75,85)
        $Form1.Controls.Add($Label)
    
        #Add Buttons
        $North = New-Object System.Windows.Forms.Button
        $North.Location = New-Object System.Drawing.Size(290,175)
        $North.backcolor = "#B71A24"
        $North.Add_MouseHover({$North.backcolor = [System.Drawing.Color]::White})
        $North.Add_MouseLeave({$North.backcolor = "#B71A24"})
        $North.Size = New-Object System.Drawing.Size(120,33)
        $North.Autosize = "True"
        $North.Cursor = "hand"
        $North.Text = "NORTH"
    
        $South = New-Object System.Windows.Forms.Button
        $South.Location = New-Object System.Drawing.Size(290, 275)
        $South.backcolor = "#B71A24"
        $South.Add_MouseHover({$South.backcolor = [System.Drawing.Color]::White})
        $South.Add_MouseLeave({$South.backcolor = "#B71A24"})
        $South.Size = New-Object System.Drawing.Size(120,33)
        $South.Autosize = "True"
        $South.Cursor = "hand"
        $South.Text = "SOUTH"
        
        $East = New-Object System.Windows.Forms.Button
        $East.Location = New-Object System.Drawing.Size(290,375)
        $East.backcolor = "#B71A24"
        $East.Add_MouseHover({$East.backcolor = [System.Drawing.Color]::White})
        $East.Add_MouseLeave({$East.backcolor = "#B71A24"})
        $East.Size = New-Object System.Drawing.Size(120,33)
        $East.Autosize = "True"
        $East.Cursor = "hand"
        $East.Text = "EAST"
    
        $Form1.Controls.Add($North)
        $Form1.Controls.Add($South)
        $Form1.Controls.Add($East)
    
        #Add Button event 
        $North.Add_Click($North_OnClick)
        $South.Add_Click($South_OnClick)
        $East.Add_Click($East_OnClick)
        $PB2.Add_Click({$form1.Close()})
    
    #Show the Form 
    $Form1.ShowDialog()| Out-Null
    
        
    $North_OnClick={
    Import-Module ActiveDirectory
    
        # Search multiple OU's
        $OUs="OU=PCs, OU=PCs,DC=TEST,DC=local"
    
    
    
        #loop through your OUs, then outputs computer name in csv format
        $OUS | Foreach { 
            Get-ADComputer -filter * -Searchbase $_ } |
            select -ExpandProperty Name | Out-File "$env:USERPROFILE\Desktop\UPDATE\Computers\North_Computer_List.txt" -Force
    
        #Computer list from AD, Skips the first 3 lines
        $Computerlist = Get-Content "$env:USERPROFILE\Desktop\UPDATE\Computers\North_Computer_List.txt" | Select-Object -Skip 0
    
        # Remote folder path
        $Destination = "C$\TEST\"
    
        # Local folder path
        $Source = "C:\TEST\*"
    
        # Echo's results from loop
        $Resultlist = @()
    
        # Loops through AD computer list to check for remote path.
        # If path exists, copies local TWIST folder to remote path minus .ini file.
        # If no path, replies failed.
        ForEach ($Computer in $Computerlist){
            $currentEAP = $ErrorActionPreference
            $ErrorActionPreference = "silentlycontinue"
    	    $DestinationPath = "\\$($Computer)\C$\TEST"
    	        if (Test-Path $DestinationPath){
                    (Get-WMIObject -computer "$computer" win32_process -filter "Name= 'notepad.exe'").Invokemethod("terminate", $null)
                    Copy-Item -Recurse $Source -Destination \\$computer\$destination -Exclude *.ini -Force –Verbose
                    $Resultlist += "$computer, Successful"
                }
                Else{
                $ResultList = $ResultList + "$computer, Failed"
    	        }
            }
        # Creates text file of loop.  Success or Fail of copy TWIST folder.
        $Resultlist | Out-File "$env:USERPROFILE\Desktop\UPDATE\Results\North_Results.csv" -Force
    
        Add-Type -AssemblyName System.Windows.Forms
    
        # Create dialogue box for user if they want to see the results
        $result = [System.Windows.Forms.MessageBox]::Show('Update has completed! Would you like to see the Results?', 'Question', 'YesNo', 'Question')
            # check the result:
            if ($result -eq 'Yes')
            {
            notepad "$env:USERPROFILE\Desktop\UPDATE\Results\North_Results.csv"
            }
            else
            {
            [System.Windows.Forms.MessageBox]::Show('Well then.... Have a good Day!')
            }
    }
    

    Thursday, June 14, 2018 8:09 PM

Answers

  • All event code must exist before it is used in a control.  All of your event code is after the point where it is used.

    Also - in PowerShell there is no "True" or "False".  It is $true or $false.

    "False" is always $true.

    PS D:\scripts> [bool]'False'
    True
    PS D:\scripts>

    All non-empty strings are always $true.

    PS D:\scripts> [bool]''
    False
    PS D:\scripts> [bool]'    '
    True


    \_(ツ)_/


    • Edited by jrvModerator Thursday, June 14, 2018 8:37 PM
    • Marked as answer by Josentech Thursday, June 14, 2018 9:18 PM
    Thursday, June 14, 2018 8:37 PM
    Moderator

All replies

  • version of powershell you are trying to run it under?
    Thursday, June 14, 2018 8:20 PM
  • All event code must be declared before it is used in the form.


    \_(ツ)_/

    Thursday, June 14, 2018 8:30 PM
    Moderator
  • Name                           Value
    ----                           -----
    PSVersion                      4.0
    WSManStackVersion              3.0
    SerializationVersion           1.1.0.1
    CLRVersion                     4.0.30319.42000
    BuildVersion                   6.3.9600.18773
    PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0}
    PSRemotingProtocolVersion      2.2
    Thursday, June 14, 2018 8:32 PM
  • All event code must exist before it is used in a control.  All of your event code is after the point where it is used.

    Also - in PowerShell there is no "True" or "False".  It is $true or $false.

    "False" is always $true.

    PS D:\scripts> [bool]'False'
    True
    PS D:\scripts>

    All non-empty strings are always $true.

    PS D:\scripts> [bool]''
    False
    PS D:\scripts> [bool]'    '
    True


    \_(ツ)_/


    • Edited by jrvModerator Thursday, June 14, 2018 8:37 PM
    • Marked as answer by Josentech Thursday, June 14, 2018 9:18 PM
    Thursday, June 14, 2018 8:37 PM
    Moderator
  • So 

     if ($result -eq 'Yes')

    Should be

     if ($result -eq $True)
    Thursday, June 14, 2018 8:46 PM
  • So 

     if ($result -eq 'Yes')

    Should be

     if ($result -eq $True)
    i think that is only part of it; i'm still trying to decode; the chicken / egg -> event code / control
    Thursday, June 14, 2018 8:55 PM
  • unless 

    #Show the Form 
    $Form1.ShowDialog()| Out-Null

    is xxxxxxx you up; because the buttons don't have the info yet? you call it before the button press action.


    • Edited by jrvModerator Thursday, June 14, 2018 9:03 PM language
    Thursday, June 14, 2018 8:57 PM
  • So 

     if ($result -eq 'Yes')

    Should be

     if ($result -eq $True)

    Every time you have sued "True" should be $true.

    if($result){

    test for true.  Never test a Boolean with "-eq".  That is what Booleans are for.  They do not need to be tested for "truthiness".


    \_(ツ)_/

    Thursday, June 14, 2018 9:02 PM
    Moderator
  • unless 

    #Show the Form 
    $Form1.ShowDialog()| Out-Null

    is xxxxxxx you up; because the buttons don't have the info yet? you call it before the button press action.


    Be careful. There are little kids here.


    \_(ツ)_/

    Thursday, June 14, 2018 9:03 PM
    Moderator
  • am i getting closer dad?

    Please refrain from the odd comments.  They are not helpful at this point.  Let the OP get his head straight about the code first.


    \_(ツ)_/

    Thursday, June 14, 2018 9:04 PM
    Moderator
  • lo siento amigo
    Thursday, June 14, 2018 9:12 PM
  • Thanks everyone...

    Just moving the form to the bottom did the trick. Still works with the if ($result -eq 'Yes')

    Thursday, June 14, 2018 9:19 PM
  • Thanks everyone...

    Just moving the form to the bottom did the trick. Still works with the if ($result -eq 'Yes')

    Because any string you compare will always be true.  Don't get into this habit.  It will get you eventually and it is technically completely incorrect.


    \_(ツ)_/

    Thursday, June 14, 2018 9:25 PM
    Moderator
  • Thursday, June 14, 2018 9:27 PM