Solved

In a ps1 script, is it possible to force someone to input the date/time in a specific format?

Posted on 2011-09-23
11
216 Views
Last Modified: 2012-05-12
I am trying to write a simple script for our lower level helpdesk guys to do message tracking. Here is what I have so far:


#####Change Directory

cd C:\users\sa\desktop

####Gather Information for CmdLet

$Start = Read-Host "What is the start date/time?"
$End = Read-Host "What is the end date/time?"
$Recipients = Read-Host "Who are the recipients?"
$Sender = Read-Host "Who is the sender?"

#####Run Message Tracking

get-exchangeserver | where {$_.ishubtransportserver -eq $true -or $_.ismailboxserver -eq $true} |Get-MessageTrackingLog -start $start -end $end -recipients $recipients -sender $sender | select timestamp, sender, recipients, messagesubject, recipientstatus > trackinglog.txt


Is there a way to force them to input the date/time in a certain format and spit out an error in red if they don't?
0
Comment
Question by:Chuck Cobern
  • 6
  • 5
11 Comments
 
LVL 9

Expert Comment

by:chrismerritt
ID: 36590402
Hmm it depends how you want this to work with the end user, do you want it to keep asking the user for a value until they enter something which is valid? if so you will need to include your read-host in a do-while loop until a valid entry is input. Just giving them an error isn't going to help them progress with actually running the command they need though.

I'd say the best method to use is to see if the value entered can be parsed as a [datetime] value successfully, if not then an invalid value has been entered.

Something like this. I don't want to put any more time into this until you can confirm how exactly you want this to work, my head is saying that it shouldn't let the user proceed until they have put in a valid value, but that isn't included in your requirements:

Function ParseDateTime
{
	param
	(
	[string]$InputDate
	)

	if ($InputDate.Length -ne 0)
	{

		Try
		{
			[datetime]$CheckDate = $InputDate
		}
		Catch [Exception]
		{
			write-host -ForeGroundColor "Red" "Invalid Datetime value specified - check the format or values specified. Exception: $_"
			return
		}

		if ($CheckDate -ne $Null)
		{
			return $CheckDate
		}
	}
	else
	{
		return
	}
}


$Start = Read-Host "What is the start date/time (MM/DD/YYYY HH:MM:SS)?"

$StartDateTime = ParseDateTime $Start

if ($StartDateTime -ne $Null)
{
	"valid datetime entered" | Out-Host
}
else
{
	"invalid datetime entered" | Out-Host
}

Open in new window

0
 

Author Comment

by:Chuck Cobern
ID: 36590740
Yes you are correct. I do not want them to proceed until they enter a valid answer.
0
 
LVL 9

Expert Comment

by:chrismerritt
ID: 36591892
Please try this in your PS1:

#Checks to see if the entered value can be converted to a [datetime] value
Function ParseDateTime
{
	param
	(
	[string]$InputDate
	)

	if ($InputDate.Length -ne 0)
	{

		Try
		{
			[datetime]$CheckDate = $InputDate
		}
		Catch [Exception]
		{
			write-host -ForeGroundColor "Red" -BackGroundColor "Black" "Invalid Datetime value specified - check the format or values specified. Exception: $_"
			return
		}

		if ($CheckDate -ne $Null)
		{
			return $CheckDate
		}
	}
	else
	{
		return
	}
}

#Change Directory
cd C:\users\sa\desktop

#Get the user inputs
Do
{
	$Start = Read-Host "What is the start date/time (MM/DD/YYYY HH:MM:SS)?"
	$StartDateTime = ParseDateTime $Start
}
while ($StartDateTime -eq $Null)

Do
{
	$End = Read-Host "What is the end date/time (MM/DD/YYYY HH:MM:SS)?"
	$EndDateTime = ParseDateTime $End
}
while ($EndDateTime -eq $Null)

$Recipients = Read-Host "Who are the recipients?"
$Sender = Read-Host "Who is the sender?"

#Run Message Tracking
Get-ExchangeServer | ? {$_.ishubtransportserver -eq $true -or $_.ismailboxserver -eq $true} | Get-MessageTrackingLog -start ($StartDateTime.ToString("MM/dd/yyyy HH:mm:ss")) -end ($EndDateTime.ToString("MM/dd/yyyy HH:mm:ss")) -recipients $Recipients -sender $Sender | select timestamp, sender, recipients, messagesubject, recipientstatus > trackinglog.txt

Open in new window

0
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 

Author Comment

by:Chuck Cobern
ID: 36593111
I'm going to test this and will let you know. Thanks!!!
0
 

Author Comment

by:Chuck Cobern
ID: 36594017
This didn't provide the results I'm looking for. Here are the results:


[PS] C:\users\sa\desktop>cd $exscripts
[PS] C:\Program Files\Microsoft\Exchange Server\V14\scripts>.\MessageTracking1.ps1
What is the start date/time (MM/DD/YYYY HH:MM:SS)?: 8/27/11
valid datetime entered
[PS] C:\Program Files\Microsoft\Exchange Server\V14\scripts>
0
 
LVL 9

Expert Comment

by:chrismerritt
ID: 36594592
You're running the wrong one, run my second script instead of my first, they are quite different :)
0
 

Author Comment

by:Chuck Cobern
ID: 36596070
This did work! Is there any way to make it require a time with the date? If not, this will suffice.
0
 
LVL 9

Expert Comment

by:chrismerritt
ID: 36596229
As with all things Powershell there is more than one way to skin a cat.

This ammendment makes sure that the datestring entered is exactly 19 characters long, any less and it keep asking the end user.

This has the side effect of making sure that the value is entered in exactly the right format by the user, i.e. 05/30/2011 20:00:00

Anything else won't be processed.

#Checks to see if the entered value can be converted to a [datetime] value
Function ParseDateTime
{
	param
	(
	[string]$InputDate
	)

	if ($InputDate.Length -eq 19)
	{

		Try
		{
			[datetime]$CheckDate = $InputDate
		}
		Catch [Exception]
		{
			write-host -ForeGroundColor "Red" -BackGroundColor "Black" "Invalid Datetime value specified - check the format or values specified. Exception: $_"
			return
		}

		if ($CheckDate -ne $Null)
		{
			return $CheckDate
		}
	}
	else
	{
		return
	}
}

#Change Directory
cd C:\users\sa\desktop

#Get the user inputs
Do
{
	$Start = Read-Host "What is the start date/time (MM/DD/YYYY HH:MM:SS)?"
	$StartDateTime = ParseDateTime $Start
}
while ($StartDateTime -eq $Null)

Do
{
	$End = Read-Host "What is the end date/time (MM/DD/YYYY HH:MM:SS)?"
	$EndDateTime = ParseDateTime $End
}
while ($EndDateTime -eq $Null)

$Recipients = Read-Host "Who are the recipients?"
$Sender = Read-Host "Who is the sender?"

#Run Message Tracking
Get-ExchangeServer | ? {$_.ishubtransportserver -eq $true -or $_.ismailboxserver -eq $true} | Get-MessageTrackingLog -start ($StartDateTime.ToString("MM/dd/yyyy HH:mm:ss")) -end ($EndDateTime.ToString("MM/dd/yyyy HH:mm:ss")) -recipients $Recipients -sender $Sender | select timestamp, sender, recipients, messagesubject, recipientstatus > trackinglog.txt

Open in new window

0
 

Author Comment

by:Chuck Cobern
ID: 36719944
That works perfectly... One more thing I've been trying to do (I'm not too good at scripting) is, after it asks for the sender, get user input and ask if they want to output to the screen or to a text file, and require either an F or S answer. Depending on how they respond, will determine if the cmdlet will add the >textfile.txt portion.
0
 
LVL 9

Accepted Solution

by:
chrismerritt earned 500 total points
ID: 36720431
This should do it. Why ask them? just do both ;)

#Checks to see if the entered value can be converted to a [datetime] value
Function ParseDateTime
{
	param
	(
	[string]$InputDate
	)

	if ($InputDate.Length -eq 19)
	{

		Try
		{
			[datetime]$CheckDate = $InputDate
		}
		Catch [Exception]
		{
			write-host -ForeGroundColor "Red" -BackGroundColor "Black" "Invalid Datetime value specified - check the format or values specified. Exception: $_"
			return
		}

		if ($CheckDate -ne $Null)
		{
			return $CheckDate
		}
	}
	else
	{
		return
	}
}

#Change Directory
cd C:\users\sa\desktop

#Get the user inputs
Do
{
	$Start = Read-Host "What is the start date/time (MM/DD/YYYY HH:MM:SS)?"
	$StartDateTime = ParseDateTime $Start
}
while ($StartDateTime -eq $Null)

Do
{
	$End = Read-Host "What is the end date/time (MM/DD/YYYY HH:MM:SS)?"
	$EndDateTime = ParseDateTime $End
}
while ($EndDateTime -eq $Null)

$Recipients = Read-Host "Who are the recipients?"
$Sender = Read-Host "Who is the sender?"

#Run Message Tracking
$MessageTracking = Get-ExchangeServer | ? {$_.ishubtransportserver -eq $true -or $_.ismailboxserver -eq $true} | Get-MessageTrackingLog -start ($StartDateTime.ToString("MM/dd/yyyy HH:mm:ss")) -end ($EndDateTime.ToString("MM/dd/yyyy HH:mm:ss")) -recipients $Recipients -sender $Sender | select timestamp, sender, recipients, messagesubject, recipientstatus

#Export to file and to screen
$MessageTracking | Out-Host
$MessageTracking > trackinglog.txt

Open in new window


0
 

Author Closing Comment

by:Chuck Cobern
ID: 36816013
Great work....thanks for the help!!
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This process describes the steps required to Import and Export data from and to .pst files using Exchange 2010. We can use these steps to export data from a user to a .pst file, import data back to the same or a different user, or even import data t…
The following article is intended as a guide to using PowerShell as a more versatile and reliable form of application detection in SCCM.
In this video we show how to create an Address List in Exchange 2013. We show this process by using the Exchange Admin Center. Log into Exchange Admin Center.: First we need to log into the Exchange Admin Center. Navigate to the Organization >> Ad…
To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question