Date conversion

Hi All

again experimenting with Sapien, but this is likely a general question so thought i would try here :slight_smile:

i am using a gui & some date selection, now my issue iss the data selection adds a time to the end which i cant use for the exchange dumpster command (as below in the $start)

05 September 2016 00:00:00

i realise there is quite a bit of uneeded code here but was just trying to see why the listbox would show a short date and then pass a longer format to the command line. Appreciate any help

$formDumpsterRestore_Load={
	#TODO: Initialize Form Controls here
	
	Set-ExecutionPolicy -ExecutionPolicy Unrestricted
	$Username = "global\mark.prior"
	$Password = ConvertTo-SecureString "xxxxxxxxx" -AsPlainText -Force
	$cred = new-object -typename System.Management.Automation.PSCredential -argumentlist $username, $password
	
	$SESJA_EX = New-PSSession -Credential $cred -ConnectionUri http://eurxhub02/powershell -ConfigurationName microsoft.exchange
	Import-PSSession $SESJA_EX
	
}

$labelDateTo_Click={
	#TODO: Place custom script here
	
}

$labelMailbox_Click={
	#TODO: Place custom script here
	
}

$monthcalendar1_DateChanged=[System.Windows.Forms.DateRangeEventHandler]{
#Event Argument: $_ = [System.Windows.Forms.DateRangeEventArgs]
	#TODO: Place custom script here
	
}

$monthcalendar2_DateChanged = [System.Windows.Forms.DateRangeEventHandler]{
	#Event Argument: $_ = [System.Windows.Forms.DateRangeEventArgs]
	#TODO: Place custom script here
	
}


$Mailbox_TextChanged={
	#TODO: Place custom script here
	
}

$buttonRestore_Click={
	$this.enabled = $false
	
	
	Set-ExecutionPolicy -ExecutionPolicy Unrestricted
	
	$testpath = Get-mailbox $Mailbox.Text
	
	if ($testpath)
	{
		$collection = $Mailbox.Text, $monthcalendar1.SelectionStart, $monthcalendar2.SelectionEnd, $combobox1.Text
		$Start = $monthcalendar1.SelectionStart
		$End = $monthcalendar2.SelectionEnd
		
		$Start |Out-File c:\date.txt
		
		Search-Mailbox $mailbox.text -SearchDumpsterOnly -TargetMailbox emea-restoreditems -SearchQuery $combobox1.Text"$start..$End" -TargetFolder Inbox
		
		Load-ListBox $listBox  $collection
		
		
	}
	
	Else
	{
		$empty = "Mailbox Not Found"
		
		Load-ListBox $listBox  $empty
	}
	
	#Remove-PSSession $sesja_ex
	
	
	$this.enabled = $true
	
}

$statusbar1_PanelClick=[System.Windows.Forms.StatusBarPanelClickEventHandler]{
#Event Argument: $_ = [System.Windows.Forms.StatusBarPanelClickEventArgs]
	#TODO: Place custom script here
	
}

#region Control Helper Functions
function Load-ComboBox 
{

	Param (
		[ValidateNotNull()]
		[Parameter(Mandatory=$true)]
		[System.Windows.Forms.ComboBox]$ComboBox,
		[ValidateNotNull()]
		[Parameter(Mandatory=$true)]
		$Items,
	    [Parameter(Mandatory=$false)]
		[string]$DisplayMember,
		[switch]$Append
	)
	
	if(-not $Append)
	{
		$ComboBox.Items.Clear()	
	}
	
	if($Items -is [Object[]])
	{
		$ComboBox.Items.AddRange($Items)
	}
	elseif ($Items -is [System.Collections.IEnumerable])
	{
		$ComboBox.BeginUpdate()
		foreach($obj in $Items)
		{
			$ComboBox.Items.Add($obj)	
		}
		$ComboBox.EndUpdate()
	}
	else
	{
		$ComboBox.Items.Add($Items)	
	}

	$ComboBox.DisplayMember = $DisplayMember	
}

function Load-ListBox 
{

	Param (
		[ValidateNotNull()]
		[Parameter(Mandatory=$true)]
		[System.Windows.Forms.ListBox]$ListBox,
		[ValidateNotNull()]
		[Parameter(Mandatory=$true)]
		$Items,
	    [Parameter(Mandatory=$false)]
		[string]$DisplayMember,
		[switch]$Append
	)
	
	if(-not $Append)
	{
		$listBox.Items.Clear()	
	}
	
	if($Items -is [System.Windows.Forms.ListBox+ObjectCollection] -or $Items -is [System.Collections.ICollection])
	{
		$listBox.Items.AddRange($Items)
	}
	elseif ($Items -is [System.Collections.IEnumerable])
	{
		$listBox.BeginUpdate()
		foreach($obj in $Items)
		{
			$listBox.Items.Add($obj)
		}
		$listBox.EndUpdate()
	}
	else
	{
		$listBox.Items.Add($Items)	
	}

	$listBox.DisplayMember = $DisplayMember	
}
#endregion

$textbox1_TextChanged={
	#TODO: Place custom script here
	
}



$combobox1_SelectedIndexChanged={
	#TODO: Place custom script here
	
}

Manipulating the date to the format you want is simple. You just need to use Get-Date to convert the string into a datetime object and then format the date to what you want to do. Take a look at Windows PowerShell Tip of the Week: Formatting Dates and Time. Here is a small example:

$startDate = "2/2/2015 8:00:00"
$shortDate = Get-Date $startDate -Format d

Output:

2/2/2015

A quick edit. As long as the date is a ‘standard’ datetime format, Get-Date will parse it. If you get funky dates from databases as non-standard string formats, you have to do manual formatting, but the format you posted does parse properly with Get-Date

Thanks Rob, much appreciated

Hmmm very odd, i can get the command line

	Search-Mailbox $mailbox.text -SearchDumpsterOnly -TargetMailbox emea-restoreditems -SearchQuery $combobox1.Text"$start..$End" -TargetFolder Inbox

to work in powershell but not in sapien, Error below

WARNING: The names of some imported commands from the module ‘tmp_riytoazx.0cw’ include unapproved verbs that might make them less discoverable. To find the commands with unapproved verbs, run the Import-Module command again with the Verbose parameter. For a list of approved verbs, type Get-Verb.
ERROR: A positional parameter cannot be found that accepts argument ‘04/09/2016…06/09/2016’.
ERROR: + CategoryInfo : InvalidArgument: (:slight_smile: [Search-Mailbox], ParameterBindingException
ERROR: + FullyQualifiedErrorId : PositionalParameterNotFound,Search-Mailbox
ERROR: + PSComputerName : eurxhub02
ERROR:

must be something to do with the way im passing info as manually entering

Search-Mailbox mark.prior -SearchDumpsterOnly -TargetMailbox emea-restoreditems -SearchQuery received:“04/09/2016…06/09/2016” -TargetFolder Inbox

works in sapien

Probably has something to do with passing datetime objects into your search query. Try with strings.

yep sorted, converted the whole command to a string and its working

Dan another Q you maybe able to help with, how would i get the GUI to display a progress bar (displayng % of command complete or left to go?)

The query should be passed as a string, such as this:

-SearchQuery 'received:"04/09/2016..06/09/2016"'

Also, consider using a splat to pass parameters to your commmand such as this:

$startDate = "01 September 2016 00:00:00"
$endDate = "05 September 2016 00:00:00"

$mailboxParams = @{
    Identity       = "Mark.Prior"
    TargetMailbox  = "emea-restoreditems"
    SearchQuery    = 'received:"{0}..{1}"' -f (Get-Date $startDate -f d), (Get-Date $startDate -f d)
    TargetFolder   = "Inbox"
    SearchDumpsterOnly=$true
}

$results = Search-Mailbox @mailboxParams

Keeps the command clean and you can add and remove command items dynamically with .Remove(“TargetFolder”) or .Add(“SearchDumpsterOnly”, $true).

Thanks Rob, think i will give splatting a go with this

$.02 I’m not a fan of the month calendar control. The Datetimepicker is much more flexible.

$datetimepicker1_ValueChanged = {
	
	$textbox1.Text = $dateTimePicker1.Value.ToString("yyyy/MM/dd")
	$datetimepicker2.MinDate = $datetimepicker1.Value.AddDays(1)
	
}

$datetimepicker2_ValueChanged = {
	
	$textbox2.Text = $datetimepicker2.Value.ToString("yyyy/MM/dd")
	
}