#########################################################################
# # This script was created by ************************** # #
# # for uploading user profile pictures to Office 365 # #
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# # Created on: 4/17/2015 # #
#########################################################################
$ErrorActionPreference = "Continue"
Function Connect-MSOL-Now() {
Import-Module MSOnline
$Credentials = Get-Credential
$ExSession = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/?proxymethod=rps -Credential $Credentials -Authentication Basic -AllowRedirection
Import-PSSession $ExSession -AllowClobber
# Set-ExecutionPolicy RemoteSigned # Only needed on first run!
Connect-MsolService -Credential $Credentials
}
# The Open-File Dialog
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.Multiselect = $false
$OpenFileDialog.Title= "Select an employee headshot photo..."
$OpenFileDialog.ShowHelp = $true # Without this line, the dialog won't load - found info on StackOverflow
$OpenFileDialogExitStatus = $OpenFileDialog.ShowDialog()
If ($OpenFileDialogExitStatus -eq "OK")
{$OpenFileDialog.filename}
Else
{$OpenFileDialog.Close} #Write-Host -foregroundcolor RED -backgroundcolor WHITE "You pressed ""Cancel"" - an error will be produced, but this is OK";
} #end function Get-FileName
Function UploadPic() {
[array]$DropDownArray = get-msoluser | where-object { $_.isLicensed -eq "TRUE" } | Sort-Object -Property UserPrincipalName | ForEach{ $_.UserPrincipalName }
#This function returns whatever object is selected in the dropdown
##to the command prompt.
function Return-DropDown {
If ($DropDown.SelectedItem -eq $null) {
[System.Windows.Forms.MessageBox]::Show("You must select an employee or cancel!" , "ERROR!")
}
Else {
$script:User = $DropDown.SelectedItem.ToString()
$Form.Close()
}
}
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
$Form = New-Object System.Windows.Forms.Form
$Form.Text = "Office365 Current Accounts"
$Form.Size = New-Object System.Drawing.Size(500,150)
$Form.StartPosition = "CenterScreen"
$Form.KeyPreview = $True
$Form.Add_KeyDown({if ($_.KeyCode -eq "Enter")
{(Return-Dropdown);$Form.Close()}}) #$x=$DropDown.Text
$Form.Add_KeyDown({if ($_.KeyCode -eq "Escape")
{$Form.Close()}})
$SelectEmployee = New-Object System.Windows.Forms.Button
$SelectEmployee.Location = New-Object System.Drawing.Size(300,70)
$SelectEmployee.Size = New-Object System.Drawing.Size(130,23)
$SelectEmployee.Text = "Select the Employee"
$Form.AcceptButton = $SelectEmployee
$SelectEmployee.Add_Click({Return-DropDown})
$Form.Controls.Add($SelectEmployee)
$CancelButton = New-Object System.Windows.Forms.Button
$CancelButton.Location = New-Object System.Drawing.Size(50,70)
$CancelButton.Size = New-Object System.Drawing.Size(130,23)
$CancelButton.Text = "Exit Script"
$CancelButton.Add_Click({DoYouWantToQuit})
$Form.Controls.Add($CancelButton)
$Form.CancelButton = $CancelButton
$DropDown = new-object System.Windows.Forms.ComboBox
$DropDown.Location = new-object System.Drawing.Size(200,10) # Sets the location of the dropdown
$DropDown.Size = new-object System.Drawing.Size(230,30) # Sets the size of the dropdown
ForEach ($Item in $DropDownArray) {
[void] $DropDown.Items.Add($Item)
}
$Form.Controls.Add($DropDown)
$DropDownLabel = new-object System.Windows.Forms.Label
$DropDownLabel.Location = new-object System.Drawing.Size(10,10) # Sets the location of the label
$DropDownLabel.size = new-object System.Drawing.Size(150,50) # Sets the size of the label
$DropDownLabel.Text = "Please select the employee you would like to upload a photo for from the dropdown"
$Form.Controls.Add($DropDownLabel)
$Form.Topmost = $True
$Form.Add_Shown({$Form.Activate()})
[void] $Form.ShowDialog()
$userphoto = Get-FileName -initialDirectory Previous
If ($userphoto -eq $null)
{
TestConnection
}
Else
{
$CheckPhoto = Preview-Photo $userphoto
If ($CheckPhoto -eq "Yes") {
Set-UserPhoto -Identity $user -PictureData ([System.IO.File]::ReadAllBytes($userphoto)) -Confirm:$false
TestConnection
}
Else {
DoYouWantToQuit; }
}
}
Function Preview-Photo($PhotoPath) {
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 = "Yes, this Photo is Correct"
$PhotoCheckFormYesButton.DialogResult = [System.Windows.Forms.DialogResult]::YES
$PhotoCheckForm.AcceptButton = $PhotoCheckFormYesButton
$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.DialogResult = [System.Windows.Forms.DialogResult]::NO
$PhotoCheckForm.CancelButton = $PhotoCheckFormNoButton
$PhotoCheckForm.Controls.Add($PhotoCheckFormNoButton)
$PhotoCheckForm.Controls.Add($PhotoCheckFormYesButton)
$PhotoCheckForm.Controls.Add($PhotoCheckFormLabel)
$PhotoCheckForm.ShowDialog()
}
Function DoYouWantToQuit() {
$ExitStatus = [System.Windows.Forms.MessageBox]::Show("Are you sure you want to exit?" , "Exit?" , 4)
If ($ExitStatus -eq "YES")
{Remove-PSSession -ID 1 ; [System.Windows.Forms.Application]::Exit($null); } #[environment]::Exit(0)
Else
{TestConnection}
}
Function TestConnection() {
$TestExchOnline = get-command get-user
If ($TestExchOnline -eq $null) {Connect-MSOL-Now; UploadPic}
Else {UploadPic}
}
TestConnection
#Connect-MSOL-Now
#UploadPic
Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.
When asked, what has been your best career decision?
Deciding to stick with EE.
Being involved with EE helped me to grow personally and professionally.
Connect with Certified Experts to gain insight and support on specific technology challenges including:
We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE