Solved

Powershell Strange Resulte

Posted on 2014-12-17
7
178 Views
Last Modified: 2014-12-18
function daymonthyear ($product,$day)
{
$display = ""
if ($day -gt 0) {
    $years = [int]($day /365)
    $months = (($day - (365 * $years)) /30)
    if ($months -le 1) {$months =0}
    $days = [int]$day  - ((365 * $years) + ($months *30))
    $display=$product + "Will Expire in " 
    $display= $display + $years + "Years" 
    $display = $display + $months + " Months"
    $display = $display + $days + " Days"
   }
   else {
      $display = $product + " Has Expired"
    }
write-output($display)
}
clear
$ds= New-TimeSpan $(Get-Date) $(Get-Date -month 04 -day 08 -year 2014)
$product = "Windows XP"
daymonthyear($product,$ds.Days)
$ds= New-TimeSpan $(Get-Date) $(Get-Date -month 07 -day 14 -year 2015)
$product = "Windows Server 2003"
daymonthyear($product,$ds.Days)
<#
$ds= New-TimeSpan $(Get-Date) $(Get-Date -month 04 -day 11 -year 2017)
$product = "Windows Vista"
daymonthyear($product,$ds.Days)

$ds= New-TimeSpan $(Get-Date) $(Get-Date -month 01 -day 14 -year 2020)
$product = "Windows 7"
daymonthyear($product,$ds.Days)

$ds= New-TimeSpan $(Get-Date) $(Get-Date -month 01 -day 09 -year 2023)
$product = "Windows 8"
daymonthyear($product,$ds.Days)
$ds= New-TimeSpan $(Get-Date) $(Get-Date -month 01 -day 14 -year 2020)
$product = "Windows Server 2008"
daymonthyear($product,$ds.Days)
$ds= New-TimeSpan $(Get-Date) $(Get-Date -month 01 -day 14 -year 2020)
$product = "Windows Server 2008R2"
daymonthyear($product,$ds.Days)

$ds= New-TimeSpan $(Get-Date) $(Get-Date -month 01 -day 10 -year 2023)
$product = "Windows Server 2012"
daymonthyear($product,$ds.Days)
$ds= New-TimeSpan $(Get-Date) $(Get-Date -month 01 -day 10 -year 2023)
$product = "Windows Server 2012R2"
daymonthyear($product,$ds.Days)
#>

Open in new window


Results:
Windows XP
-254
 Has Expired
Windows Server 2003
208
 Has Expired
0
Comment
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 
LVL 40

Accepted Solution

by:
footech earned 500 total points
ID: 40506473
You're passing the arguments to the function incorrectly.  With the current code you're passing ($product,$ds.Days) as a single array to the $product variable of the function.  Instead call the function like this:
daymonthyear $product $ds.Days

Open in new window

0
 
LVL 40

Expert Comment

by:footech
ID: 40506512
Additionally, I think you can improve your function like this.
function daymonthyear ($product,$day)
{
    $display = ""
    if ($day -gt 0) {
        $years = [math]::Floor($day /365)
        $months = [math]::floor(($day - (365 * $years)) /30)
        $days = [int]$day  - ((365 * $years) + ($months *30))
        $display = "{0} Will Expire in`n {1} Years {2} Months {3} Days" -f $product,$years,$months,$days
    }
    else {
        $display = "{0} Has Expired" -f $product
    }
    Write-Output $display
}

Open in new window

0
 
LVL 81

Author Comment

by:David Johnson, CD, MVP
ID: 40506547
added your idea
function daymonthyear ($product,$day)
{
Write-output("Product = " + $product + " Days=:" + $day)
$day = [int]$day
$display = ""
    if ($day -gt 0) {
        $years = [math]::Floor($day /365)
        $months = [math]::floor(($day - (365 * $years)) /30)
        $days = [int]$day  - ((365 * $years) + ($months *30))
        $display = "{0} Will Expire in`n {1} Years {2} Months {3} Days" -f $product,$years,$months,$days
    }
    else {
        $display = "{0} Has Expired" -f $product
    }
    Write-Output $display
}
clear
$ds= New-TimeSpan $(Get-Date) $(Get-Date -month 04 -day 08 -year 2014)
$product = "Windows XP"
daymonthyear($product,$ds.Days)
$ds= New-TimeSpan $(Get-Date) $(Get-Date -month 07 -day 14 -year 2015)
$product = "Windows Server 2003"
daymonthyear($product,$ds.Days)

$ds= New-TimeSpan $(Get-Date) $(Get-Date -month 04 -day 11 -year 2017)
$product = "Windows Vista"
daymonthyear($product,$ds.Days)

$ds= New-TimeSpan $(Get-Date) $(Get-Date -month 01 -day 14 -year 2020)
$product = "Windows 7"
daymonthyear($product,$ds.Days)

$ds= New-TimeSpan $(Get-Date) $(Get-Date -month 01 -day 09 -year 2023)
$product = "Windows 8"
daymonthyear($product,$ds.Days)
$ds= New-TimeSpan $(Get-Date) $(Get-Date -month 01 -day 14 -year 2020)
$product = "Windows Server 2008"
daymonthyear($product,$ds.Days)
$ds= New-TimeSpan $(Get-Date) $(Get-Date -month 01 -day 14 -year 2020)
$product = "Windows Server 2008R2"
daymonthyear($product,$ds.Days)
$ds= New-TimeSpan $(Get-Date) $(Get-Date -month 01 -day 10 -year 2023)
$product = "Windows Server 2012"
daymonthyear($product,$ds.Days)
$ds= New-TimeSpan $(Get-Date) $(Get-Date -month 01 -day 10 -year 2023)
$product = "Windows Server 2012R2"
daymonthyear($product,$ds.Days)

Open in new window

added a line for debugging result=
Product = Windows XP -254 Days=:
Windows XP Has Expired
Product = Windows Server 2003 208 Days
Windows Server 2003 Has Expired
Product = Windows Vista 845 Days
Windows Vista Has Expired
Product = Windows 7 1853 Days
Windows 7 Has Expired
Product = Windows 8 2944 Days
Windows 8 Has Expired
Product = Windows Server 2008 1853 Days
Windows Server 2008 Has Expired
Product = Windows Server 2008R2 1853 Days
Windows Server 2008R2 Has Expired
Product = Windows Server 2012 2945 Days
Windows Server 2012 Has Expired
Product = Windows Server 2012R2 2945 Days
Windows Server 2012R2 Has Expired
0
Comparison of Amazon Drive, Google Drive, OneDrive

What is Best for Backup: Amazon Drive, Google Drive or MS OneDrive? In this free whitepaper we look at their performance, pricing, and platform availability to help you decide which cloud drive is right for your situation. Download and read the results of our testing for free!

 
LVL 40

Expert Comment

by:footech
ID: 40506565
You're still passing the arguments to the function incorrectly.  See my first post.  Substitute the code for line 20, 23, etc. in your last post.

Here's a good way of checking the contents of a variable - output it while surrounding it by visible characters.  It's a great way to see when it spans multiple lines, has whitespace at the beginning or end, etc.
function daymonthyear ($product,$day)
{
"`$product is ::$product::"
"`$day is ::$day::"
    $display = ""
    if ($day -gt 0) {
        $years = [math]::Floor($day /365)
        $months = [math]::floor(($day - (365 * $years)) /30)
        $days = [int]$day  - ((365 * $years) + ($months *30))
        $display = "{0} Will Expire in`n {1} Years {2} Months {3} Days" -f $product,$years,$months,$days
    }
    else {
        $display = "{0} Has Expired" -f $product
    }
    Write-Output $display
}

Open in new window

0
 
LVL 81

Author Comment

by:David Johnson, CD, MVP
ID: 40506600
now it works Thank you I didn't realize that multiple variables are passed using a space as a deliminator

function daymonthyear ($product,$day)
{
$day = [int]$day
    $display = ""
    if ($day -gt 0) {
        $years = [math]::Floor($day /365)
        $months = [math]::floor(($day - (365 * $years)) /30)
        $days = [int]$day  - ((365 * $years) + ($months *30))
        $display = "{0} Will Expire in {1} Years {2} Months {3} Days" -f $product,$years,$months,$days
    }
    else {
        $display = "{0} Has Expired" -f $product
    }
    Write-Output $display
}

clear
$ds= New-TimeSpan $(Get-Date) $(Get-Date -month 04 -day 08 -year 2014)
$product = "Windows XP"
daymonthyear $product $ds.Days
$ds= New-TimeSpan $(Get-Date) $(Get-Date -month 07 -day 14 -year 2015)
$product = "Windows Server 2003"
daymonthyear $product $ds.Days

$ds= New-TimeSpan $(Get-Date) $(Get-Date -month 04 -day 11 -year 2017)
$product = "Windows Vista"
daymonthyear $product $ds.Days

$ds= New-TimeSpan $(Get-Date) $(Get-Date -month 01 -day 14 -year 2020)
$product = "Windows 7"
daymonthyear $product $ds.Days

$ds= New-TimeSpan $(Get-Date) $(Get-Date -month 01 -day 09 -year 2023)
$product = "Windows 8"
daymonthyear $product $ds.Days
$ds= New-TimeSpan $(Get-Date) $(Get-Date -month 01 -day 14 -year 2020)
$product = "Windows Server 2008"
daymonthyear $product $ds.Days
$ds= New-TimeSpan $(Get-Date) $(Get-Date -month 01 -day 14 -year 2020)
$product = "Windows Server 2008R2"
daymonthyear $product $ds.Days

$ds= New-TimeSpan $(Get-Date) $(Get-Date -month 01 -day 10 -year 2023)
$product = "Windows Server 2012"
daymonthyear $product $ds.Days
$ds= New-TimeSpan $(Get-Date) $(Get-Date -month 01 -day 10 -year 2023)
$product = "Windows Server 2012R2"
daymonthyear $product $ds.Days

Open in new window

0
 
LVL 40

Expert Comment

by:footech
ID: 40506620
Glad I could help.

It may help to remember if you think of passing variables to functions the same way that you pass information for different parameters to a cmdlet.  In the case of a simple function the parameters are always by position (vs. named parameters).  Here's an example of a cmdlet with parameters by position.
Add-Content c:\temp\file.txt "line1","line2"

Open in new window

0
 
LVL 81

Author Comment

by:David Johnson, CD, MVP
ID: 40506850
such is the problem when you are fluent in several languages you tend to use what you're used to..
0

Featured Post

Edgartown IT Case Study

Learn about Edgartown's quest to ensure the safety and security of the entire town's employee and citizen data. Read the case study!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A procedure for exporting installed hotfix details of remote computers using powershell
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

695 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question