locked
PowerShell: Get Date based on Week Number of Year... RRS feed

  • Question

  • What I am trying to do is get an estimated manufacturing date from the serial number of our computer fleet. For example, if a serial number is 2UA1234XY5, that tells me that the computer was built on the 23rd week of the year 2011. The year is taken from the fourth character (1), and the week is taken from the fifth(2) and sixth(3) characters; always.

    However, the problem I need help with is converting that "week, year" into an actual date, specifically the Monday of that week. In this scenario mentioned above, the 23rd week of 2011 would be Monday, June 6, 2011.

    What I've seen online is only how to get the week number off a date, but I already have that variable from the serial number. I need help figuring out how to do the opposite: turn that week number into a date. Thank you so much in advance for any help given. Really appreciate it. This is what I have so far...

    [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing") 
    [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") 
    
    # $x=$objTextBox.Text;$objForm.Close()
    $todaysdate = get-date -displayhint date
    $ErrorProvider = New-Object System.Windows.Forms.ErrorProvider
    $wshell = New-Object -ComObject Wscript.Shell
    
    $objForm = New-Object System.Windows.Forms.Form 
    $objForm.Text = "PC Upgrade Lookup Tool"
    $objForm.Size = New-Object System.Drawing.Size(300,100) 
    $objForm.StartPosition = "CenterScreen"
    $objForm.FormBorderStyle = "FixedDialog"
    $objForm.MaximizeBox = $false
    $objForm.MinimizeBox = $false
    $objForm.ShowInTaskbar = $false
    
    $objForm.AutoSize = $True
    $objForm.AutoSizeMode = "GrowOnly"
    
    $objForm.KeyPreview = $True
    $objForm.Add_KeyDown({if ($_.KeyCode -eq "Enter") {
        If($objTextBox.Text -match "^US2UA(.)(..)\w{4}$") {
            $manyear = $matches[1];
    		$manweek = $matches[2];
    		$year = if($manyear -eq "0") { "2010" }
    		    elseif($manyear -eq "1") { "2011" }
    			elseif($manyear -eq "2") { "2012" }
    			elseif($manyear -eq "3") { "2013" }
    			elseif($manyear -eq "4") { "2014" }
    			elseif($manyear -eq "5") { "2015" }
    			elseif($manyear -eq "6") { "2016" }
    			elseif($manyear -eq "7") { "2017" }
    			elseif($manyear -eq "8") { "2018" }
    			elseif($manyear -eq "9") { "2009" }
    			    else { "No year could be found" }
    			
    		$wshell.Popup("Here are your Results:
    
    Estimated Build Date: 
    $manweek,$year",0,"Results",0x40 + 0x1000 + 0x0)
            
            } elseif($objTextBox.Text -match "^2UA(.)(..)\w{4}$") {
                $manyear = $matches[1];
                $manweek = $matches[2];
                $year = if($manyear -eq "0") { "2010" }
    				elseif($manyear -eq "1") { "2011" }
    				elseif($manyear -eq "2") { "2012" }
    				elseif($manyear -eq "3") { "2013" }
    				elseif($manyear -eq "4") { "2014" }
    				elseif($manyear -eq "5") { "2015" }
    				elseif($manyear -eq "6") { "2016" }
    				elseif($manyear -eq "7") { "2017" }
    				elseif($manyear -eq "8") { "2018" }
    				elseif($manyear -eq "9") { "2009" }
    					else { "No year could be found" };
                $wshell.Popup("Here are your results:
    Estimated Build Date: 
    $manweek, $year",0,"Results",0x40 + 0x1000 + 0x0)
        } else {$ErrorProvider.SetError($objTextBox, "Please enter a valid Computer Serial Number"); Start-Sleep -s 1; $ErrorProvider.Clear()
        
        }
    }})
    
    $objForm.Add_KeyDown({if ($_.KeyCode -eq "Escape") 
        {$objForm.Close()}})
    
    $CheckButton = New-Object System.Windows.Forms.Button
    $CheckButton.Location = New-Object System.Drawing.Size(75,120)
    $CheckButton.Size = New-Object System.Drawing.Size(75,23)
    $CheckButton.Text = "Check"
    $CheckButton.Add_Click({
        If($objTextBox.Text -match "^US2UA(.)(..)\w{4}$") {
            $manyear = $matches[1];
    		$manweek = $matches[2];
    		$year = if($manyear -eq "0") { "2010" }
    		    elseif($manyear -eq "1") { "2011" }
    			elseif($manyear -eq "2") { "2012" }
    			elseif($manyear -eq "3") { "2013" }
    			elseif($manyear -eq "4") { "2014" }
    			elseif($manyear -eq "5") { "2015" }
    			elseif($manyear -eq "6") { "2016" }
    			elseif($manyear -eq "7") { "2017" }
    			elseif($manyear -eq "8") { "2018" }
    			elseif($manyear -eq "9") { "2009" }
    			    else { "No year could be found" }
    			
    		$wshell.Popup("Here are your Results:
    
    Estimated Build Date: 
    $manweek,$year",0,"Results",0x40 + 0x1000 + 0x0)
            
            } elseif($objTextBox.Text -match "^2UA(.)(..)\w{4}$") {
                $manyear = $matches[1];
                $manweek = $matches[2];
                $year = if($manyear -eq "0") { "2010" }
    				elseif($manyear -eq "1") { "2011" }
    				elseif($manyear -eq "2") { "2012" }
    				elseif($manyear -eq "3") { "2013" }
    				elseif($manyear -eq "4") { "2014" }
    				elseif($manyear -eq "5") { "2015" }
    				elseif($manyear -eq "6") { "2016" }
    				elseif($manyear -eq "7") { "2017" }
    				elseif($manyear -eq "8") { "2018" }
    				elseif($manyear -eq "9") { "2009" }
    					else { "No year could be found" };
                $wshell.Popup("Here are your results:
    Estimated Build Date: 
    $manweek, $year",0,"Results",0x40 + 0x1000 + 0x0)
        } else {$ErrorProvider.SetError($objTextBox, "Please enter a valid Computer Serial Number"); Start-Sleep -s 1; $ErrorProvider.Clear()
        
        }
        
        
    })
    $objForm.Controls.Add($CheckButton)
    
    $CancelButton = New-Object System.Windows.Forms.Button
    $CancelButton.Location = New-Object System.Drawing.Size(150,120)
    $CancelButton.Size = New-Object System.Drawing.Size(75,23)
    $CancelButton.Text = "Close"
    $CancelButton.Add_Click({$objForm.Close()})
    $objForm.Controls.Add($CancelButton)
    
    $objLabel = New-Object System.Windows.Forms.Label
    $objLabel.Location = New-Object System.Drawing.Size(10,30) 
    $objLabel.Size = New-Object System.Drawing.Size(280,30) 
    $objLabel.Text = "Please enter the Computer Serial Number:
    e.g. 2UA1234XY5, or US2UA1234XY5"
    $objForm.Controls.Add($objLabel)
    
    
    $objTextBox = New-Object System.Windows.Forms.TextBox 
    $objTextBox.Location = New-Object System.Drawing.Size(10,60) 
    $objTextBox.Size = New-Object System.Drawing.Size(260,60) 
    $objForm.Controls.Add($objTextBox)
    
    $objForm.Topmost = $True
    $objForm.Add_Shown({$objForm.Activate()})
    [void] $objForm.ShowDialog()
    


    Ben G

    Saturday, March 19, 2016 11:50 PM

Answers

All replies