locked
Cannot convert null to type "System.DateTime". + CategoryInfo : InvalidArgument: (:) [], RuntimeException + FullyQualifiedErrorId : nullToObjectInvalidCast RRS feed

  • Question

  • Hi,

    Actually, im creating objects into $information variable with converted datetime format.

    $Information = New-Object -TypeName PSObject
    
    Add-Member -InputObject $Information -MemberType NoteProperty -Name DPName -Value $Server
    
    $LastLineFormatted = ($LastLine.Replace( '"',' ' )).Substring(158,36)
    
    $Z = $LastLineFormatted.Replace('.', ' ') | ConvertFrom-Csv -Delimiter ' ' -Header "Notrequired", "Time", "Notrequired2", "0", "1", "Date"
    
    Add-Member -InputObject $Information -MemberType NoteProperty -Name Time -Value ([datetime]"$($Z.Time)").ToString('hh:mm:ss')
    Add-Member -InputObject $Information -MemberType NoteProperty -Name Date -Value ([datetime]"$($Z.Date)").ToString('yyyy-MM-dd')
    
    
    
    } 

    Information variable looks like :

    PS DDD:\> $Information

    DPName               Time     Date     
    ------               ----     ----     
    ser.mydomain.net 03:55:24 2017-10-16

    After that, im trying to get local server time and substract from that information log time and get difference.

    $LocalServerDate = get-date
    
    foreach ($Difference in $Information){
    
    
    invoke-command -ComputerName $Difference.DPName -ScriptBlock {$LocalServerDate - [datetime]$($Difference.Time)}
    
    
    
    }

    Then im trying to run this code, im getting -

    Cannot convert null to type "System.DateTime".
        + CategoryInfo          : InvalidArgument: (:) [], RuntimeException
        + FullyQualifiedErrorId : nullToObjectInvalidCast

    but if im run just selection:

    {$LocalServerDate - [datetime]$($Difference.Time)

    im getting results:

    PS DBP:\> $LocalServerDate - [datetime]$($Difference.Time)

    Days              : 0
    Hours             : 13
    Minutes           : 59
    Seconds           : 13
    Milliseconds      : 98
    Ticks             : 503530989469
    TotalDays         : 0,582790497070602
    TotalHours        : 13,9869719296944
    TotalMinutes      : 839,218315781667
    TotalSeconds      : 50353,0989469
    TotalMilliseconds : 50353098,9469

    Why its not working when im trying invoke-command to get local server time and substract from [datetime]$($Difference.Time)?


    • Edited by TechSpark0 Monday, October 16, 2017 2:56 PM add
    Monday, October 16, 2017 2:56 PM

Answers

  • Help invoke-Command -Full

    read the full help until you understand how the command works.


    \_(ツ)_/

    • Marked as answer by TechSpark0 Thursday, October 19, 2017 1:34 PM
    Monday, October 16, 2017 3:01 PM

All replies

  • Help invoke-Command -Full

    read the full help until you understand how the command works.


    \_(ツ)_/

    • Marked as answer by TechSpark0 Thursday, October 19, 2017 1:34 PM
    Monday, October 16, 2017 3:01 PM
  • Help invoke-Command -Full

    read the full help until you understand how the command works.


    \_(ツ)_/

    Oh, okay, results (variable) just left  at local server where invoke command used.

    $DPlocalTime = Invoke-command -computername $Server -scriptblock {$time = Get-WmiObject -Class win32_localtime

    Then i have information in variable, just then can make difference.

    Thursday, October 19, 2017 1:34 PM