Powershell Script Question for Exchange 2007

wayy2be
wayy2be used Ask the Experts™
on
I have two cmdlets that I would like to run in a script that I can run instead of running each comdlet by itself. What I would like the script to do is to run the first cmdlet and after it is done, print a success message and then run the other cmdlet. I would also like to have it create a logfile of the time it started, actions it took and time ended. Can anyone help me with this?  

The cmdlets are :

get-mailbox –server mail | Export-Mailbox –SubjectKeywords "Discovery Keywords" –TargetMailbox Administrator –TargetFolder 'MyData'

get-mailbox –server mail | Export-Mailbox –ContentKeywords "Discovery Keywords" –TargetMailbox Administrator –TargetFolder 'MyData

Also does anyone know if the cmdlets will search all the information stores on Exchange?

Thanks,
Joe
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Try this one, it will scan all mailboxes on the Mail server. If you want it to search all servers then you should just omit the -Server parameter off the Get-Mailbox command, or pipe Get-MailboxServer into Get-Mailbox instead.

#Function to export to log file and write to screen
Function ExportLogFile
{
param
	(
	$LogFilePath,
	$ExportText
	)
	
	$Date = (Get-Date).ToString("yyyy/MM/dd HH:mm:ss")
	$Text = $Date + " : " + $ExportText
	
	$Text | Out-File $LogFilePath -Append
	$Text | Out-Host
}

#Function to check if a file exists
Function FileExists
{
param
	(
	[string]$FileName
	)
	
	$CheckFile = New-Object System.IO.FileInfo ($FileName)
	return $CheckFile.exists
}

#LogFile and Directory config
$WorkingDir = "C:\tmp\"
$LogFile = "LogFile.txt"
$LogFilePath = $WorkingDir + $LogFile

#Delete content from Log File is file exists
if (FileExists $LogFilePath -eq $True)
{
	Clear-Content $LogFilePath
}

ExportLogFile $LogFilePath ("Starting Script")

$MailboxList = Get-Mailbox –Server mail

foreach ($Mailbox in $MailboxList)
{
	ExportLogFile $LogFilePath ("Exporting Mailbox Content for Mailbox" + $Mailbox + "(Subject Keywords)")
	Export-Mailbox -Identity $Mailbox –SubjectKeywords "Discovery Keywords" –TargetMailbox Administrator –TargetFolder 'MyData'
}

ExportLogFile $LogFilePath ("Success - Subject Keywords")

foreach ($Mailbox in $MailboxList)
{
	ExportLogFile $LogFilePath ("Exporting Mailbox Content for Mailbox" + $Mailbox + "(Content Keywords)")
	Export-Mailbox -Identity $Mailbox –ContentKeywords "Discovery Keywords" –TargetMailbox Administrator –TargetFolder 'MyData'
}

ExportLogFile $LogFilePath ("Success - Content Keywords")

Open in new window

Author

Commented:
Using this script will it search every information store on the server if there is more than one? Also will delete any of the emails that it finds? Thanks
I'm not sure why you term it as information store, Get-Mailbox will return all mailboxes unless you filter it with the -Server switch for example. If you want all mailboxes on a server then you can just change the line in my script on Line 42 so that it references whatever server you want.

If you want all mailboxes then as per my previous post, you can just omit the -Server switch completely.

No emails will be deleted because the Export-Mailbox command does not have -DeleteContent set to true, the command should be the same as the ones you posted originally.

Author

Commented:
Thanks. What is the best way to run this?  I am totally new to PowerShell.
Qlemo"Batchelor", Developer and EE Topic Advisor
Top Expert 2015
Commented:
Store the content into a file with extension .ps1.
Start the Exchange Console - which is PowerShell plus Exchange extensions.
Type in full path and filename of the .ps1 file.
That should perform the required actions. In above script, the "main" code starts with line 29. The following lines are determining what is executed.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial