none
Cannot index into a null array RRS feed

  • Question

  • Hi

    I have a script which takes user inputs and sets expiry in AD for a user. I am getting an error while clicking the Select Expiry date button.

    #~~< Button4 >~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    $Button4 = New-Object System.Windows.Forms.Button
    $Button4.Font = New-Object System.Drawing.Font("Tahoma", 9.75, [System.Drawing.FontStyle]::Regular, [System.Drawing.GraphicsUnit]::Point, ([System.Byte](0)))
    $Button4.Location = New-Object System.Drawing.Point(480, 169)
    $Button4.Size = New-Object System.Drawing.Size(144, 23)
    $Button4.TabIndex = 13
    $Button4.Text = "Select Expiry Date"
    $Button4.UseVisualStyleBackColor = $true
    $Button4.add_Click({Button4Click($Button4)})
    function Button4Click( $object ){
    
    $dateTimeString = [regex]::Matches($MonthCalendar1, '(\d\d/\d\d/\d\d\d\d.+): ')[0].Groups[1].Value
    $convertedDate = $dateTimeString -replace " 00:00:00, End", " 23:59:00"
    $textbox9.Text = "$convertedDate"
    $textbox11.Text = "False"

    I get the below errors while clicking the Select Expiry Date

    Cannot index into a null array.
    At D:\Scripts\SAMScripts\User-Expiry-Tool\Newuserexpiry.ps1:487 char:1
    + $dateTimeString = [regex]::Matches($MonthCalendar1, '(\d\d/\d\d/\d\d\ ...
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
        + FullyQualifiedErrorId : NullArray
    
    Get-Date : Cannot bind parameter 'Date'. Cannot convert value "" to type "System.DateTime". Error: "String was not
    recognized as a valid DateTime."
    At D:\Scripts\SAMScripts\User-Expiry-Tool\Newuserexpiry.ps1:757 char:22
    + $Newtime = (Get-Date $convertedDate).AddHours(0).ToString("HH:mm:ss")
    +                      ~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidArgument: (:) [Get-Date], ParameterBindingException
        + FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Microsoft.PowerShell.Commands.GetDateCommand
    
    Get-Date : Cannot bind parameter 'Date'. Cannot convert value "" to type "System.DateTime". Error: "String was not
    recognized as a valid DateTime."
    At D:\Scripts\SAMScripts\User-Expiry-Tool\Newuserexpiry.ps1:759 char:22
    + $newdate = (Get-Date $convertedDate).AddDays(0).ToString("dd/MM/yyyy" ...
    +                      ~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidArgument: (:) [Get-Date], ParameterBindingException
        + FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Microsoft.PowerShell.Commands.GetDateCommand
    
    Get-Date : The input object cannot be bound to any parameters for the command either because the command does not take
    pipeline input or the input and its properties do not match any of the parameters that take pipeline input.
    At D:\Scripts\SAMScripts\User-Expiry-Tool\Newuserexpiry.ps1:760 char:19
    + $DST = $newdate | Get-Date -Format "MM/dd/yyyy HH:mm:ss"
    +                   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidArgument: (:) [Get-Date], ParameterBindingException
        + FullyQualifiedErrorId : InputObjectNotBound,Microsoft.PowerShell.Commands.GetDateCommand

    What i did to troubleshoot - Try to print the $MonthCalendar1 and found no value was passed. I passed a static value (getdate) and was able to get the expiry displayed on the GUI. But still the expiry is not set for the user.

    Any suggestions to further troubleshoot or fix the issue would be appreciated.


    Justin

    Monday, September 9, 2019 2:46 PM

All replies

  • Like the error sys - you cannot index into a null value.

    $dateTimeString = [regex]::Matches($MonthCalendar1, '(\d\d/\d\d/\d\d\d\d.+): ')[0].Groups[1].Value

    When the "regex" returns nothing then you have a null value and the indexing fails.  Change the code to correctly test the results of the regex.

    This is the better way to use regex in PowerShell,

    if($MonthCalendar1 -match '(\d\d/\d\d/\d\d\d\d.+): '){
        $matches[1]
    }else{
        # no match
    }
    


    \_(ツ)_/

    Monday, September 9, 2019 5:13 PM
  • Nothing in your code snippets show how you process/calculate $MonthCalendar1. If the regex fails, you will not be able to reference the groups.value. You have to validate your input. 

    Here is a sample.

    function RegexTest() {
    "Testing $MonthCalendar1"
    $rc = [regex]::Matches($MonthCalendar1, '(\d\d/\d\d/\d\d\d\d.+): ')
    if ($rc.count -eq 0) {
       'A valid date was not found in your input data.'
       return
    }
    
    $rc.Groups[1].Value
    
    }
    
    $MonthCalendar1 = '01/01/2020 : hh:mm'
    RegexTest
    
    $MonthCalendar1 = 'yufkddkhgdjkjdk'
    RegexTest
    
    $MonthCalendar1 = '01/01/2020 : yufkddkhgdjkjdk  '
    RegexTest
    
    

    Testing 01/01/2020 : hh:mm
    01/01/2020
    Testing yufkddkhgdjkjdk
    A valid date was not found in your input data.
    Testing 01/01/2020 : yufkddkhgdjkjdk 
    01/01/2020

    Monday, September 9, 2019 5:17 PM
  • Hi jrv

    I do not get that error now. But what i am concerned is that from the button click the date is not getting passed to the $MonthCalendar1.

    #~~< MonthCalendar1 >~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    $MonthCalendar1 = New-Object System.Windows.Forms.MonthCalendar
    $MonthCalendar1.Location = New-Object System.Drawing.Point(12, 27)
    $MonthCalendar1.MaxSelectionCount = 1
    $MonthCalendar1.TabIndex = 0
    $GroupBox2.Controls.Add($Button1)
    $GroupBox2.Controls.Add($Label11)
    $GroupBox2.Controls.Add($TextBox11)
    $GroupBox2.Controls.Add($Label1)
    $GroupBox2.Controls.Add($TextBox10)
    $GroupBox2.Controls.Add($ComboBox1)
    $GroupBox2.Controls.Add($Button4)
    $GroupBox2.Controls.Add($TextBox9)
    $GroupBox2.Controls.Add($TextBox8)
    $GroupBox2.Controls.Add($Label10)
    $GroupBox2.Controls.Add($Label9)
    $GroupBox2.Controls.Add($Label7)
    $GroupBox2.Controls.Add($MonthCalendar1)

    if (($ComboBox1.SelectedIndex) -eq "14") # London [DAYLIGHT SAVING]
    {
    ##############################################
    #             LONDON                         #
    ##############################################
    
    if ($MonthCalendar1 -match "2018") {$2018 = ($DSTstart = Get-Date ([Datetime]"3/24/2018 01:00:00") -Format "MM/dd/yyyy HH:mm:ss"), ($DSTend = Get-Date ([Datetime]"10/28/2018 02:00:00") -Format "MM/dd/yyyy HH:mm:ss")}
    if ($MonthCalendar1 -match "2019") {$2019 = ($DSTstart = Get-Date ([Datetime]"3/31/2018 01:00:00") -Format "MM/dd/yyyy HH:mm:ss"), ($DSTend = Get-Date ([Datetime]"10/27/2018 02:00:00") -Format "MM/dd/yyyy HH:mm:ss")}
    if ($MonthCalendar1 -match "2020") {$2020 = ($DSTstart = Get-Date ([Datetime]"3/29/2018 01:00:00") -Format "MM/dd/yyyy HH:mm:ss"), ($DSTend = Get-Date ([Datetime]"10/25/2018 02:00:00") -Format "MM/dd/yyyy HH:mm:ss")}
    if ($MonthCalendar1 -match "2021") {$2021 = ($DSTstart = Get-Date ([Datetime]"3/28/2018 01:00:00") -Format "MM/dd/yyyy HH:mm:ss"), ($DSTend = Get-Date ([Datetime]"10/31/2018 02:00:00") -Format "MM/dd/yyyy HH:mm:ss")}
    if ($MonthCalendar1 -match "2022") {$2022 = ($DSTstart = Get-Date ([Datetime]"3/27/2018 01:00:00") -Format "MM/dd/yyyy HH:mm:ss"), ($DSTend = Get-Date ([Datetime]"10/30/2018 02:00:00") -Format "MM/dd/yyyy HH:mm:ss")}
    if ($MonthCalendar1 -match "2023") {$2023 = ($DSTstart = Get-Date ([Datetime]"3/26/2018 01:00:00") -Format "MM/dd/yyyy HH:mm:ss"), ($DSTend = Get-Date ([Datetime]"10/29/2018 02:00:00") -Format "MM/dd/yyyy HH:mm:ss")}
    if ($MonthCalendar1 -match "2024") {$2024 = ($DSTstart = Get-Date ([Datetime]"3/31/2018 01:00:00") -Format "MM/dd/yyyy HH:mm:ss"), ($DSTend = Get-Date ([Datetime]"10/27/2018 02:00:00") -Format "MM/dd/yyyy HH:mm:ss")}
    if ($MonthCalendar1 -match "2025") {$2025 = ($DSTstart = Get-Date ([Datetime]"3/30/2018 01:00:00") -Format "MM/dd/yyyy HH:mm:ss"), ($DSTend = Get-Date ([Datetime]"10/26/2018 02:00:00") -Format "MM/dd/yyyy HH:mm:ss")}
    
    $Office = "London"
    $Newtime = (Get-Date $convertedDate).AddHours(0).ToString("HH:mm:ss")
    $convertedDate = $dateTimeString -replace " 00:00:00, End", " $newtime"
    $newdate = (Get-Date $convertedDate).AddDays(0).ToString("dd/MM/yyyy")
    $DST = $newdate | Get-Date -Format "MM/dd/yyyy HH:mm:ss"
    
    if ($DST -ge $DSTstart -and $DST -le $DSTend)
    {
    $Newtime = (Get-Date $convertedDate).AddHours(1).ToString("HH:mm:ss")
    $newdate = (Get-Date $convertedDate).AddDays(1).ToString("dd/MM/yyyy")
    $textbox11.Text = "True"
    }
    ##############################################
    }

    I do not get a match as the $monthcalendar is a null value. Also i get the below error.

    Get-Date : Cannot bind parameter 'Date'. Cannot convert value "" to type "System.DateTime". Error: "String was not 
    recognized as a valid DateTime."
    At C:\Users\ID\Documents\test expiry.ps1:762 char:22
    + $Newtime = (Get-Date $convertedDate).AddHours(0).ToString("HH:mm:ss")
    +                      ~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidArgument: (:) [Get-Date], ParameterBindingException
        + FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Microsoft.PowerShell.Commands.GetDateCommand
     
    Get-Date : Cannot bind parameter 'Date'. Cannot convert value "" to type "System.DateTime". Error: "String was not 
    recognized as a valid DateTime."
    At C:\Users\ID\Documents\test expiry.ps1:764 char:22
    + $newdate = (Get-Date $convertedDate).AddDays(0).ToString("dd/MM/yyyy" ...
    +                      ~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidArgument: (:) [Get-Date], ParameterBindingException
        + FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Microsoft.PowerShell.Commands.GetDateCommand
     
    Get-Date : The input object cannot be bound to any parameters for the command either because the command does not 
    take pipeline input or the input and its properties do not match any of the parameters that take pipeline input.
    At C:\Users\ID\Documents\test expiry.ps1:765 char:19
    + $DST = $newdate | Get-Date -Format "MM/dd/yyyy HH:mm:ss"
    +                   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidArgument: (:) [Get-Date], ParameterBindingException
        + FullyQualifiedErrorId : InputObjectNotBound,Microsoft.PowerShell.Commands.GetDateCommand


    Justin

    Tuesday, September 10, 2019 5:26 AM
  • Thank you. With the piece of condition provided by jrv i was able to check if the value $MonthCalendar1 is a null value and found its not passing any value.

    I have pasted the code snippet for your reference.


    Justin


    • Edited by JS2206 Tuesday, September 10, 2019 5:30 AM
    Tuesday, September 10, 2019 5:30 AM
  • If the"-match" returns false then no match was found.  That is an issue with what you are trying to match to.

    All of your code is quite odd.  It appears that you have some lack of understanding about how to write PS cod.

    Example here is how to code this:

    switch ($monthcalendar1.SelectionStart.Date.Year){
        2018 {
            $2018 = (
                $DSTstart = [Datetime]'3/24/2018 01:00:00').ToString('MM/dd/yyyy HH:mm:ss'),
                $DSTend = [Datetime]'10/28/2018 02:00:00').ToString('MM/dd/yyyy HH:mm:ss')
            )
        }
        2019 {
            # set dates
        }
        2020 {
            $2020 = (
                $DSTstart = etc,
                $DSTend = etc
            )
        }
    }

    There is no need to parse the calendar object as a string.  The switch statement is made just for this situation.

    The overall design and putpose makes little sense.  This should all be just a table that you use to llokup the settings.



    \_(ツ)_/

    Tuesday, September 10, 2019 5:55 AM
  • To determine is a time is in our out od daylight savings takes one line of codeL'

    $monthcalendar1.SelectionStart.Date.IsDaylightSavingTime()

    The table is built into Windows and respects locale and culture.


    \_(ツ)_/


    • Edited by jrv Tuesday, September 10, 2019 6:02 AM
    Tuesday, September 10, 2019 6:01 AM
  • Find DST start and end for any year.

    # find DST start for the year
    for($i=0;$i -lt 365;$i++){
        $DSTStart = ([datetime]'1-1-2019 03:00:00').AddDays($i)
        if ($DSTStart.IsDaylightSavingTime()) {
            break
        }
    }
    
    # find DST end for the year
    for ($i = 0; $i -lt 365; $i++) {
        $DSTEnd = $DSTStart.AddDays($i)
        if(-not $DSTEnd.IsDaylightSavingTime()) {
            break
        }
    }
    $DSTStart
    $DSTEnd


    \_(ツ)_/



    • Edited by jrv Tuesday, September 10, 2019 6:22 AM
    Tuesday, September 10, 2019 6:21 AM
  • Now we can create a function that returns both for any year.

    function Get-DSTForYear{
        Param(
            $year = [datetime]::Today.Year
        )
        for($i=0;$i -lt 100;$i++){
            $DSTStart = ([datetime]"1-1-$year 03:00:00").AddDays($i)
            if ($DSTStart.IsDaylightSavingTime()) {
                
                break
            }
        }
    
        # find DST end for the year
        for ($i = 0; $i -lt 265; $i++) {
            $DSTEnd = $DSTStart.AddDays($i)
            if(-not $DSTEnd.IsDaylightSavingTime()) {
                break
            }
        }
        $DSTStart
        $DSTend
    }
    $start, $end = Get-DSTForYear 2018


    \_(ツ)_/


    • Edited by jrv Tuesday, September 10, 2019 6:32 AM
    Tuesday, September 10, 2019 6:29 AM
  • Hi Jrv

    Thank you. I will try and include this to the script. But my initial issue remains unsolved and I do not how to fix it.

    The $monthcalendar selection date is not being matched and ends up in an error.


    Justin

    Tuesday, September 10, 2019 3:11 PM
  • What are you trying to match?  Remember the calendar is an object and not a string.  It cannot be managed as a string.


    \_(ツ)_/

    Tuesday, September 10, 2019 5:07 PM
  • I am trying to match the Selectionstartdate from the Date picker form. But for some reason the date is not getting selected on the buttonclick.

    #region Constructor
    
    [void][System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
    [void][System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
    
    #endregion
    
    #region Post-Constructor Custom Code
    
    #endregion
    
    #region Form Creation
    #Warning: It is recommended that changes inside this region be handled using the ScriptForm Designer.
    #When working with the ScriptForm designer this region and any changes within may be overwritten.
    #~~< Form1 >~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    $Form1 = New-Object System.Windows.Forms.Form
    $Form1.ClientSize = New-Object System.Drawing.Size(747, 474)
    $Form1.FormBorderStyle = [System.Windows.Forms.FormBorderStyle]::Fixed3D
    $Form1.MaximizeBox = $false
    $Form1.MinimizeBox = $false
    $Form1.Text = "AD Account Expiration Tool v1.0"
    $Form1.BackColor = [System.Drawing.SystemColors]::Control
    #~~< Button5 >~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    $Button5 = New-Object System.Windows.Forms.Button
    $Button5.Location = New-Object System.Drawing.Point(24, 445)
    $Button5.Size = New-Object System.Drawing.Size(125, 23)
    $Button5.TabIndex = 7
    $Button5.Text = "Remove Expiry Date"
    $Button5.UseVisualStyleBackColor = $true
    $Button5.add_Click({Button5Click($Button5)})
    #~~< Button2 >~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    $Button2 = New-Object System.Windows.Forms.Button
    $Button2.Location = New-Object System.Drawing.Point(651, 445)
    $Button2.Size = New-Object System.Drawing.Size(75, 23)
    $Button2.TabIndex = 3
    $Button2.Text = "Exit"
    $Button2.UseVisualStyleBackColor = $true
    $Button2.add_Click({Button2Click($Button2)})
    #~~< Button3 >~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    $Button3 = New-Object System.Windows.Forms.Button
    $Button3.Location = New-Object System.Drawing.Point(492, 38)
    $Button3.Size = New-Object System.Drawing.Size(75, 23)
    $Button3.TabIndex = 4
    $Button3.Text = "Check"
    $Button3.UseVisualStyleBackColor = $true
    $Button3.add_Click({Button3Click($Button3)})
    #~~< Label8 >~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    $Label8 = New-Object System.Windows.Forms.Label
    $Label8.Font = New-Object System.Drawing.Font("Tahoma", 9.75, [System.Drawing.FontStyle]::Bold, [System.Drawing.GraphicsUnit]::Point, ([System.Byte](0)))
    $Label8.Location = New-Object System.Drawing.Point(333, 13)
    $Label8.Size = New-Object System.Drawing.Size(153, 23)
    $Label8.TabIndex = 0
    $Label8.Text = "Enter users login ID:"
    $Label8.BackColor = [System.Drawing.SystemColors]::WindowText
    $Label8.ForeColor = [System.Drawing.Color]::White
    #~~< GroupBox2 >~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    $GroupBox2 = New-Object System.Windows.Forms.GroupBox
    $GroupBox2.Font = New-Object System.Drawing.Font("Microsoft Sans Serif", 9.75, [System.Drawing.FontStyle]::Bold, [System.Drawing.GraphicsUnit]::Point, ([System.Byte](0)))
    $GroupBox2.Location = New-Object System.Drawing.Point(12, 236)
    $GroupBox2.Size = New-Object System.Drawing.Size(727, 203)
    $GroupBox2.TabIndex = 1
    $GroupBox2.TabStop = $false
    $GroupBox2.Text = "AD Account Expiry Details"
    #~~< Button1 >~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    $Button1 = New-Object System.Windows.Forms.Button
    $Button1.Font = New-Object System.Drawing.Font("Tahoma", 9.75, [System.Drawing.FontStyle]::Regular, [System.Drawing.GraphicsUnit]::Point, ([System.Byte](0)))
    $Button1.Location = New-Object System.Drawing.Point(639, 169)
    $Button1.Size = New-Object System.Drawing.Size(75, 23)
    $Button1.TabIndex = 0
    $Button1.Text = "Apply"
    $Button1.UseVisualStyleBackColor = $true
    $Button1.add_Click({Button1Click($Button1)})
    #~~< Label11 >~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    $Label11 = New-Object System.Windows.Forms.Label
    $Label11.Font = New-Object System.Drawing.Font("Tahoma", 9.75, [System.Drawing.FontStyle]::Regular, [System.Drawing.GraphicsUnit]::Point, ([System.Byte](0)))
    $Label11.Location = New-Object System.Drawing.Point(281, 115)
    $Label11.Size = New-Object System.Drawing.Size(169, 23)
    $Label11.TabIndex = 38
    $Label11.Text = "Daylight Saving Time:"
    #~~< TextBox11 >~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    $TextBox11 = New-Object System.Windows.Forms.TextBox
    $TextBox11.Location = New-Object System.Drawing.Point(480, 112)
    $TextBox11.ReadOnly = $true
    $TextBox11.Size = New-Object System.Drawing.Size(234, 22)
    $TextBox11.TabIndex = 37
    $TextBox11.Text = ""
    #~~< Label1 >~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    $Label1 = New-Object System.Windows.Forms.Label
    $Label1.Font = New-Object System.Drawing.Font("Tahoma", 9.75, [System.Drawing.FontStyle]::Regular, [System.Drawing.GraphicsUnit]::Point, ([System.Byte](0)))
    $Label1.Location = New-Object System.Drawing.Point(281, 143)
    $Label1.Size = New-Object System.Drawing.Size(193, 23)
    $Label1.TabIndex = 36
    $Label1.Text = "UTC Actual AD Applied Time:"
    #~~< TextBox10 >~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    $TextBox10 = New-Object System.Windows.Forms.TextBox
    $TextBox10.Location = New-Object System.Drawing.Point(480, 140)
    $TextBox10.ReadOnly = $true
    $TextBox10.Size = New-Object System.Drawing.Size(234, 22)
    $TextBox10.TabIndex = 35
    $TextBox10.Text = ""
    #~~< ComboBox1 >~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    $ComboBox1 = New-Object System.Windows.Forms.ComboBox
    $ComboBox1.FormattingEnabled = $true
    $ComboBox1.Location = New-Object System.Drawing.Point(480, 26)
    $ComboBox1.Size = New-Object System.Drawing.Size(234, 24)
    $ComboBox1.TabIndex = 34
    $ComboBox1.Text = "Select Office"
    $ComboBox1.Items.AddRange([System.Object[]](@("Abu Dhabi", "Amsterdam", "Bangkok", "Barcelona", "Beijing", "Brussels", "Bucharest", "Casablanca", "Delhi", "Dubai", "Düsseldorf", "Frankfurt", "Hong Kong", "Istanbul", "London", "Luxembourg", "Madrid", "Milan", "Moscow", "München", "New York", "Paris", "Perth", "Prague", "Rome", "Sao Paulo", "Seoul", "Shanghai", "Singapore", "Sydney", "Tokyo", "Warsaw", "Washington")))
    $ComboBox1.SelectedIndex = -1
    #~~< Button4 >~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    $Button4 = New-Object System.Windows.Forms.Button
    $Button4.Font = New-Object System.Drawing.Font("Tahoma", 9.75, [System.Drawing.FontStyle]::Regular, [System.Drawing.GraphicsUnit]::Point, ([System.Byte](0)))
    $Button4.Location = New-Object System.Drawing.Point(480, 169)
    $Button4.Size = New-Object System.Drawing.Size(144, 23)
    $Button4.TabIndex = 13
    $Button4.Text = "Select Expiry Date"
    $Button4.UseVisualStyleBackColor = $true
    $Button4.add_Click({Button4Click($Button4)})
    #~~< TextBox9 >~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    $TextBox9 = New-Object System.Windows.Forms.TextBox
    $TextBox9.Location = New-Object System.Drawing.Point(480, 84)
    $TextBox9.ReadOnly = $true
    $TextBox9.Size = New-Object System.Drawing.Size(234, 22)
    $TextBox9.TabIndex = 12
    $TextBox9.Text = ""
    #~~< TextBox8 >~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    $TextBox8 = New-Object System.Windows.Forms.TextBox
    $TextBox8.Location = New-Object System.Drawing.Point(480, 56)
    $TextBox8.ReadOnly = $true
    $TextBox8.Size = New-Object System.Drawing.Size(234, 22)
    $TextBox8.TabIndex = 11
    $TextBox8.Text = ""
    #~~< Label10 >~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    $Label10 = New-Object System.Windows.Forms.Label
    $Label10.Font = New-Object System.Drawing.Font("Tahoma", 9.75, [System.Drawing.FontStyle]::Regular, [System.Drawing.GraphicsUnit]::Point, ([System.Byte](0)))
    $Label10.Location = New-Object System.Drawing.Point(281, 56)
    $Label10.Size = New-Object System.Drawing.Size(169, 23)
    $Label10.TabIndex = 10
    $Label10.Text = "Current Expiry Date:"
    #~~< Label9 >~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    $Label9 = New-Object System.Windows.Forms.Label
    $Label9.Font = New-Object System.Drawing.Font("Tahoma", 9.75, [System.Drawing.FontStyle]::Regular, [System.Drawing.GraphicsUnit]::Point, ([System.Byte](0)))
    $Label9.Location = New-Object System.Drawing.Point(281, 84)
    $Label9.Size = New-Object System.Drawing.Size(169, 23)
    $Label9.TabIndex = 9
    $Label9.Text = "New Expiry Date:"
    #~~< Label7 >~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    $Label7 = New-Object System.Windows.Forms.Label
    $Label7.Font = New-Object System.Drawing.Font("Tahoma", 9.75, [System.Drawing.FontStyle]::Regular, [System.Drawing.GraphicsUnit]::Point, ([System.Byte](0)))
    $Label7.Location = New-Object System.Drawing.Point(281, 26)
    $Label7.Size = New-Object System.Drawing.Size(169, 23)
    $Label7.TabIndex = 2
    $Label7.Text = "Office:"
    #~~< MonthCalendar1 >~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    $MonthCalendar1 = New-Object System.Windows.Forms.MonthCalendar
    $MonthCalendar1.Location = New-Object System.Drawing.Point(12, 27)
    $MonthCalendar1.MaxSelectionCount = 1
    $MonthCalendar1.TabIndex = 0
    $GroupBox2.Controls.Add($Button1)
    $GroupBox2.Controls.Add($Label11)
    $GroupBox2.Controls.Add($TextBox11)
    $GroupBox2.Controls.Add($Label1)
    $GroupBox2.Controls.Add($TextBox10)
    $GroupBox2.Controls.Add($ComboBox1)
    $GroupBox2.Controls.Add($Button4)
    $GroupBox2.Controls.Add($TextBox9)
    $GroupBox2.Controls.Add($TextBox8)
    $GroupBox2.Controls.Add($Label10)
    $GroupBox2.Controls.Add($Label9)
    $GroupBox2.Controls.Add($Label7)
    $GroupBox2.Controls.Add($MonthCalendar1)
    #~~< GroupBox1 >~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    $GroupBox1 = New-Object System.Windows.Forms.GroupBox
    $GroupBox1.Font = New-Object System.Drawing.Font("Microsoft Sans Serif", 9.75, [System.Drawing.FontStyle]::Bold, [System.Drawing.GraphicsUnit]::Point, ([System.Byte](0)))
    $GroupBox1.Location = New-Object System.Drawing.Point(12, 82)
    $GroupBox1.Size = New-Object System.Drawing.Size(727, 137)
    $GroupBox1.TabIndex = 0
    $GroupBox1.TabStop = $false
    $GroupBox1.Text = "AD User Account Details"
    $GroupBox1.BackColor = [System.Drawing.SystemColors]::Control
    #~~< TextBox6 >~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    $TextBox6 = New-Object System.Windows.Forms.TextBox
    $TextBox6.Location = New-Object System.Drawing.Point(480, 61)
    $TextBox6.ReadOnly = $true
    $TextBox6.Size = New-Object System.Drawing.Size(234, 22)
    $TextBox6.TabIndex = 7
    $TextBox6.Text = ""
    #~~< TextBox5 >~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    $TextBox5 = New-Object System.Windows.Forms.TextBox
    $TextBox5.Location = New-Object System.Drawing.Point(480, 27)
    $TextBox5.ReadOnly = $true
    $TextBox5.Size = New-Object System.Drawing.Size(234, 22)
    $TextBox5.TabIndex = 9
    $TextBox5.Text = ""
    #~~< TextBox4 >~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    $TextBox4 = New-Object System.Windows.Forms.TextBox
    $TextBox4.Font = New-Object System.Drawing.Font("Tahoma", 7.0, [System.Drawing.FontStyle]::Bold, [System.Drawing.GraphicsUnit]::Point, ([System.Byte](0)))
    $TextBox4.Location = New-Object System.Drawing.Point(121, 94)
    $TextBox4.ReadOnly = $true
    $TextBox4.Size = New-Object System.Drawing.Size(593, 19)
    $TextBox4.TabIndex = 8
    $TextBox4.Text = ""
    #~~< TextBox3 >~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    $TextBox3 = New-Object System.Windows.Forms.TextBox
    $TextBox3.Location = New-Object System.Drawing.Point(121, 61)
    $TextBox3.ReadOnly = $true
    $TextBox3.Size = New-Object System.Drawing.Size(234, 22)
    $TextBox3.TabIndex = 7
    $TextBox3.Text = ""
    #~~< TextBox2 >~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    $TextBox2 = New-Object System.Windows.Forms.TextBox
    $TextBox2.Location = New-Object System.Drawing.Point(121, 27)
    $TextBox2.ReadOnly = $true
    $TextBox2.Size = New-Object System.Drawing.Size(234, 22)
    $TextBox2.TabIndex = 6
    $TextBox2.Text = ""
    #~~< Label6 >~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    $Label6 = New-Object System.Windows.Forms.Label
    $Label6.Font = New-Object System.Drawing.Font("Tahoma", 9.75, [System.Drawing.FontStyle]::Regular, [System.Drawing.GraphicsUnit]::Point, ([System.Byte](0)))
    $Label6.Location = New-Object System.Drawing.Point(374, 64)
    $Label6.Size = New-Object System.Drawing.Size(100, 23)
    $Label6.TabIndex = 5
    $Label6.Text = "Employee ID:"
    #~~< Label5 >~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    $Label5 = New-Object System.Windows.Forms.Label
    $Label5.Font = New-Object System.Drawing.Font("Tahoma", 9.75, [System.Drawing.FontStyle]::Regular, [System.Drawing.GraphicsUnit]::Point, ([System.Byte](0)))
    $Label5.Location = New-Object System.Drawing.Point(374, 30)
    $Label5.Size = New-Object System.Drawing.Size(100, 23)
    $Label5.TabIndex = 4
    $Label5.Text = "Surname:"
    #~~< Label4 >~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    $Label4 = New-Object System.Windows.Forms.Label
    $Label4.Font = New-Object System.Drawing.Font("Tahoma", 9.75, [System.Drawing.FontStyle]::Regular, [System.Drawing.GraphicsUnit]::Point, ([System.Byte](0)))
    $Label4.Location = New-Object System.Drawing.Point(15, 97)
    $Label4.Size = New-Object System.Drawing.Size(100, 23)
    $Label4.TabIndex = 3
    $Label4.Text = "User OU:"
    #~~< Label3 >~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    $Label3 = New-Object System.Windows.Forms.Label
    $Label3.Font = New-Object System.Drawing.Font("Tahoma", 9.75, [System.Drawing.FontStyle]::Regular, [System.Drawing.GraphicsUnit]::Point, ([System.Byte](0)))
    $Label3.Location = New-Object System.Drawing.Point(15, 64)
    $Label3.Size = New-Object System.Drawing.Size(100, 23)
    $Label3.TabIndex = 2
    $Label3.Text = "Display Name:"
    #~~< Label2 >~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    $Label2 = New-Object System.Windows.Forms.Label
    $Label2.Font = New-Object System.Drawing.Font("Tahoma", 9.75, [System.Drawing.FontStyle]::Regular, [System.Drawing.GraphicsUnit]::Point, ([System.Byte](0)))
    $Label2.Location = New-Object System.Drawing.Point(15, 30)
    $Label2.Size = New-Object System.Drawing.Size(100, 23)
    $Label2.TabIndex = 1
    $Label2.Text = "Forename:"
    $GroupBox1.Controls.Add($TextBox6)
    $GroupBox1.Controls.Add($TextBox5)
    $GroupBox1.Controls.Add($TextBox4)
    $GroupBox1.Controls.Add($TextBox3)
    $GroupBox1.Controls.Add($TextBox2)
    $GroupBox1.Controls.Add($Label6)
    $GroupBox1.Controls.Add($Label5)
    $GroupBox1.Controls.Add($Label4)
    $GroupBox1.Controls.Add($Label3)
    $GroupBox1.Controls.Add($Label2)
    #~~< TextBox1 >~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    $TextBox1 = New-Object System.Windows.Forms.TextBox
    $TextBox1.Location = New-Object System.Drawing.Point(492, 12)
    $TextBox1.MaxLength = 12
    $TextBox1.Size = New-Object System.Drawing.Size(214, 20)
    $TextBox1.TabIndex = 2
    $TextBox1.Text = ""
    
    #endregion
    $Form1.Controls.Add($Button5)
    $Form1.Controls.Add($Button2)
    $Form1.Controls.Add($Button3)
    $Form1.Controls.Add($Label8)
    $Form1.Controls.Add($GroupBox2)
    $Form1.Controls.Add($GroupBox1)
    $Form1.Controls.Add($TextBox1)
    
    endregion
    
    #region Custom Code
    
    #endregion
    
    #region Event Loop
    
    function Main{
    	[System.Windows.Forms.Application]::EnableVisualStyles()
    	[System.Windows.Forms.Application]::Run($Form1)
    }
    
    #endregion
    
    #endregion
    
    #region Event Handlers
    
    function Button3Click( $object ){
    $textbox11.Text = ""
    $User = $TextBox1.Text
    $textbox2.Text = (Get-ADUser -Identity $user).givenname
    $textbox3.Text = (Get-ADUser -Identity $user -properties *).displayname
    $textbox4.Text = (Get-ADUser -Identity $user -properties *).DistinguishedName  
    $textbox5.Text = (Get-ADUser -Identity $user).Surname
    $textbox6.Text = (Get-ADUser -Identity $user -properties *).EmployeeID 
    $combobox1.Text = (Get-ADUser -Identity $user -properties *).Office
    $textbox8.Text = (Get-ADUser -Identity $user -properties *).AccountExpirationDate
    if (($textbox8.text) -match "/")
    {
    $textbox8.Text = (Get-ADUser -Identity $user -properties *).AccountExpirationDate | Get-Date -Format "dd/MM/yyyy HH:mm:ss"
    }
    else
    {
    $textbox8.Text = "No account expiration date set"
    }
    }
    
    function Button4Click( $object ){
    
    $dateTimeString = [regex]::Matches($MonthCalendar1, '(\d\d/\d\d/\d\d\d\d.+): ')[0].Groups[1].Value
    $convertedDate = $dateTimeString -replace " 00:00:00, End", " 23:59:00"
    $textbox9.Text = "$convertedDate"
    $textbox11.Text = "False"
    
    if (($ComboBox1.SelectedIndex) -eq "0") # Abu Dhabi [NO DAYLIGHT SAVING]
    {
    ##############################################
    #             ABU DHABI                      #
    ##############################################
    
    $Office = "Abu Dhabi"
    $Newtime = (Get-Date $convertedDate).AddHours(4).ToString("HH:mm:ss")
    $convertedDate = $dateTimeString -replace " 00:00:00, End", " $newtime"
    $newdate = (Get-Date $convertedDate).AddDays(1).ToString("dd/MM/yyyy")
    
    ##############################################
    }
    
    }
    
    $conv = $newdate, $Newtime
    $textbox10.Text = "$conv"
    
    
    
    function Button1Click( $object ){
    $User = $TextBox1.Text
    
    if (($ComboBox1.SelectedIndex -notmatch "-1") -and ($User -gt 3))
    {
    Write-Host "Applying Expiry Date to AD User Account"
    set-ADAccountExpiration "$user" -DateTime $textbox10.Text
    }
    }
    
    
    function Button2Click( $object ){
    [environment]::exit(0)
    }
    
    function Button5Click( $object ){
    $User = $TextBox1.Text
    if ($User -gt 3)
    {
    Write-Host "Removing Expiry Date from AD User Account"
    clear-ADAccountExpiration "$user"
    }
    }
    
    Main # This call must remain below all other event functions
    
    #endregion
    
    

    The error "Cannot index into a null array" if i replace with the below code

    function Button4Click( $object ){
    
    if($MonthCalendar1 -match '(\d\d/\d\d/\d\d\d\d.+): '){
        $matches[1]
        Write-Host $matches[1]
    }else{
        write-host 'no match'
    }

    But not sure why the button click does not return a value.


    Justin

    Wednesday, September 11, 2019 4:50 AM
  • "$monthcalendar1.SelectionStart" is a date.

    Stop thinking in strings.  PowerShel uses objects.  "Dates" are objects and not strings.  You do not need to match anything. 

    You need to stop and think about what you are doing.  Computers manage objects and forms controls are objects not strings.  You cannot do this by trying to match strings.

    Your form is not being built correctly for PowerShell.  I n PowerShell you need to use "ShowDialog()" to use the form or the events will not work correctly.


    \_(ツ)_/

    Wednesday, September 11, 2019 5:06 AM
  • This is how to build a form in PowerShell. It is still not th best but it was what I could do in a few minutes. Notice that the date is correctly selected when you click the calendar. The date is an object and should be managed as an object.

    Add-Type -AssemblyName System.Windows.Forms
    
    [System.Windows.Forms.Application]::EnableVisualStyles()
    
    $Form1 = New-Object System.Windows.Forms.Form
    $Form1.ClientSize = New-Object System.Drawing.Size(747, 474)
    $Form1.FormBorderStyle = [System.Windows.Forms.FormBorderStyle]::Fixed3D
    $Form1.MaximizeBox = $false
    $Form1.MinimizeBox = $false
    $Form1.Text = "AD Account Expiration Tool v1.0"
    $Form1.BackColor = [System.Drawing.SystemColors]::Control
    
    #~~< Button5 >~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    $Button5 = New-Object System.Windows.Forms.Button
    $Button5.Location = New-Object System.Drawing.Point(24, 445)
    $Button5.Size = New-Object System.Drawing.Size(125, 23)
    $Button5.TabIndex = 7
    $Button5.Text = "Remove Expiry Date"
    $Button5.UseVisualStyleBackColor = $true
    $button5_Click = {
        $User = $TextBox1.Text
        if ($User -gt 3) {
            Write-Host "Removing Expiry Date from AD User Account"
            clear-ADAccountExpiration "$user"
        }
    }
    
    $Button5.add_Click($button5_Click)
    
    #~~< Button2 >~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    $Button2 = New-Object System.Windows.Forms.Button
    $Button2.Location = New-Object System.Drawing.Point(651, 445)
    $Button2.Size = New-Object System.Drawing.Size(75, 23)
    $Button2.TabIndex = 3
    $Button2.Text = "Exit"
    $Button2.UseVisualStyleBackColor = $true
    $Button2.add_Click({$Form1.Close()})
    
    #~~< Button3 >~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    $Button3 = New-Object System.Windows.Forms.Button
    $Button3.Location = New-Object System.Drawing.Point(492, 38)
    $Button3.Size = New-Object System.Drawing.Size(75, 23)
    $Button3.TabIndex = 4
    $Button3.Text = "Check"
    $Button3.UseVisualStyleBackColor = $true
    $Button3_Click = {
        $textbox11.Text = ""
        $User = $TextBox1.Text
        $textbox2.Text = (Get-ADUser -Identity $user).givenname
        $textbox3.Text = (Get-ADUser -Identity $user -properties *).displayname
        $textbox4.Text = (Get-ADUser -Identity $user -properties *).DistinguishedName
        $textbox5.Text = (Get-ADUser -Identity $user).Surname
        $textbox6.Text = (Get-ADUser -Identity $user -properties *).EmployeeID
        $combobox1.Text = (Get-ADUser -Identity $user -properties *).Office
        $textbox8.Text = (Get-ADUser -Identity $user -properties *).AccountExpirationDate
        if (($textbox8.text) -match "/") {
            $textbox8.Text = (Get-ADUser -Identity $user -properties *).AccountExpirationDate | Get-Date -Format "dd/MM/yyyy HH:mm:ss"
        } else {
            $textbox8.Text = "No account expiration date set"
        }
    }
    $Button3.add_Click($button3_Click)
    
    #~~< Label8 >~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    $Label8 = New-Object System.Windows.Forms.Label
    $Label8.Font = New-Object System.Drawing.Font("Tahoma", 9.75, [System.Drawing.FontStyle]::Bold, [System.Drawing.GraphicsUnit]::Point, ([System.Byte](0)))
    $Label8.Location = New-Object System.Drawing.Point(333, 13)
    $Label8.Size = New-Object System.Drawing.Size(153, 23)
    $Label8.TabIndex = 0
    $Label8.Text = "Enter users login ID:"
    $Label8.BackColor = [System.Drawing.SystemColors]::WindowText
    $Label8.ForeColor = [System.Drawing.Color]::White
    
    #~~< GroupBox2 >~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    $GroupBox2 = New-Object System.Windows.Forms.GroupBox
    $GroupBox2.Font = New-Object System.Drawing.Font("Microsoft Sans Serif", 9.75, [System.Drawing.FontStyle]::Bold, [System.Drawing.GraphicsUnit]::Point, ([System.Byte](0)))
    $GroupBox2.Location = New-Object System.Drawing.Point(12, 236)
    $GroupBox2.Size = New-Object System.Drawing.Size(727, 203)
    $GroupBox2.TabIndex = 1
    $GroupBox2.TabStop = $false
    $GroupBox2.Text = "AD Account Expiry Details"
    
    #~~< Button1 >~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    $Button1 = New-Object System.Windows.Forms.Button
    $Button1.Font = New-Object System.Drawing.Font("Tahoma", 9.75, [System.Drawing.FontStyle]::Regular, [System.Drawing.GraphicsUnit]::Point, ([System.Byte](0)))
    $Button1.Location = New-Object System.Drawing.Point(639, 169)
    $Button1.Size = New-Object System.Drawing.Size(75, 23)
    $Button1.TabIndex = 0
    $Button1.Text = "Apply"
    $Button1.UseVisualStyleBackColor = $true
    $button1_Click = {
        $User = $TextBox1.Text
        
        if (($ComboBox1.SelectedIndex -notmatch "-1") -and ($User -gt 3)) {
            Write-Host "Applying Expiry Date to AD User Account"
            set-ADAccountExpiration "$user" -DateTime $textbox10.Text
        }
    }
    $Button1.add_Click($button1_Click)
    
    #~~< Label11 >~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    $Label11 = New-Object System.Windows.Forms.Label
    $Label11.Font = New-Object System.Drawing.Font("Tahoma", 9.75, [System.Drawing.FontStyle]::Regular, [System.Drawing.GraphicsUnit]::Point, ([System.Byte](0)))
    $Label11.Location = New-Object System.Drawing.Point(281, 115)
    $Label11.Size = New-Object System.Drawing.Size(169, 23)
    $Label11.TabIndex = 38
    $Label11.Text = "Daylight Saving Time:"
    
    #~~< TextBox11 >~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    $TextBox11 = New-Object System.Windows.Forms.TextBox
    $TextBox11.Location = New-Object System.Drawing.Point(480, 112)
    $TextBox11.ReadOnly = $true
    $TextBox11.Size = New-Object System.Drawing.Size(234, 22)
    $TextBox11.TabIndex = 37
    
    #~~< Label1 >~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    $Label1 = New-Object System.Windows.Forms.Label
    $Label1.Font = New-Object System.Drawing.Font("Tahoma", 9.75, [System.Drawing.FontStyle]::Regular, [System.Drawing.GraphicsUnit]::Point, ([System.Byte](0)))
    $Label1.Location = New-Object System.Drawing.Point(281, 143)
    $Label1.Size = New-Object System.Drawing.Size(193, 23)
    $Label1.TabIndex = 36
    $Label1.Text = "UTC Actual AD Applied Time:"
    
    #~~< TextBox10 >~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    $TextBox10 = New-Object System.Windows.Forms.TextBox
    $TextBox10.Location = New-Object System.Drawing.Point(480, 140)
    $TextBox10.ReadOnly = $true
    $TextBox10.Size = New-Object System.Drawing.Size(234, 22)
    $TextBox10.TabIndex = 35
    
    #~~< ComboBox1 >~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    $ComboBox1 = New-Object System.Windows.Forms.ComboBox
    $ComboBox1.FormattingEnabled = $true
    $ComboBox1.Location = New-Object System.Drawing.Point(480, 26)
    $ComboBox1.Size = New-Object System.Drawing.Size(234, 24)
    $ComboBox1.TabIndex = 34
    $ComboBox1.Text = "Select Office"
    $ComboBox1.Items.AddRange([System.Object[]](@("Abu Dhabi", "Amsterdam", "Bangkok", "Barcelona", "Beijing", "Brussels", "Bucharest", "Casablanca", "Delhi", "Dubai", "Düsseldorf", "Frankfurt", "Hong Kong", "Istanbul", "London", "Luxembourg", "Madrid", "Milan", "Moscow", "München", "New York", "Paris", "Perth", "Prague", "Rome", "Sao Paulo", "Seoul", "Shanghai", "Singapore", "Sydney", "Tokyo", "Warsaw", "Washington")))
    $ComboBox1.SelectedIndex = -1
    
    #~~< Button4 >~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    $Button4 = New-Object System.Windows.Forms.Button
    $Button4.Font = New-Object System.Drawing.Font("Tahoma", 9.75, [System.Drawing.FontStyle]::Regular, [System.Drawing.GraphicsUnit]::Point, ([System.Byte](0)))
    $Button4.Location = New-Object System.Drawing.Point(480, 169)
    $Button4.Size = New-Object System.Drawing.Size(144, 23)
    $Button4.TabIndex = 13
    $Button4.Text = "Select Expiry Date"
    $Button4.UseVisualStyleBackColor = $true
    $button4_Click = {
        
        $MonthCalendar1.SelectionStart
        $convertedDate = $dateTimeString -replace " 00:00:00, End", " 23:59:00"
        $textbox9.Text = "$convertedDate"
        $textbox11.Text = "False"
        
        if ($ComboBox1.SelectedIndex -eq 0) {
            # Abu Dhabi [NO DAYLIGHT SAVING]
            ##############################################
            #             ABU DHABI                      #
            ##############################################
            
            $Office = "Abu Dhabi"
            $Newtime = (Get-Date $convertedDate).AddHours(4).ToString("HH:mm:ss")
            $convertedDate = $dateTimeString -replace " 00:00:00, End", " $newtime"
            $newdate = (Get-Date $convertedDate).AddDays(1).ToString("dd/MM/yyyy")
            $textbox10.Text = ($newdate, $Newtime) -join ' '
            
            ##############################################
        }
    }
    $Button4.add_Click($button4_Click)
    
    #~~< TextBox9 >~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    $TextBox9 = New-Object System.Windows.Forms.TextBox
    $TextBox9.Location = New-Object System.Drawing.Point(480, 84)
    $TextBox9.ReadOnly = $true
    $TextBox9.Size = New-Object System.Drawing.Size(234, 22)
    $TextBox9.TabIndex = 12
    
    #~~< TextBox8 >~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    $TextBox8 = New-Object System.Windows.Forms.TextBox
    $TextBox8.Location = New-Object System.Drawing.Point(480, 56)
    $TextBox8.ReadOnly = $true
    $TextBox8.Size = New-Object System.Drawing.Size(234, 22)
    $TextBox8.TabIndex = 11
    
    #~~< Label10 >~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    $Label10 = New-Object System.Windows.Forms.Label
    $Label10.Font = New-Object System.Drawing.Font("Tahoma", 9.75, [System.Drawing.FontStyle]::Regular, [System.Drawing.GraphicsUnit]::Point, ([System.Byte](0)))
    $Label10.Location = New-Object System.Drawing.Point(281, 56)
    $Label10.Size = New-Object System.Drawing.Size(169, 23)
    $Label10.TabIndex = 10
    $Label10.Text = "Current Expiry Date:"
    
    #~~< Label9 >~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    $Label9 = New-Object System.Windows.Forms.Label
    $Label9.Font = New-Object System.Drawing.Font("Tahoma", 9.75, [System.Drawing.FontStyle]::Regular, [System.Drawing.GraphicsUnit]::Point, ([System.Byte](0)))
    $Label9.Location = New-Object System.Drawing.Point(281, 84)
    $Label9.Size = New-Object System.Drawing.Size(169, 23)
    $Label9.TabIndex = 9
    $Label9.Text = "New Expiry Date:"
    
    #~~< Label7 >~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    $Label7 = New-Object System.Windows.Forms.Label
    $Label7.Font = New-Object System.Drawing.Font("Tahoma", 9.75, [System.Drawing.FontStyle]::Regular, [System.Drawing.GraphicsUnit]::Point, ([System.Byte](0)))
    $Label7.Location = New-Object System.Drawing.Point(281, 26)
    $Label7.Size = New-Object System.Drawing.Size(169, 23)
    $Label7.TabIndex = 2
    $Label7.Text = "Office:"
    
    #~~< MonthCalendar1 >~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    $MonthCalendar1 = New-Object System.Windows.Forms.MonthCalendar
    $MonthCalendar1.Location = '12, 27'
    $MonthCalendar1.MaxSelectionCount = 1
    $MonthCalendar1.TabIndex = 0
    $MonthCalendar1.add_DateChanged({
        [System.Windows.Forms.MessageBox]::Show($MonthCalendar1.SelectionStart)
    })
    
    
    $GroupBox2.Controls.Add($Button1)
    $GroupBox2.Controls.Add($Label11)
    $GroupBox2.Controls.Add($TextBox11)
    $GroupBox2.Controls.Add($Label1)
    $GroupBox2.Controls.Add($TextBox10)
    $GroupBox2.Controls.Add($ComboBox1)
    $GroupBox2.Controls.Add($Button4)
    $GroupBox2.Controls.Add($TextBox9)
    $GroupBox2.Controls.Add($TextBox8)
    $GroupBox2.Controls.Add($Label10)
    $GroupBox2.Controls.Add($Label9)
    $GroupBox2.Controls.Add($Label7)
    $GroupBox2.Controls.Add($MonthCalendar1)
    
    #~~< GroupBox1 >~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    $GroupBox1 = New-Object System.Windows.Forms.GroupBox
    $GroupBox1.Font = New-Object System.Drawing.Font("Microsoft Sans Serif", 9.75, [System.Drawing.FontStyle]::Bold, [System.Drawing.GraphicsUnit]::Point, ([System.Byte](0)))
    $GroupBox1.Location = New-Object System.Drawing.Point(12, 82)
    $GroupBox1.Size = New-Object System.Drawing.Size(727, 137)
    $GroupBox1.TabIndex = 0
    $GroupBox1.TabStop = $false
    $GroupBox1.Text = "AD User Account Details"
    $GroupBox1.BackColor = [System.Drawing.SystemColors]::Control
    
    #~~< TextBox6 >~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    $TextBox6 = New-Object System.Windows.Forms.TextBox
    $TextBox6.Location = New-Object System.Drawing.Point(480, 61)
    $TextBox6.ReadOnly = $true
    $TextBox6.Size = New-Object System.Drawing.Size(234, 22)
    $TextBox6.TabIndex = 7
    
    #~~< TextBox5 >~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    $TextBox5 = New-Object System.Windows.Forms.TextBox
    $TextBox5.Location = New-Object System.Drawing.Point(480, 27)
    $TextBox5.ReadOnly = $true
    $TextBox5.Size = New-Object System.Drawing.Size(234, 22)
    $TextBox5.TabIndex = 9
    
    #~~< TextBox4 >~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    $TextBox4 = New-Object System.Windows.Forms.TextBox
    $TextBox4.Font = New-Object System.Drawing.Font("Tahoma", 7.0, [System.Drawing.FontStyle]::Bold, [System.Drawing.GraphicsUnit]::Point, ([System.Byte](0)))
    $TextBox4.Location = New-Object System.Drawing.Point(121, 94)
    $TextBox4.ReadOnly = $true
    $TextBox4.Size = New-Object System.Drawing.Size(593, 19)
    $TextBox4.TabIndex = 8
    
    #~~< TextBox3 >~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    $TextBox3 = New-Object System.Windows.Forms.TextBox
    $TextBox3.Location = New-Object System.Drawing.Point(121, 61)
    $TextBox3.ReadOnly = $true
    $TextBox3.Size = New-Object System.Drawing.Size(234, 22)
    $TextBox3.TabIndex = 7
    
    #~~< TextBox2 >~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    $TextBox2 = New-Object System.Windows.Forms.TextBox
    $TextBox2.Location = New-Object System.Drawing.Point(121, 27)
    $TextBox2.ReadOnly = $true
    $TextBox2.Size = New-Object System.Drawing.Size(234, 22)
    $TextBox2.TabIndex = 6
    
    #~~< Label6 >~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    $Label6 = New-Object System.Windows.Forms.Label
    $Label6.Font = New-Object System.Drawing.Font("Tahoma", 9.75, [System.Drawing.FontStyle]::Regular, [System.Drawing.GraphicsUnit]::Point, ([System.Byte](0)))
    $Label6.Location = New-Object System.Drawing.Point(374, 64)
    $Label6.Size = New-Object System.Drawing.Size(100, 23)
    $Label6.TabIndex = 5
    $Label6.Text = "Employee ID:"
    
    #~~< Label5 >~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    $Label5 = New-Object System.Windows.Forms.Label
    $Label5.Font = New-Object System.Drawing.Font("Tahoma", 9.75, [System.Drawing.FontStyle]::Regular, [System.Drawing.GraphicsUnit]::Point, ([System.Byte](0)))
    $Label5.Location = New-Object System.Drawing.Point(374, 30)
    $Label5.Size = New-Object System.Drawing.Size(100, 23)
    $Label5.TabIndex = 4
    $Label5.Text = "Surname:"
    
    #~~< Label4 >~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    $Label4 = New-Object System.Windows.Forms.Label
    $Label4.Font = New-Object System.Drawing.Font("Tahoma", 9.75, [System.Drawing.FontStyle]::Regular, [System.Drawing.GraphicsUnit]::Point, ([System.Byte](0)))
    $Label4.Location = New-Object System.Drawing.Point(15, 97)
    $Label4.Size = New-Object System.Drawing.Size(100, 23)
    $Label4.TabIndex = 3
    $Label4.Text = "User OU:"
    
    #~~< Label3 >~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    $Label3 = New-Object System.Windows.Forms.Label
    $Label3.Font = New-Object System.Drawing.Font("Tahoma", 9.75, [System.Drawing.FontStyle]::Regular, [System.Drawing.GraphicsUnit]::Point, ([System.Byte](0)))
    $Label3.Location = New-Object System.Drawing.Point(15, 64)
    $Label3.Size = New-Object System.Drawing.Size(100, 23)
    $Label3.TabIndex = 2
    $Label3.Text = "Display Name:"
    
    #~~< Label2 >~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    $Label2 = New-Object System.Windows.Forms.Label
    $Label2.Font = New-Object System.Drawing.Font("Tahoma", 9.75, [System.Drawing.FontStyle]::Regular, [System.Drawing.GraphicsUnit]::Point, ([System.Byte](0)))
    $Label2.Location = New-Object System.Drawing.Point(15, 30)
    $Label2.Size = New-Object System.Drawing.Size(100, 23)
    $Label2.TabIndex = 1
    $Label2.Text = "Forename:"
    
    $GroupBox1.Controls.Add($TextBox6)
    $GroupBox1.Controls.Add($TextBox5)
    $GroupBox1.Controls.Add($TextBox4)
    $GroupBox1.Controls.Add($TextBox3)
    $GroupBox1.Controls.Add($TextBox2)
    $GroupBox1.Controls.Add($Label6)
    $GroupBox1.Controls.Add($Label5)
    $GroupBox1.Controls.Add($Label4)
    $GroupBox1.Controls.Add($Label3)
    $GroupBox1.Controls.Add($Label2)
    
    #~~< TextBox1 >~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    $TextBox1 = New-Object System.Windows.Forms.TextBox
    $TextBox1.Location = New-Object System.Drawing.Point(492, 12)
    $TextBox1.MaxLength = 12
    $TextBox1.Size = New-Object System.Drawing.Size(214, 20)
    $TextBox1.TabIndex = 2
    
    $Form1.Controls.Add($Button5)
    $Form1.Controls.Add($Button2)
    $Form1.Controls.Add($Button3)
    $Form1.Controls.Add($Label8)
    $Form1.Controls.Add($GroupBox2)
    $Form1.Controls.Add($GroupBox1)
    $Form1.Controls.Add($TextBox1)
    
    $Form1.ShowDialog()
    
    
    

    Here is the documentation on the datetime objects: https://docs.microsoft.com/en-us/dotnet/api/system.datetime?view=netframework-4.8#methods

    It has all of the methods to manage dates without using strings or conversions attempts.


    \_(ツ)_/

    Wednesday, September 11, 2019 5:37 AM
  • Thank you so much for taking your time and helping me out with my issues. I was able to run the script and get the showdialog the date selected and also have the date displayed in Select Expiry date.

    I am working on understanding why I am still unable to set the expiry as i do not get anymore errors.


    Justin

    Wednesday, September 11, 2019 10:07 AM
  • How are you setting the expiry date?

    This is how to set the date:

            Set-ADUser $TextBox1.Text -AccountExpirationDate $textbox10.Text

    If you would give your controls proper names then all of this would be easier to understand.


    \_(ツ)_/


    • Edited by jrv Wednesday, September 11, 2019 10:17 AM
    Wednesday, September 11, 2019 10:16 AM
  • This is how to code this event:

    $button1_Click = {
        if ($ComboBox1.SelectedIndex -gt -1 -and $textbox1.Text) {
            Write-Host 'Applying Expiry Date to AD User Account'
            Try{
                Set-ADUser $TextBox1.Text -AccountExpirationDate $textbox10.Text -ErrorAction Stop
            }
            Catch{
                [System.Windows.Forms.MessageBox]::Show($_)
            }
        }
    }
    

    You really should learn PowerShell before trying to build forms.

    You cannot match an integer with a string.  It just won't work.  Using quote on everything only creates issues.  Learn what quotwes are and how they and other objects and types work or you will never learn PowerSHell.


    \_(ツ)_/

    Wednesday, September 11, 2019 10:23 AM
  • Thanks jrv. I am sure i learnt a lot from working on this and with your explanation.

    


    Justin

    Wednesday, September 11, 2019 10:53 AM