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

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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Synchronize a new Active Directory domain with an existing Office 365 tenant
Not sure what the best email signature size is? Are you worried about email signature image size? Follow this best practice guide.
In this video we show how to create a Resource Mailbox in Exchange 2013. We show this process by using the Exchange Admin Center. Log into Exchange Admin Center.: Navigate to the Recipients >> Resources tab.: "Recipients" is our default selection …
The basic steps you have just learned will be implemented in this video. The basic steps are shown to configure an Exchange DAG in a live working Exchange Server Environment and manage the same (Exchange Server 2010 Software is used in a Windows Ser…

758 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now