1

I am trying to display milestones. I tried below but I get an error that displays Method invocation failed because [System.String] does not contain a method named 'AddDays'. I have predefined $lastmodified in a previous line and it is 12/28/2015 0:00

$predetermined=[system.datetime]$LastModified
$date= ($predetermined).AddDays(30).ToString("MM/dd/yyyy:")

$date5 = ($date).AddDays(-15).ToString("MM/dd/yyyy:")
$date4 = ($date).AddDays(-12).ToString("MM/dd/yyyy:")
$date3 = ($date).AddDays(-9).ToString("MM/dd/yyyy:")
$date2 = ($date).AddDays(-6).ToString("MM/dd/yyyy:")
$date1 = ($date).AddDays(-3).ToString("MM/dd/yyyy:")

write-host -foregroundcolor Green "$date5 Action 1"
write-host -foregroundcolor Green "$date4 Action 2"
write-host -foregroundcolor Green "$date3 Action 3"
write-host -foregroundcolor Green "$date2 Action 4"
write-host -foregroundcolor Green "$date1 Action 5"
write-host -foregroundcolor Green "$date Action 6"

My output should say

12/23/2015: Action 1"
12/26/2015: Action 2"
12/19/2015: Action 3"
12/22/2015: Action 4"
12/25/2015: Action 5"
12/28/2015: Action 6"
ShanayL
  • 1,217
  • 2
  • 14
  • 29
  • Your `$date` is string, because you call `ToString("MM/dd/yyyy:")` yourself. And string does not have `AddDays` method. – user4003407 Mar 13 '16 at 02:46
  • That did it. I removed the string part: `$date5 = ($date).AddDays(-15) $date4 = ($date).AddDays(-12) $date3 = ($date).AddDays(-9) $date2 = ($date).AddDays(-6) $date1 = ($date).AddDays(-3)` but is there a way to format the answers because the outputs are all going off of `$lastmodified` which is in the format 12/28/2015 0:00. Maybe add -d at the end – ShanayL Mar 13 '16 at 02:57
  • 1
    Keep variables, which you use in `datetime` arithmetic, as `datetime`. And use `ToString` for variables, which you want to display. – user4003407 Mar 13 '16 at 03:09

2 Answers2

2

The error is right in front of you: Method invocation failed because [System.String] does not contain a method named 'AddDays'

You are trying to use AddDays() which is a method in the DateTime-class, but $date is not a DateTime-object because you turned it into a string

$date= ($predetermined).AddDays(30).ToString("MM/dd/yyyy:")

You need to keep $date as a DateTime-object if you aregoing to use it to create the other variables. Ex:

$predetermined=[system.datetime](get-date)
$date = $predetermined.AddDays(30)

$date5 = $date.AddDays(-15).ToString("MM/dd/yyyy:")
$date4 = $date.AddDays(-12).ToString("MM/dd/yyyy:")
$date3 = $date.AddDays(-9).ToString("MM/dd/yyyy:")
$date2 = $date.AddDays(-6).ToString("MM/dd/yyyy:")
$date1 = $date.AddDays(-3).ToString("MM/dd/yyyy:")
#Convert `$date` to string using specified format
$date0 = $date.ToString("MM/dd/yyyy:")

write-host -foregroundcolor Green "$date5 Action 1"
write-host -foregroundcolor Green "$date4 Action 2"
write-host -foregroundcolor Green "$date3 Action 3"
write-host -foregroundcolor Green "$date2 Action 4"
write-host -foregroundcolor Green "$date1 Action 5"
write-host -foregroundcolor Green "$date0 Action 6"

UPDATE: Steps to troubleshoot.. The error you received included more then just the message. It also said which line caused the error.

Method invocation failed because [System.String] does not contain a method named 'AddDays'.
At line:4 char:1
+ $date5 = ($date).AddDays(-15).ToString("MM/dd/yyyy:")
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : MethodNotFound

It also said that AddDays() is not a method in System.String, so then we look at what you object you called AddDays() on:

($date).AddDays(-15)...

This means $date is a string-object. Now why is that? That's because it contains the result from of a ToString()-method.

$date= ($predetermined).AddDays(30).ToString("MM/dd/yyyy:")
Frode F.
  • 52,376
  • 9
  • 98
  • 114
  • I noticed after someone commented. I corrected it as stated. I just needed a second pair of eyes. Also I did not know what `[System.String] does not contain a method named 'AddDays'` meant in reference to my code and why it was displayed. – ShanayL Mar 13 '16 at 17:57
  • The fact that you didn't know why you got the error tells me that you didn't spend enough time troubleshooting yourself to be honest. See updated answer for explanation. – Frode F. Mar 13 '16 at 18:15
0

Do something like this in your code:

function Get-DateFmt1
{
    param (
        [Parameter(Mandatory=$true, Position=0)]
        [DateTime] $Date
    )

    $Date.ToString("MM/dd/yyyy")
}

$LastModified = [DateTime]::Parse('12/28/2015')
# ....
$predetermined=[DateTime]$LastModified
$date = ($predetermined).AddDays(30)

$date5 = $date.AddDays(-15)
$date4 = $date.AddDays(-12)
$date3 = $date.AddDays(-9)
$date2 = $date.AddDays(-6)
$date1 = $date.AddDays(-3)

Write-Host -Foregroundcolor Green "$($date5.ToString("MM/dd/yyyy")): Action 1"
Write-Host -Foregroundcolor Green "$($date4.ToString("MM/dd/yyyy")): Action 2"
Write-Host -Foregroundcolor Green "$($date3.ToString("MM/dd/yyyy")): Action 3"
# or write a helper function if that makes sense to you
Write-Host -Foregroundcolor Green "$(Get-DateFmt1 $date2): Action 4"
Write-Host -Foregroundcolor Green "$(Get-DateFmt1 $date1): Action 5"
Write-Host -Foregroundcolor Green "$(Get-DateFmt1 $date): Action 6"
Kory Gill
  • 6,993
  • 1
  • 25
  • 33
  • Thank you I like this way better. Its a lot cleaner. – ShanayL Mar 13 '16 at 17:57
  • This answer doesn't explain why his code wasn't working. – Frode F. Mar 13 '16 at 18:19
  • Technically, the OP's question, actually has no question. It is unclear whether the OP wanted an explanation of how PowerShell works, or to be shown working code for more investigation an self-learning. – Kory Gill Mar 13 '16 at 19:38