Tricky powershell this time

Alex
Alex used Ask the Experts™
on
Right,

So I have a function to bring up a date and time


[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing") 

Function Prompt-For-Date
{
Param(  
    [string]$sDatePrompt = "Select a Date"
) #End Param

$script:dtmDate = ''
$objForm = New-Object Windows.Forms.Form 

$objForm.Text = $sDatePrompt
$objForm.Size = New-Object Drawing.Size @(300,300) 
$objForm.StartPosition = "CenterScreen"

$objForm.KeyPreview = $True

$objForm.Add_KeyDown({
    if ($_.KeyCode -eq "Enter") 
        {
            $script:dtmDate = $objCalendar.SelectionStart
            $objForm.Close()
            
        }
    })

$objForm.Add_KeyDown({
    if ($_.KeyCode -eq "Escape") 
        {
            $objForm.Close()
        }
    })

$objCalendar = New-Object System.Windows.Forms.MonthCalendar 
$objCalendar.ShowTodayCircle = $False
$objCalendar.MaxSelectionCount = 1
$objForm.Controls.Add($objCalendar) 

$objForm.Topmost = $True

$objForm.Add_Shown({$objForm.Activate()})  
[void] $objForm.ShowDialog()
return $script:dtmDate


}

$dateReturned = Prompt-For-Date -sDatePrompt "Please Select A Date"
Write-Host "Date:" $dateReturned

Open in new window


I have a bit of code which will pull information

function Copy-SelectedFile
{
    [CmdletBinding()]
    param(
        [parameter(Mandatory = $true, ValueFromPipelineByPropertyName = $true)]
        [ValidateScript({Test-Path -path $_})]
        [string]$SourcePath,

        [parameter(Mandatory = $true, ValueFromPipelineByPropertyName = $true)]
        [string]$DestinationPath,

        [parameter(Mandatory = $true, ValueFromPipelineByPropertyName = $true)]
        [System.Management.Automation.PSCredential]$Credential = (Get-Credential), 

        [parameter(ValueFromPipelineByPropertyName = $true)]
        [datetime]$MinimumAgeTime = ([datetime]::now), 

        [parameter(ValueFromPipelineByPropertyName = $true)]
        [datetime]$MaximumAgeTime
    )

    $Files = Get-ChildItem -Path $SourcePath | Where-Object { $_.LastWriteTime -gt $MinimumAgeTime -and $_.LastWriteTime -lt $MaximumAgeTime}
    
    $Destination = New-Item -Path $DestinationPath -ItemType Directory -Name $MinimumAgeTime.ToString('ddMMyy')
    Copy-Item -Path $Files -Destination $Destination -Credential $Credential -Recurse
}

Open in new window



How the hell do I get my date and time to get pushed into the second bit of code to pull the data off the disk?1?!?!?!?!?!?!?

Any help would be great.

Thanks
Alex
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Qlemo"Batchelor", Developer and EE Topic Advisor
Top Expert 2015

Commented:
If you want to keep the scripts separate, append
$dateReturned

Open in new window

to the first code, and run something like
. c:\path\to\script\script2.ps1     # imports the function for use
$date = c:\path\to\script\script1.ps1
Copy-SelectedFile -MinimumAgeTime $date -MaximumAgeTime $date.AddDays(1) C:\Test\ D:\Test\

Open in new window

or similar. Note that you need to dates for the function.
AlexSenior Infrastructure Analyst

Author

Commented:
There isn't any reason to keep them separate, I just have no idea how to join them... :(
"Batchelor", Developer and EE Topic Advisor
Top Expert 2015
Commented:
Do you need the date input twice, one each for minimum and maximum date? I assumed you want to input only one.
In this combined all-in-one script, you are prompted for each date:
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing") 

Function Prompt-For-Date
{
Param(  
    [string]$sDatePrompt = "Select a Date"
) #End Param

$script:dtmDate = ''
$objForm = New-Object Windows.Forms.Form 

$objForm.Text = $sDatePrompt
$objForm.Size = New-Object Drawing.Size @(300,300) 
$objForm.StartPosition = "CenterScreen"

$objForm.KeyPreview = $True

$objForm.Add_KeyDown({
    if ($_.KeyCode -eq "Enter") 
        {
            $script:dtmDate = $objCalendar.SelectionStart
            $objForm.Close()
            
        }
    })

$objForm.Add_KeyDown({
    if ($_.KeyCode -eq "Escape") 
        {
            $objForm.Close()
        }
    })

$objCalendar = New-Object System.Windows.Forms.MonthCalendar 
$objCalendar.ShowTodayCircle = $False
$objCalendar.MaxSelectionCount = 1
$objForm.Controls.Add($objCalendar) 

$objForm.Topmost = $True

$objForm.Add_Shown({$objForm.Activate()})  
[void] $objForm.ShowDialog()
return $script:dtmDate
}

function Copy-SelectedFile
{
    [CmdletBinding()]
    param(
        [parameter(Mandatory = $true, ValueFromPipelineByPropertyName = $true)]
        [ValidateScript({Test-Path -path $_})]
        [string]$SourcePath,

        [parameter(Mandatory = $true, ValueFromPipelineByPropertyName = $true)]
        [string]$DestinationPath,

        [parameter(Mandatory = $true, ValueFromPipelineByPropertyName = $true)]
        [System.Management.Automation.PSCredential]$Credential = (Get-Credential), 

        [parameter(ValueFromPipelineByPropertyName = $true)]
        [datetime]$MinimumAgeTime = ([datetime]::now), 

        [parameter(ValueFromPipelineByPropertyName = $true)]
        [datetime]$MaximumAgeTime
    )

    $Files = Get-ChildItem -Path $SourcePath | Where-Object { $_.LastWriteTime -gt $MinimumAgeTime -and $_.LastWriteTime -lt $MaximumAgeTime}
    
    $Destination = New-Item -Path $DestinationPath -ItemType Directory -Name $MinimumAgeTime.ToString('ddMMyy')
    Copy-Item -Path $Files -Destination $Destination -Credential $Credential -Recurse
}

# --------- End of Definitions ---------------
Copy-SelectedFile -MinimumAgeTime (Prompt-For-Date "Please select minumum date") `
                  -MaximumAgeTime (Prompt-For-Date "Please select maximum date") `
                  C:\Test\ D:\Test\

Open in new window

AlexSenior Infrastructure Analyst

Author

Commented:
Oh my... oh my.... GOOODNESS!!!!!

Key reference to Boku no hero acadamia...

Thank you, that's perfect.... You... Little... LEGEND!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial