?
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
Medium Priority
?
249 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 

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 2000 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

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

A project that enables an administrator to perform actions within a user session context not just at the time of login but any time later on day(s) or week(s) later.
If you troubleshoot Outlook for clients, you may want to know a bit more about the OST file before doing your next job. IMAP can cause a lot of drama if removed in the accounts without backing up.
To show how to generate a certificate request 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 Servers >> Certificates…
This video demonstrates how to sync Microsoft Exchange Public Folders with smartphones using CodeTwo Exchange Sync and Exchange ActiveSync. To learn more about CodeTwo Exchange Sync and download the free trial, go to: http://www.codetwo.com/excha…
Suggested Courses
Course of the Month9 days, 4 hours left to enroll

764 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