locked
How to use a GUI built in Power Shell to edit variables from another Power Shell script? RRS feed

  • Question

  • I'm relatively new at Power Shell, and I need to know how I can edit variables on one Power Shell script to another. Here are my two projects so far-                                   

                                                                       --GUI INTERFACE--

    [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing") 
    [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") 

    $objForm = New-Object System.Windows.Forms.Form 
    $objForm.Text = "Email Recipient Form"
    $objForm.Size = New-Object System.Drawing.Size(300,200) 
    $objForm.StartPosition = "CenterScreen"

    $objForm.KeyPreview = $True
    $objForm.Add_KeyDown({if ($_.KeyCode -eq "Enter") 
        {$x=$objTextBox.Text;$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(75,120)
    $OKButton.Size = New-Object System.Drawing.Size(75,23)
    $OKButton.Text = "OK"
    $OKButton.Add_Click({$x=$objTextBox.Text;$objForm.Close()})
    $objForm.Controls.Add($OKButton)

    $CancelButton = New-Object System.Windows.Forms.Button
    $CancelButton.Location = New-Object System.Drawing.Size(150,120)
    $CancelButton.Size = New-Object System.Drawing.Size(75,23)
    $CancelButton.Text = "Cancel"
    $CancelButton.Add_Click({$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 enter the E-mail Recpient's information:"
    $objForm.Controls.Add($objLabel) 

    $objTextBox = New-Object System.Windows.Forms.TextBox 
    $objTextBox.Location = New-Object System.Drawing.Size(10,40) 
    $objTextBox.Size = New-Object System.Drawing.Size(260,20) 
    $objForm.Controls.Add($objTextBox) 

    $objForm.Topmost = $True

    $objForm.Add_Shown({$objForm.Activate()})
    [void] $objForm.ShowDialog()

    $x

                                                                       --SCRIPT NEEDING TO BE EDITED--

    $MyEmail = "smtp@domain.com"
    $SMTP= "smtp.domain.com"
    $To = "support@domain.com"
    $Subject = "IP Address Change Alert"
    $Body = "IP Address Change detected. Check your ISP."
    $Password = get-content C:\YourPassword.txt | convertto-securestring
    $Creds = new-object -typename System.Management.Automation.PSCredential -argumentlist "smtp@domain.com",$password
    $ip = Invoke-RestMethod http://ipinfo.io/json | Select -exp ip

    Start-Sleep 2

    If ($ip -ne '192.168.1.1') {
    Send-MailMessage -To $to -From $MyEmail -Subject $Subject -Body $Body -SmtpServer $SMTP -Credential $Creds -UseSsl -Port 587 -DeliveryNotificationOption OnSuccess
    }

    I need to edit the "$To" on this script through the first script, so that way when a user types in their email in the box created by the first script, it edits the $To variable in the second script. If anyone knows how do this, I would be extremely grateful. Side note- the second script has to be running in the background every fifteen minutes in order to detect IP Address changes. 

    Thank you, Logan

    • Moved by Bill_Stewart Monday, March 12, 2018 8:14 PM Unanswerable drive-by question
    Saturday, January 13, 2018 12:56 AM

All replies

  • What does it mean to "edit" another script?  There is really no such thing in the computer world.  Perhaps you mean something else.

    Note that we don't "edit" variables.  We can "declare" a variable and we can "assign" a variable.

    It sounds like you are asking how to pass values from one script to another.

    start here:

    help about_Scripts

    This will show you how to do what you are trying to do.


    \_(ツ)_/

    Saturday, January 13, 2018 1:06 AM
  • combine the script in one script. Using below single line statement you can find the user name.

    $user = $(whoami).split("\")[1]

    After finding the user name you can extract email address using AD commands.

    $aduser = get-aduser $user -property emailaddress

    $to = $aduser.emailaddress

    Saturday, January 13, 2018 12:23 PM
  • Here is how to use forms in PowerShell.  The code you copied is very old and was not written by a person who had any programming experience with forms and the code is for a very old version of PowerShell.

    function Show-MyForm {
    	Add-Type -AssemblyName System.Windows.Forms
    	
    	$form = New-Object System.Windows.Forms.Form
    	$form.Text = "Email Recipient Form"
    	$form.Size = '300, 200'
    	$form.StartPosition = 'CenterScreen'
    	$form.Topmost = $True
    	$form.Add_Shown({
    			$form.Activate()
    		})
    	
    	$buttonOK = New-Object System.Windows.Forms.Button
    	$form.Controls.Add($buttonOK)
    	$buttonOK.Location = '75, 120'
    	$buttonOK.Size = '75, 23'
    	$buttonOK.Text = 'OK'
    	$buttonOK.DialogResult = 'OK'
    	$form.AcceptButton = $buttonOK
    	
    	$buttonCancel = New-Object System.Windows.Forms.Button
    	$form.Controls.Add($buttonCAncel)
    	$buttonCancel.Location = '150, 120'
    	$buttonCancel.Size = '75, 23'
    	$buttonCancel.Text = 'Cancel'
    	$buttonCancel.DialogResult = 'Cancel'
    	$form.CancelButton = $buttonCancel
    	
    	$label = New-Object System.Windows.Forms.Label
    	$form.Controls.Add($label)
    	$label.Location = '10, 20'
    	$label.Size = '280, 20'
    	$label.Text = 'Please enter the E-mail Recpient''s information:'
    	
    	$textBox = New-Object System.Windows.Forms.TextBox
    	$form.Controls.Add($TextBox)
    	$textBox.Location = '10, 40'
    	$textBox.Size = '260, 20'
    	
    	if ('Ok' -eq $form.ShowDialog()) {
    		Write-Host $textBox.Text
    		$mailprops = @{
    			To         = $textBox.Text
    			From	   = 'somebody@somewhere.com'
    			Subject	   = 'IP Address Change Alert'
    			Body	   = 'IP Address Change detected. Check your ISP.'
    			SmtpServer = 'smtp@domain.com'
    			UseSSL	   = $true
    			Port          = 587
    			Credential  = Get-Credential $textBox.Text
    		}
    		Send-MailMessage @mailprops
    	} else {
    		Write-Host 'Cancelled' -ForegroundColor Red
    	}
    }
    $ipinfo = Invoke-RestMethod http://ipinfo.io/json
    If ($ipinfo.ip -ne '192.168.1.1') {
    	Show-MyForm
    } else {
    	Write-Host 'Wrong IP' -fore red	
    }

    The following does the same form with two lines.  Try it.

    add-type -AssemblyName Microsoft.VisualBasic
    $mailto = [Microsoft.VisualBasic.Interaction]::InputBox('Please enter the E-mail Recpient's information:')


    \_(ツ)_/






    • Edited by jrv Sunday, January 14, 2018 5:57 PM
    Sunday, January 14, 2018 5:52 PM
  • Thank you for this, however is there any way I can have the portion of code that detects changes in the IP running in the background, so it can detect IP changes without having to enter credentials or open the text box every time the script runs? Sorry if it sounds stupid or obvious, but I am new at Power Shell.

    Thank you again, Logan

    Tuesday, January 16, 2018 4:01 AM
  • Running in the background, meaning adding it to task scheduler so it can auto-run every 15 minutes or so.
    Tuesday, January 16, 2018 4:07 AM
  • Running in the background, meaning adding it to task scheduler so it can auto-run every 15 minutes or so.

    You can't run a form in a task.


    \_(ツ)_/

    Tuesday, January 16, 2018 4:22 AM
  • Thank you for this, however is there any way I can have the portion of code that detects changes in the IP running in the background, so it can detect IP changes without having to enter credentials or open the text box every time the script runs? Sorry if it sounds stupid or obvious, but I am new at Power Shell.

    Thank you again, Logan

    I think you would do best by learning PowerShell and a bit about programming Windows.

    Learn PowerShell  

    Help Start-Job -Online

    https://www.sapien.com/blog/2012/05/16/powershell-studio-creating-responsive-forms/


    \_(ツ)_/

    Tuesday, January 16, 2018 4:24 AM
  • But if I kept the two scripts separate, would I be able to change the value of one from the output of the other?

    Logan

    Tuesday, January 16, 2018 9:43 PM
  • Why not. It all depends on how you write the script. External functions called from a form should not reference any controls. All values should be passed into the function.


    \_(ツ)_/

    Tuesday, January 16, 2018 9:46 PM