locked
Powershell script error RRS feed

  • Question

  • Hi ,

    Could you help me to fix this error , thank you

    +         $usersObj.IdleTime = [timespan]"0:$($quserData[5])"
    +                                             ~~~~~~~~~~~~~
        + CategoryInfo          : InvalidArgument: (:) [], RuntimeException
        + FullyQualifiedErrorId : InvalidCastParseTargetInvocationWithFormatProvider

      $quserData[5] = $quserData[5] -replace "\+",":" -replace "\.","0:0" -replace "Disc","0:0"
        if($quserData[5] -like "*:*"){
            $usersObj.IdleTime = [timespan]"$($quserData[5])"
        }elseif($quserData[5] -eq "." -or $quserData[5] -eq "none"){
            $usersObj.idleTime = [timespan]"0:0"
        }else{
            $usersObj.IdleTime = [timespan]"0:$($quserData[5])"
        }
        # LogonTime
        $usersObj.LogonTime = (Get-Date "$($quserData[6]) $($quserData[7]) $($quserData[8] )")
    
        $users += $usersObj
    
       if($usersObj.idleTime -ge [timespan]"2:00" -and  $usersObj.SessionState -eq "Disc")
    

    Thursday, February 20, 2020 2:38 PM

All replies

  • You have an invalid string.

    To create a timespan you must create a valid string format.

    Here are the instructions: https://docs.microsoft.com/en-us/dotnet/api/system.timespan?view=netframework-4.8

    If you do not provide valid strings you will get that error.


    \_(ツ)_/

    Thursday, February 20, 2020 2:44 PM
  • thank you for you help to provide the link honestly I'm not good with PowerShell and I need more time to learn    , but do you have idea what need to be change if it's quickly I will appreciate , ths 
    Thursday, February 20, 2020 3:10 PM
  • You need to verify that you are getting/creating a valid string.  If you don't understand then you will need to take the time to learn PowerShell before continuing.


    \_(ツ)_/

    Thursday, February 20, 2020 3:17 PM