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
244 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
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 

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

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

This article will help to fix the below errors for MS Exchange Server 2013 I. Certificate error "name on the security certificate is invalid or does not match the name of the site" II. Out of Office not working III. Make Internal URLs and Externa…
A couple of months ago we ran into an issue that necessitated re-creating our Edge Subscriptions. However, when we attempted to execute the command: New-EdgeSubscription -filename C:\NewEdgeSub_01.xml we received an error indicating that the LDAP se…
how to add IIS SMTP to handle application/Scanner relays into office 365.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

717 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