Writing a script for users to copy Outlook PST files from laptop C drives to network drive
----------
#Check whether Outlook is running - if so need to warn user that we will close it down if they click ok
$OutlookisOpen=$false
if (get-process-EA“SilentlyContinue”outlook|where {$_.ProcessName-eq“Outlook”}) {
$OutlookisOpen
=$true
}
$startcopy=$false
[void][System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
[void][System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
$objForm=New-Object
System.Windows.Forms.Form
$objForm.Text="Copy Outlook PST Files"
$objForm.Size=New-Object
System.Drawing.Size(800,600)
$objForm.font=New-Object
System.Drawing.Font("Times New Roman",18,[System.Drawing.FontStyle]::Italic)
$objForm.BackColor="Orange"
$objForm.StartPosition="CenterScreen"
# This just allows for Enter or Escape to entered in addition to clicking on OK or Cancel
$objForm.KeyPreview=$True
$objForm.Add_KeyDown({if
($_.KeyCode-eq"Enter")
{$global:startcopy=$true;$objForm.Close()}})
$objForm.Add_KeyDown({if
($_.KeyCode-eq"Escape")
{$objForm.Close()}})
$objLabel=New-Object
System.Windows.Forms.Label
$objLabel.Location=New-Object
System.Drawing.Size(50,50)
$objLabel.Size=New-Object
System.Drawing.Size(700,100)
$objLabel.Text="This process takes a long time. You must not shut your computer down or open Outlook whilst the copy process is running"
$objForm.Controls.Add($objLabel)
$objLabel=New-Object
System.Windows.Forms.Label
$objLabel.Location=New-Object
System.Drawing.Size(50,150)
$objLabel.Size=New-Object
System.Drawing.Size(700,100)
if ($OutlookisOpen-eq"True") {
$objLabel.Text="WARNING: Outlook is currently open. Save your work and click OK to close Outlook and continue or CANCEL to quit"
}
$objForm.Controls.Add($objLabel)
$ProgressLabel=New-Object
System.Windows.Forms.Label
$ProgressLabel.Location=New-Object
System.Drawing.Size(50,250)
$ProgressLabel.Size=New-Object
System.Drawing.Size(700,100)
$ProgressLabel.Text="Copying File - Please wait..."
$ProgressLabel.Visible=$false
$objForm.Controls.Add($ProgressLabel)
$OKButton=New-Object
System.Windows.Forms.Button
$OKButton.Location=New-Object
System.Drawing.Size(300,400)
$OKButton.Size=New-Object
System.Drawing.Size(100,50)
$OKButton.Text="OK"
$OKbutton.add_Click($OKButton_OnClick)
#$OKButton.Add_Click({$global:startcopy=$true;$objForm.Close()})
$objForm.Controls.Add($OKButton)
$CancelButton=New-Object
System.Windows.Forms.Button
$CancelButton.Location=New-Object
System.Drawing.Size(425,400)
$CancelButton.Size=New-Object
System.Drawing.Size(100,50)
$CancelButton.Text="Cancel"
$CancelButton.Add_Click({$objForm.Close()})
$objForm.Controls.Add($CancelButton)
# This ensure the window is in front
$objForm.Topmost=$True
$objForm.Add_Shown({$objForm.Activate()})
[void]$objForm.ShowDialog()
#------------------------------------------------------------------------------------------
$okbutton_OnClick= {
# Start the Process
$PSTLocal
="C:\PSTFiles\"
$PSTRemote
="H:\My Documents\PSTFiles"
if (!(Test-Path$PSTRemote)) {
new-item-path$PSTRemote-ItemTypeDirectory
}
Get-ChildItem
$PSTLocal-Recurse
-Force|ForEach-Object {
$ProgressLabel.Visible=$true
Copy-Item-Path$_.FullName-Destination$PSTRemote
}
$objForm.Close()
}
---------------
Its a work in progress but essentially working fine. What I am trying to do is to keep the form on screen and to display each file as its being copied with the filename. Therefore I setup the
form and text boxes etc. and do the copying process after user clicks on ok button. It copies fine but the $progresslabel text does not display because it appears the form is already open
How can I fix this ?
Ian Burnell, London (UK)