20210927

Powershellで、カレンダーから選択した範囲内の日付を表示

書きかけだけどメモとして。
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing

$form = New-Object Windows.Forms.Form -Property @{
    StartPosition = [Windows.Forms.FormStartPosition]::CenterScreen
    Size          = New-Object Drawing.Size 400, 230
    Text          = 'Shiftキーを押して、解析対象の範囲を選択'
    Topmost       = $true
}


$calendar = New-Object Windows.Forms.MonthCalendar -Property @{
    ShowTodayCircle   = $false
    MaxSelectionCount = 365
}
$form.Controls.Add($calendar)


$okButton = New-Object Windows.Forms.Button -Property @{
    Location     = New-Object Drawing.Point 38, 165
    Size         = New-Object Drawing.Size 75, 23
    Text         = 'OK'
    DialogResult = [Windows.Forms.DialogResult]::OK
}

$form.AcceptButton = $okButton
$form.Controls.Add($okButton)


$cancelButton = New-Object Windows.Forms.Button -Property @{
    Location     = New-Object Drawing.Point 113, 165
    Size         = New-Object Drawing.Size 75, 23
    Text         = 'Cancel'
    DialogResult = [Windows.Forms.DialogResult]::Cancel
}

$form.CancelButton = $cancelButton
$form.Controls.Add($cancelButton)


$result = $form.ShowDialog()

if ($result -eq [Windows.Forms.DialogResult]::OK) {
    $date = $calendar.SelectionRange
    
    $start = $date.start.ToShortDateString()
    $end= $date.end.ToShortDateString()
    
    #$diff= New-TimeSpan -Start $start -End $end
    $diff= $date.end - $date.start
    $diff2= $diff.Days
    Write-Output "Time difference is: $diff2"
    
    for ($i=0; $i -lt $diff2+1 ; $i++){
        #Write-Host "Date selected: $($date.ToString())"
        Write-Host "Date selected: $($date.start.addDays($i).ToString())"
        #Write-Output ("count:"+$i)
    }
}


$wsobj = new-object -comobject wscript.shell
$result = $wsobj.popup("Finished.")

posted by yuchan at 05:14 | Comment(5) | Windows