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
LVL 1
jay_tullisAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

jay_tullisAuthor Commented:
Figured it out:
	$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"
[b]	$PhotoCheckFormYesButton.DialogResult = [System.Windows.Forms.DialogResult]::YES
	$PhotoCheckForm.AcceptButton = $PhotoCheckFormYesButton[/b]

	$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!"
[b]	$PhotoCheckFormNoButton.DialogResult = [System.Windows.Forms.DialogResult]::NO
	$PhotoCheckForm.CancelButton = $PhotoCheckFormNoButton[/b]

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.