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
240 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
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

 

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

Office 365 Training for Admins - 7 Day Trial

Learn how to provision tenants, synchronize on-premise Active Directory, implement Single Sign-On, customize Office deployment, and protect your organization with eDiscovery and DLP policies.  Only from Platform Scholar.

Question has a verified solution.

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

My attempt to use PowerShell and other great resources found online to simplify the deployment of Office 365 ProPlus client components to any workstation that needs it, regardless of existing Office components that may be needing attention.
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
In this video we show how to create a mailbox database 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 >> Data…
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…

732 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