Using a variable inside a method

This topic contains 2 replies, has 3 voices, and was last updated by Profile photo of Dan Potter Dan Potter 2 weeks, 4 days ago.

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #53807
    Profile photo of Andrew
    Andrew
    Participant

    I am not a developer but on the Admin side. Is the below formatting legal? Essentially, I want to pass in a value for $time based upon user input, then try and perfom the query – below. If I put in -2 it works but if I assign $time=-2 I am hitting issues

    Get-WinEvent -computername localhost -FilterHashtable @{ logname = $log; StartTime = ((get-date).AddHours($time))}

    • This topic was modified 2 weeks, 4 days ago by Profile photo of Andrew Andrew.
    #53829
    Profile photo of 84rusty .
    84rusty .
    Participant

    Can you paste all your code?

    This works for me:

    [string]$log2 = "Application"
    [int]$time = -2
    Get-WinEvent -computername localhost -FilterHashtable @{ logname = $log2; StartTime = ((get-date).AddHours($time))}
    
    #53871
    Profile photo of Dan Potter
    Dan Potter
    Participant

    You don't have to make it an integer, it already is.

    PS H:\> $hours = -2
    PS H:\> $hours.gettype()

    IsPublic IsSerial Name BaseType
    ——– ——– —- ——–
    True True Int32 System.ValueType

    PS H:\> @{start = (get-date).AddHours($hours)}

    Name Value
    —- —–
    start 9/8/2016 5:56:52 AM

Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.