######################################################################## # Code Generated By: SAPIEN Technologies, Inc., PrimalForms 2009 v1.1.7.0 # Generated On: 5/17/2010 8:11 AM # Generated By: Mike Pfeiffer # More Info: http://www.mikepfeiffer.net/2010/05/manage-exchange-2010-thumbnail-photos-with-a-powershell-based-gui ######################################################################## #---------------------------------------------- #region Application Functions #---------------------------------------------- function OnApplicationLoad { #Note: This function runs before the form is created #Note: To get the script directory in the Packager use: Split-Path $hostinvocation.MyCommand.path #Note: To get the console output in the Packager (Windows Mode) use: $ConsoleOutput (Type: System.Collections.ArrayList) #Important: Form controls cannot be accessed in this function #TODO: Add snapins and custom code to validate the application load return $true #return true for success or false for failure #Set-ExecutionPolicy unrestricted } function OnApplicationExit { #Note: This function runs after the form is closed #TODO: Add custom code to clean up and unload snapins when the application exits $picturebox1.dispose() gci $env:temp 'exThumbnail*.jpg' | %{Remove-Item -Path $_.fullname -Confirm:$false -Force -EA 0} $script:ExitCode = 0 #Set the exit code for the Packager } #endregion #---------------------------------------------- # Generated Form Function #---------------------------------------------- function GenerateForm { #---------------------------------------------- #region Import Assemblies #---------------------------------------------- [void][reflection.assembly]::Load("System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089") [void][reflection.assembly]::Load("System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a") [void][reflection.assembly]::Load("mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089") [void][reflection.assembly]::Load("System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089") #endregion #---------------------------------------------- #region Generated Form Objects #---------------------------------------------- [System.Windows.Forms.Application]::EnableVisualStyles() $form1 = New-Object System.Windows.Forms.Form $statusbar1 = New-Object System.Windows.Forms.StatusBar $groupbox2 = New-Object System.Windows.Forms.GroupBox $linklabel1 = New-Object System.Windows.Forms.LinkLabel $button4 = New-Object System.Windows.Forms.Button $button3 = New-Object System.Windows.Forms.Button $textbox2 = New-Object System.Windows.Forms.TextBox $lblPhoto = New-Object System.Windows.Forms.Label $global:picturebox1 = New-Object System.Windows.Forms.PictureBox $groupbox1 = New-Object System.Windows.Forms.GroupBox $datagridview1 = New-Object System.Windows.Forms.DataGridView $button1 = New-Object System.Windows.Forms.Button $textbox1 = New-Object System.Windows.Forms.TextBox $openfiledialog1 = New-Object System.Windows.Forms.OpenFileDialog $InitialFormWindowState = New-Object System.Windows.Forms.FormWindowState #endregion Generated Form Objects #---------------------------------------------- # User Generated Script #---------------------------------------------- function Get-ExchangeServerInSite { $ADSite = [System.DirectoryServices.ActiveDirectory.ActiveDirectorySite] $siteDN = $ADSite::GetComputerSite().GetDirectoryEntry().distinguishedName $configNC=([ADSI]"LDAP://RootDse").configurationNamingContext $search = new-object DirectoryServices.DirectorySearcher([ADSI]"LDAP://$configNC") $objectClass = "objectClass=msExchExchangeServer" $version = "versionNumber>=1937801568" $site = "msExchServerSite=$siteDN" $search.Filter = "(&($objectClass)($version)($site))" $search.PageSize=1000 [void] $search.PropertiesToLoad.Add("name") [void] $search.PropertiesToLoad.Add("msexchcurrentserverroles") [void] $search.PropertiesToLoad.Add("networkaddress") $search.FindAll() | %{ New-Object PSObject -Property @{ Name = $_.Properties.name[0] FQDN = $_.Properties.networkaddress | %{if ($_ -match "ncacn_ip_tcp") {$_.split(":")[1]}} Roles = $_.Properties.msexchcurrentserverroles[0] } } } function Import-EMS { $servers = New-Object System.Collections.ArrayList Get-ExchangeServerInSite | %{ [void]$servers.Add(($_.fqdn)) } #select a random server from the current site if($servers.count -gt 1) { $random = Get-Random -Minimum 0 -Maximum $servers.count $fqdn = $servers[$random] } else { $fqdn = $servers[0] } try { $session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri "http://$fqdn/PowerShell/" -Authentication Kerberos Import-PSSession $session } catch { [System.Windows.Forms.MessageBox]::Show( $_.Exception.Message, "Error", [System.Windows.Forms.MessageBoxButtons]::OK, [System.Windows.Forms.MessageBoxIcon]::Error ) } } $handler_button1_Click={ #TODO: Place custom script here $textbox2.Text = "" $picturebox1.Image = $null $statusbar1.Text = "Status: Searching..." try { $mailboxes = New-Object System.Collections.ArrayList if(!($textbox1.Text)) { $mailboxInfo = Get-Mailbox -resultsize unlimited -ErrorAction Stop | select Name,Alias,PrimarySMTPAddress,DistinguishedName } else { $mailboxInfo = Get-Mailbox $textbox1.Text -resultsize unlimited -ErrorAction Stop | select Name,Alias,PrimarySMTPAddress,DistinguishedName } if($mailboxInfo.count) { $mailboxes.AddRange($mailboxInfo) } else { $mailboxes.Add($mailboxInfo) } $datagridview1.DataSource = $mailboxes $form1.refresh() $statusbar1.Text = "" } catch { [System.Windows.Forms.MessageBox]::Show( $_.Exception.Message, "Error", [System.Windows.Forms.MessageBoxButtons]::OK, [System.Windows.Forms.MessageBoxIcon]::Error ) $statusbar1.Text = "" } } $handler_button3_Click={ #TODO: Place custom script here if($textbox2.Text -eq "") { [System.Windows.Forms.MessageBox]::Show( "You must first select a file.", "Error", [System.Windows.Forms.MessageBoxButtons]::OK, [System.Windows.Forms.MessageBoxIcon]::Error ) } else { try { Import-RecipientDataProperty -Identity $dataGridView1.SelectedCells[0].Value.ToString() -Picture -FileData ([Byte[]]$(Get-Content -Path $textbox2.Text -Encoding Byte -ReadCount 0)) -ErrorAction Stop [System.Windows.Forms.MessageBox]::Show( "The image was uploaded successfully.", "Image Upload", [System.Windows.Forms.MessageBoxButtons]::OK, [System.Windows.Forms.MessageBoxIcon]::Information ) $textbox2.Text = "" $picturebox1.Image = $null } catch { [System.Windows.Forms.MessageBox]::Show( $_.Exception.Message, "Error", [System.Windows.Forms.MessageBoxButtons]::OK, [System.Windows.Forms.MessageBoxIcon]::Error ) } } } $handler_button4_Click={ #TODO: Place custom script here try { if($openfiledialog1.ShowDialog() -eq [System.Windows.Forms.DialogResult]::OK) { $textbox2.Text = $openfiledialog1.FileName.ToString() $picturebox1.Image = [System.Drawing.Image]::FromFile($openfiledialog1.FileName.ToString()) $picturebox1.Refresh() } } catch { [System.Windows.Forms.MessageBox]::Show( $_.Exception.Message, "Error", [System.Windows.Forms.MessageBoxButtons]::OK, [System.Windows.Forms.MessageBoxIcon]::Error ) } } $FormEvent_Load={ #TODO: Place custom script here if(!(get-command get-mailbox -EA 0)) { if(Get-PSSnapin -Registered "Microsoft.Exchange.Management.PowerShell.E2010" -EA 0) { Add-PSSnapin "Microsoft.Exchange.Management.PowerShell.E2010" } else { $message = [System.Windows.Forms.MessageBox]::Show( "Thumbnail Photo Manager will now import EMS commands from Exchange. Please be patient..this may take a few seconds.", "Connecting to Exchange", [System.Windows.Forms.MessageBoxButtons]::OK, [System.Windows.Forms.MessageBoxIcon]::Information ) Import-EMS } } } $handler_linklabel1_LinkClicked=[System.Windows.Forms.LinkLabelLinkClickedEventHandler]{ #Event Argument: $_ = [System.Windows.Forms.LinkLabelLinkClickedEventArgs] #TODO: Place custom script here $picturebox1.Image = $null $user = [adsi]"LDAP://$($dataGridView1.SelectedCells[3].Value.ToString())" if(!($user.thumbnailPhoto)) { [System.Windows.Forms.MessageBox]::Show( "$($dataGridView1.SelectedCells[0].Value.ToString()) does not currently have a thumbnail photo defined.", "Thumbnail Photo", [System.Windows.Forms.MessageBoxButtons]::OK, [System.Windows.Forms.MessageBoxIcon]::Warning ) } else { try { $file = Join-Path -path $env:temp -ChildPath ("exThumbnail_$([System.Guid]::NewGuid().Guid).jpg") Export-RecipientDataProperty -Identity $dataGridView1.SelectedCells[0].Value.ToString() -Picture -ErrorAction Stop | ForEach { $_.FileData | Add-Content $file -Encoding Byte} $picturebox1.Image = [System.Drawing.Image]::FromFile($file) $picturebox1.Refresh() } catch { [System.Windows.Forms.MessageBox]::Show( $_.Exception.Message, "Error", [System.Windows.Forms.MessageBoxButtons]::OK, [System.Windows.Forms.MessageBoxIcon]::Error ) } } } #---------------------------------------------- # Generated Events #---------------------------------------------- $Form_StateCorrection_Load= { #Correct the initial state of the form to prevent the .Net maximized form issue $form1.WindowState = $InitialFormWindowState } #---------------------------------------------- #region Generated Form Code #---------------------------------------------- # # form1 # $form1.Controls.Add($statusbar1) $form1.Controls.Add($groupbox2) $form1.Controls.Add($groupbox1) $form1.Text = "Exchange Thumbnail Photo Manager" $form1.Name = "form1" $form1.MinimumSize = New-Object System.Drawing.Size(559,396) $form1.StartPosition = [System.Windows.Forms.FormStartPosition]::CenterScreen $form1.DataBindings.DefaultDataSourceUpdateMode = [System.Windows.Forms.DataSourceUpdateMode]::OnValidation $form1.ClientSize = New-Object System.Drawing.Size(551,386) $form1.add_Load($FormEvent_Load) # # statusbar1 # $statusbar1.Size = New-Object System.Drawing.Size(551,22) $statusbar1.TabIndex = 3 $statusbar1.Location = New-Object System.Drawing.Point(0,364) $statusbar1.DataBindings.DefaultDataSourceUpdateMode = [System.Windows.Forms.DataSourceUpdateMode]::OnValidation $statusbar1.Name = "statusbar1" # # groupbox2 # $groupbox2.Controls.Add($linklabel1) $groupbox2.Controls.Add($button4) $groupbox2.Controls.Add($button3) $groupbox2.Controls.Add($textbox2) $groupbox2.Controls.Add($lblPhoto) $groupbox2.Controls.Add($picturebox1) $groupbox2.TabIndex = 2 $groupbox2.Anchor = [System.Windows.Forms.AnchorStyles]::Bottom -bor [System.Windows.Forms.AnchorStyles]::Left -bor [System.Windows.Forms.AnchorStyles]::Right $groupbox2.Size = New-Object System.Drawing.Size(527,142) $groupbox2.Text = "Select Photo" $groupbox2.Location = New-Object System.Drawing.Point(12,216) $groupbox2.DataBindings.DefaultDataSourceUpdateMode = [System.Windows.Forms.DataSourceUpdateMode]::OnValidation $groupbox2.TabStop = $False $groupbox2.Name = "groupbox2" # # linklabel1 # $linklabel1.Size = New-Object System.Drawing.Size(231,23) $linklabel1.TabStop = $True $linklabel1.Text = "Preview Current Photo" $linklabel1.TabIndex = 7 $linklabel1.Name = "linklabel1" $linklabel1.Location = New-Object System.Drawing.Point(11,113) $linklabel1.DataBindings.DefaultDataSourceUpdateMode = [System.Windows.Forms.DataSourceUpdateMode]::OnValidation $linklabel1.add_LinkClicked($handler_linklabel1_LinkClicked) # # button4 # $button4.TabIndex = 6 $button4.Anchor = [System.Windows.Forms.AnchorStyles]::Top -bor [System.Windows.Forms.AnchorStyles]::Right $button4.Name = "button4" $button4.Size = New-Object System.Drawing.Size(66,23) $button4.UseVisualStyleBackColor = $True $button4.FlatStyle = [System.Windows.Forms.FlatStyle]::Flat $button4.Text = "Browse" $button4.Location = New-Object System.Drawing.Point(284,31) $button4.DataBindings.DefaultDataSourceUpdateMode = [System.Windows.Forms.DataSourceUpdateMode]::OnValidation $button4.add_Click($handler_button4_Click) # # button3 # $button3.TabIndex = 5 $button3.Name = "button3" $button3.Size = New-Object System.Drawing.Size(117,23) $button3.UseVisualStyleBackColor = $True $button3.FlatStyle = [System.Windows.Forms.FlatStyle]::Flat $button3.Text = "Upload New Photo" $button3.Location = New-Object System.Drawing.Point(11,63) $button3.DataBindings.DefaultDataSourceUpdateMode = [System.Windows.Forms.DataSourceUpdateMode]::OnValidation $button3.add_Click($handler_button3_Click) # # textbox2 # $textbox2.Size = New-Object System.Drawing.Size(262,20) $textbox2.DataBindings.DefaultDataSourceUpdateMode = [System.Windows.Forms.DataSourceUpdateMode]::OnValidation $textbox2.ReadOnly = $True $textbox2.BorderStyle = [System.Windows.Forms.BorderStyle]::FixedSingle $textbox2.Anchor = [System.Windows.Forms.AnchorStyles]::Top -bor [System.Windows.Forms.AnchorStyles]::Left -bor [System.Windows.Forms.AnchorStyles]::Right $textbox2.Name = "textbox2" $textbox2.BackColor = [System.Drawing.Color]::FromArgb(255,255,255,255) $textbox2.Location = New-Object System.Drawing.Point(11,31) $textbox2.TabIndex = 3 # # lblPhoto # $lblPhoto.TabIndex = 4 $lblPhoto.Anchor = [System.Windows.Forms.AnchorStyles]::Top -bor [System.Windows.Forms.AnchorStyles]::Right $lblPhoto.Size = New-Object System.Drawing.Size(87,17) $lblPhoto.Text = "Photo Preview" $lblPhoto.Location = New-Object System.Drawing.Point(421,120) $lblPhoto.DataBindings.DefaultDataSourceUpdateMode = [System.Windows.Forms.DataSourceUpdateMode]::OnValidation $lblPhoto.Name = "lblPhoto" # # picturebox1 # $picturebox1.TabIndex = 0 $picturebox1.Anchor = [System.Windows.Forms.AnchorStyles]::Top -bor [System.Windows.Forms.AnchorStyles]::Right $picturebox1.Size = New-Object System.Drawing.Size(96,96) $picturebox1.BorderStyle = [System.Windows.Forms.BorderStyle]::FixedSingle $picturebox1.Location = New-Object System.Drawing.Point(412,19) $picturebox1.TabStop = $False $picturebox1.Name = "picturebox1" $picturebox1.DataBindings.DefaultDataSourceUpdateMode = [System.Windows.Forms.DataSourceUpdateMode]::OnValidation # # groupbox1 # $groupbox1.Controls.Add($datagridview1) $groupbox1.Controls.Add($button1) $groupbox1.Controls.Add($textbox1) $groupbox1.TabIndex = 1 $groupbox1.Anchor = [System.Windows.Forms.AnchorStyles]::Top -bor [System.Windows.Forms.AnchorStyles]::Bottom -bor [System.Windows.Forms.AnchorStyles]::Left -bor [System.Windows.Forms.AnchorStyles]::Right $groupbox1.Size = New-Object System.Drawing.Size(527,183) $groupbox1.Text = "Select Mailbox" $groupbox1.Location = New-Object System.Drawing.Point(12,12) $groupbox1.DataBindings.DefaultDataSourceUpdateMode = [System.Windows.Forms.DataSourceUpdateMode]::OnValidation $groupbox1.TabStop = $False $groupbox1.Name = "groupbox1" # # datagridview1 # $datagridview1.ShowEditingIcon = $False $datagridview1.AllowUserToAddRows = $False $datagridview1.AutoSizeColumnsMode = [System.Windows.Forms.DataGridViewAutoSizeColumnsMode]::Fill $System_Windows_Forms_DataGridViewCellStyle_22 = New-Object System.Windows.Forms.DataGridViewCellStyle $System_Windows_Forms_DataGridViewCellStyle_22.Alignment = [System.Windows.Forms.DataGridViewContentAlignment]::MiddleLeft $System_Windows_Forms_DataGridViewCellStyle_22.BackColor = [System.Drawing.Color]::FromArgb(255,212,208,200) $System_Windows_Forms_DataGridViewCellStyle_22.Font = New-Object System.Drawing.Font("Microsoft Sans Serif",8.25,0,3,1) $System_Windows_Forms_DataGridViewCellStyle_22.ForeColor = [System.Drawing.Color]::FromArgb(255,0,0,0) $System_Windows_Forms_DataGridViewCellStyle_22.SelectionBackColor = [System.Drawing.Color]::FromArgb(255,10,36,106) $System_Windows_Forms_DataGridViewCellStyle_22.SelectionForeColor = [System.Drawing.Color]::FromArgb(255,255,255,255) $System_Windows_Forms_DataGridViewCellStyle_22.WrapMode = [System.Windows.Forms.DataGridViewTriState]::True $datagridview1.ColumnHeadersDefaultCellStyle = $System_Windows_Forms_DataGridViewCellStyle_22 $datagridview1.Name = "datagridview1" $datagridview1.Size = New-Object System.Drawing.Size(492,98) $datagridview1.Anchor = [System.Windows.Forms.AnchorStyles]::Top -bor [System.Windows.Forms.AnchorStyles]::Bottom -bor [System.Windows.Forms.AnchorStyles]::Left -bor [System.Windows.Forms.AnchorStyles]::Right $datagridview1.SelectionMode = [System.Windows.Forms.DataGridViewSelectionMode]::FullRowSelect $datagridview1.BackgroundColor = [System.Drawing.Color]::FromArgb(255,255,255,255) $datagridview1.DataBindings.DefaultDataSourceUpdateMode = [System.Windows.Forms.DataSourceUpdateMode]::OnValidation $System_Windows_Forms_DataGridViewCellStyle_23 = New-Object System.Windows.Forms.DataGridViewCellStyle $System_Windows_Forms_DataGridViewCellStyle_23.Alignment = [System.Windows.Forms.DataGridViewContentAlignment]::MiddleLeft $System_Windows_Forms_DataGridViewCellStyle_23.BackColor = [System.Drawing.Color]::FromArgb(255,212,208,200) $System_Windows_Forms_DataGridViewCellStyle_23.Font = New-Object System.Drawing.Font("Microsoft Sans Serif",8.25,0,3,1) $System_Windows_Forms_DataGridViewCellStyle_23.ForeColor = [System.Drawing.Color]::FromArgb(255,0,0,0) $System_Windows_Forms_DataGridViewCellStyle_23.SelectionBackColor = [System.Drawing.Color]::FromArgb(255,10,36,106) $System_Windows_Forms_DataGridViewCellStyle_23.SelectionForeColor = [System.Drawing.Color]::FromArgb(255,255,255,255) $System_Windows_Forms_DataGridViewCellStyle_23.WrapMode = [System.Windows.Forms.DataGridViewTriState]::True $datagridview1.RowHeadersDefaultCellStyle = $System_Windows_Forms_DataGridViewCellStyle_23 $System_Windows_Forms_DataGridViewCellStyle_24 = New-Object System.Windows.Forms.DataGridViewCellStyle $System_Windows_Forms_DataGridViewCellStyle_24.Alignment = [System.Windows.Forms.DataGridViewContentAlignment]::MiddleLeft $System_Windows_Forms_DataGridViewCellStyle_24.BackColor = [System.Drawing.Color]::FromArgb(255,255,255,255) $System_Windows_Forms_DataGridViewCellStyle_24.Font = New-Object System.Drawing.Font("Microsoft Sans Serif",8.25,0,3,1) $System_Windows_Forms_DataGridViewCellStyle_24.ForeColor = [System.Drawing.Color]::FromArgb(255,0,0,0) $System_Windows_Forms_DataGridViewCellStyle_24.SelectionBackColor = [System.Drawing.Color]::FromArgb(255,10,36,106) $System_Windows_Forms_DataGridViewCellStyle_24.SelectionForeColor = [System.Drawing.Color]::FromArgb(255,255,255,255) $System_Windows_Forms_DataGridViewCellStyle_24.WrapMode = [System.Windows.Forms.DataGridViewTriState]::False $datagridview1.DefaultCellStyle = $System_Windows_Forms_DataGridViewCellStyle_24 $datagridview1.Location = New-Object System.Drawing.Point(16,69) $datagridview1.AllowUserToResizeRows = $False $datagridview1.RowHeadersVisible = $False $datagridview1.TabIndex = 2 $datagridview1.ReadOnly = $True $datagridview1.AllowUserToDeleteRows = $False $datagridview1.MultiSelect = $False $datagridview1.ColumnHeadersHeightSizeMode = [System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode]::AutoSize # # button1 # $button1.TabIndex = 1 $button1.Anchor = [System.Windows.Forms.AnchorStyles]::Top -bor [System.Windows.Forms.AnchorStyles]::Right $button1.Name = "button1" $button1.Size = New-Object System.Drawing.Size(87,23) $button1.UseVisualStyleBackColor = $True $button1.FlatStyle = [System.Windows.Forms.FlatStyle]::Flat $button1.Text = "Search" $button1.Location = New-Object System.Drawing.Point(421,29) $button1.DataBindings.DefaultDataSourceUpdateMode = [System.Windows.Forms.DataSourceUpdateMode]::OnValidation $button1.add_Click($handler_button1_Click) # # textbox1 # $textbox1.Size = New-Object System.Drawing.Size(393,20) $textbox1.DataBindings.DefaultDataSourceUpdateMode = [System.Windows.Forms.DataSourceUpdateMode]::OnValidation $textbox1.BorderStyle = [System.Windows.Forms.BorderStyle]::FixedSingle $textbox1.Anchor = [System.Windows.Forms.AnchorStyles]::Top -bor [System.Windows.Forms.AnchorStyles]::Left -bor [System.Windows.Forms.AnchorStyles]::Right $textbox1.Name = "textbox1" $textbox1.Location = New-Object System.Drawing.Point(16,30) $textbox1.TabIndex = 0 # # openfiledialog1 # $openfiledialog1.Filter = "Image files (*.jpg)|*.jpg" $openfiledialog1.ShowHelp = $True $openfiledialog1.InitialDirectory = "C:\" #endregion Generated Form Code #---------------------------------------------- #Save the initial state of the form $InitialFormWindowState = $form1.WindowState #Init the OnLoad event to correct the initial state of the form $form1.add_Load($Form_StateCorrection_Load) #Show the Form return $form1.ShowDialog() } #End Function #Call OnApplicationLoad to initialize if(OnApplicationLoad -eq $true) { #Create the form GenerateForm | Out-Null #Perform cleanup OnApplicationExit }