Automation

 View Only
Expand all | Collapse all

Unable to get the Date Output

  • 1.  Unable to get the Date Output

    Posted Jul 17, 2021 05:17 PM

    I am unable to get the date output from below, please help

    $code = @'
    ("$(Get-Date)", "$(Get-TimeZone)") -join '::'
    '@

    $report = @()

    Import-Csv -Path $reportlocation1 -UseCulture -PipelineVariable row |
    ForEach-Object -Process {
    $sInvoke = @{
    VM = $_.Name
    GuestCredential = $Creds
    ScriptTYpe = 'powershell'
    ScriptText = $code
    }
    $result = Invoke-VMScript @sInvoke
    $dummy, $out1 = $result.ScriptOutput -split '### Output ###'
    $out1 = $out1.TrimStart("`n`r")
    $report += $row | Add-Member -MemberType NoteProperty -Name 'DateTime' -Value ([DateTime]::Parse($out1)) -PassThru
    }

    $report | ft -auto

     

    Error

    You cannot call a method on a null-valued expression.
    At C:\Apps_date_info.ps1:29 char:5
    + $out1 = $out1.TrimStart("`n`r")
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

     



  • 2.  RE: Unable to get the Date Output

    Posted Jul 17, 2021 05:35 PM

    The handling of the returned result is incorrect.
    You create a string with "<datetime>::<TZ-info>", so you have to split on '::', not '### Output ###'
    Also, the DateTime is the 1st field, not the 2nd.

    Try with


     



  • 3.  RE: Unable to get the Date Output

    Posted Jul 17, 2021 06:02 PM

    when I made the changes, I am getting the below error

    Exception calling "Parse" with "1" argument(s): "The string was not recognized as a valid DateTime. There is an unknown word starting at index 0."
    At C:\Apps_date_info.ps1:30 char:5
    + $report += $row | Add-Member -MemberType NoteProperty -Name 'Date ...
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : FormatException

     

    $code = @'
    ("$(Get-Date)", "$(Get-TimeZone)")
    '@

    $report = @()

    Import-Csv -Path $reportlocation1 -UseCulture -PipelineVariable row |
    ForEach-Object -Process {
    $sInvoke = @{
    VM = $_.Name
    GuestCredential = $Creds
    ScriptTYpe = 'powershell'
    ScriptText = $code
    }
    $result = Invoke-VMScript @sInvoke
    $out1, $dummy = $result.ScriptOutput -split '::'
    $out1 = $out1.TrimStart("`n`r")
    $report += $row | Add-Member -MemberType NoteProperty -Name 'DateTime' -Value ([DateTime]::Parse($out1)) -PassThru
    }

    $report | ft -auto



  • 4.  RE: Unable to get the Date Output

    Posted Jul 17, 2021 06:14 PM

    You changed the content of $code, you left out the -join.
    Then of course it will not work.



  • 5.  RE: Unable to get the Date Output

    Posted Jul 17, 2021 06:58 PM

    LucD,

    I tried as below now

    $code = @'
    ("$(Get-Date)", "$(Get-TimeZone)") -join '::'
    '@

    $report = @()

    Import-Csv -Path $reportlocation1 -UseCulture -PipelineVariable row |
    ForEach-Object -Process {
    $sInvoke = @{
    VM = $_.Name
    GuestCredential = $Creds
    ScriptTYpe = 'powershell'
    ScriptText = $code
    }
    $result = Invoke-VMScript @sInvoke
    $out1, $dummy = $result.ScriptOutput -split '::'
    $out1 = $out1.TrimStart("`n`r")
    $report += $row | Add-Member -MemberType NoteProperty -Name 'DateTime' -Value ([DateTime]::Parse($out1)) -PassThru
    }

    $report | ft -auto

     

    But again, still getting error

    Exception calling "Parse" with "1" argument(s): "The string was not recognized as a valid DateTime. There is an unknown word starting at index 0."
    At C:\Apps_date_info.ps1:30 char:5
    + $report += $row | Add-Member -MemberType NoteProperty -Name 'Date ...
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : FormatException

    Exception calling "Parse" with "1" argument(s): "String was not recognized as a valid DateTime."
    At C:\Apps_date_info.ps1:30 char:5
    + $report += $row | Add-Member -MemberType NoteProperty -Name 'Date ...
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : FormatException

     

    I would like to get the output as below

    ganapa2000_0-1626548280592.png

     

     



  • 6.  RE: Unable to get the Date Output

    Posted Jul 17, 2021 07:07 PM

    The script is working correctly, the problem is that the [DateTime] casting doesn't know how to convert the string to a DateTime.
    That might be related to different Cultures on that targetted VM and the station where you run the script.

    What exactly do you have in $out1 when the error happens?
    Are the Cultures (Get-Culture) different on the targetted VM and the current station?

    If [DateTime] casting doesn't work, you can use the [DateTime]::ParseExact method, but you have to know the layout of the date and time in the string.



  • 7.  RE: Unable to get the Date Output

    Posted Jul 17, 2021 07:43 PM

    LucD,

    (Get-Culture) is same on current and all the servers

    I tried as below

    $code = @'
    ("$(Get-Date)", "$(Get-TimeZone)") -join '::'
    '@

    $report = @()

    Import-Csv -Path $reportlocation1 -UseCulture -PipelineVariable row |
    ForEach-Object -Process {
    $sInvoke = @{
    VM = $_.Name
    GuestCredential = $Creds
    ScriptTYpe = 'powershell'
    ScriptText = $code
    }
    $result = Invoke-VMScript @sInvoke
    $out1, $dummy = $result.ScriptOutput -join '::'
    $out1 = $out1.TrimStart("`n`r")
    $report += $row | Add-Member -MemberType NoteProperty -Name 'DateTime' -Value ($out1) -PassThru
    }

    $report | ft -auto

     

    The output is getting truncated at the last part, how can I get complete output

    ganapa2000_0-1626550943591.png

     

     



  • 8.  RE: Unable to get the Date Output

    Posted Jul 17, 2021 08:30 PM

    That's not what I asked.
    What is in $result.ScriptOutput and in $out1



  • 9.  RE: Unable to get the Date Output

    Posted Jul 19, 2021 01:08 PM

    Hi LucD,

    I am getting the output as below, both $result.ScriptOutput and in $out1 are getting the same output

     

    $code = @'
    ("$(Get-Date)", "$(Get-TimeZone)")
    '@

    ForEach-Object -Process {
    $sInvoke = @{
    VM = "MyApp1"
    GuestCredential = $Creds
    ScriptTYpe = 'powershell'
    ScriptText = $code
    }
    $result = Invoke-VMScript @sInvoke
    $result.ScriptOutput
    $out1, $dummy = $result.ScriptOutput -join '::'
    $out1
    }

    ganapa2000_0-1626700028416.png

     



  • 10.  RE: Unable to get the Date Output

    Posted Jul 19, 2021 02:49 PM

    You keep changing the code you execute.
    With the following you do the join on the targetted VM, no need to split and join again.

    The reason of the 2 dots is that the output does not fit on the console screen.
    As the Format-List will show



  • 11.  RE: Unable to get the Date Output

    Posted Jul 19, 2021 03:49 PM

    LucD,

    Now I am getting the clear output output but when I export to csv the quotes are not ending the same line in the CSV file as below

    "Name","DateTime"
    "MyApp1","7/19/2021 10:37:07 AM::(UTC-06:00) Central Time (US & Canada)
    "
    "MyApp2","7/19/2021 10:37:15 AM::(UTC-06:00) Central Time (US & Canada)
    "



  • 12.  RE: Unable to get the Date Output
    Best Answer

    Posted Jul 19, 2021 04:28 PM

    Remove the newline from the result, change the line to



  • 13.  RE: Unable to get the Date Output

    Posted Jul 19, 2021 04:42 PM

    Thank you very much LucD. that worked