Link to home
Start Free TrialLog in
Avatar of jay_tullis
jay_tullisFlag for United States of America

asked on

Powershell Script Function Return

Hello everyone,

I'm trying to write an "Office 365 Employee Photo Uploader" for a resource group, and am running into a few issues....

First, I'm trying to display the photo the person chose, so they acknowledge they are uploading the correct employee photo.

I've written a simple script to test just this portion of my Office 365 Photo Uploader script, and what I'm trying to do is to present a dialog (a form) that has the background set as the picture they chose and two buttons (a "Yes, this is right" and a "No, this is not correct" button). I want the form to close (or unload?) when the user clicks one of the buttons and the function to return a value I set back to the PowerShell script.

Here's what I've got:
Function Get-FileName($initialDirectory)
{   
 [System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms") | Out-Null
 $OpenFileDialog = New-Object System.Windows.Forms.OpenFileDialog
 $OpenFileDialog.initialDirectory = $initialDirectory
 $OpenFileDialog.filter = "Uploadable Picture Files (*.jpg, *.gif, *.png)| *.jpg; *.gif; *.png"
 $OpenFileDialog.ShowHelp = $true # Without this line, the dialog won't load - found info on StackOverflow
 $OpenFileDialog.ShowDialog() | Out-Null
 $OpenFileDialog.filename
} #end function Get-FileName



$userphoto = Get-FileName -initialDirectory Previous


Function Preview-Photo($PhotoPath) {
	$PhotoReturnYes = "YES"
	$PhotoReturnNo = "NO"
	Add-Type -AssemblyName System.Windows.Forms
	$PhotoCheckForm = New-Object System.Windows.Forms.Form
	$PhotoCheckForm.Text = "Is this the right picture?"

	$Image = [System.Drawing.Image]::FromFile($PhotoPath)
	$PhotoCheckForm.BackgroundImage = $Image
	$PhotoCheckForm.BackgroundImageLayout = "Stretch"
	$PhotoCheckForm.Width = 800
	$PhotoCheckForm.Height = 800
	$PhotoCheckForm.StartPosition = [System.Windows.Forms.FormStartPosition]::CenterScreen

	$PhotoCheckFormFont = New-Object System.Drawing.Font("Times New Roman", 30)
	$PhotoCheckFormButtonFont = New-Object System.Drawing.Font("Segoe UI", 9)
	$PhotoCheckForm.Font = $PhotoCheckFormFont

	$PhotoCheckFormLabel = New-Object System.Windows.Forms.Label
	$PhotoCheckFormLabel.Text = "Is this the photo you meant to select (It will not show correctly in this window because it's stretched to fit, just check to see it's the right person)?"
	$PhotoCheckFormLabel.BackColor = "Transparent"
	$PhotoCheckFormLabel.ForeColor = "DarkGray"
	$PhotoCheckFormLabel.Width = 800
	$PhotoCheckFormLabel.Height = 400
	
	$PhotoCheckFormYesButton = New-Object System.Windows.Forms.Button
	$PhotoCheckFormYesButton.Location = New-Object System.Drawing.Size(10,700)
	$PhotoCheckFormYesButton.Size = New-Object System.Drawing.Size(100,60)
	$PhotoCheckFormYesButton.Font = $PhotoCheckFormButtonFont
	$PhotoCheckFormYesButton.Text = "This Photo is Correct"
	$PhotoCheckFormYesButton.Add_Click({Return $PhotoReturnYes})
	
	$PhotoCheckFormNoButton = New-Object System.Windows.Forms.Button
	$PhotoCheckFormNoButton.Location = New-Object System.Drawing.Size(670,700)
	$PhotoCheckFormNoButton.Size = New-Object System.Drawing.Size(100,60)
	$PhotoCheckFormNoButton.Font = $PhotoCheckFormButtonFont
	$PhotoCheckFormNoButton.Text = "No, Not Correct!"
	$PhotoCheckFormNoButton.Add_Click({Return $PhotoReturnNo})
	
	$PhotoCheckForm.Controls.Add($PhotoCheckFormNoButton)	
	$PhotoCheckForm.Controls.Add($PhotoCheckFormYesButton)
	$PhotoCheckForm.Controls.Add($PhotoCheckFormLabel)
	$PhotoCheckForm.ShowDialog()
}

$PhotoCheckFormResponse = Preview-Photo $userphoto
Write-Host $PhotoCheckFormResponse

Open in new window


This should work - right? What am I doing wrong?

Once we get this figured out, I'll post back with a question regarding proper closing/unloading of forms (but in a way that I can "loop" the script) without causing exceptions in Powershell.exe...

Thanks!
-Duane
ASKER CERTIFIED SOLUTION
Avatar of jay_tullis
jay_tullis
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial