Get System Install Date throws an error

This topic contains 0 replies, has 1 voice, and was last updated by  Forums Archives 5 years, 7 months ago.

  • Author
    Posts
  • #5730

    by jeg at 2013-03-27 14:49:29

    Running below from here:
    http://gallery.technet.microsoft.com/sc ... dd84bc6239

    < #
    .SYNOPSIS
    This script gets a system installation date.
    .DESCRIPTION
    This script uses WMI to get a system installation date.
    .INPUTS
    You can enter a list of computernames as a parameter.
    The defaut is the current computer.
    The script accepts input from the pipeline.
    .OUTPUTS
    An object containing Computer and InstallDate properties.
    .NOTES
    File Name : Get-SystemInstallDate.ps1
    Author : Robert van den Nieuwendijk
    Twitter : rvdnieuwendijk
    Requires : PowerShell Version 2.0
    .LINK
    This script is posted to Microsoft TechNet Script Center Repository:
    http://gallery.technet.microsoft.com/Sc ... ter/en-us/
    .EXAMPLE
    C:\PS>.\Get-SystemInstallDate.ps1
    Retrieves the installation date from the current system.
    .EXAMPLE
    C:\PS>.\Get-SystemInstallDate.ps1 -ComputerName server1,server2
    Retrieves the installation date from server1 and server 2.
    .EXAMPLE
    C:\PS>"server1","server2"| .\Get-SystemInstallDate.ps1
    Retrieves the installation date from server1 and server 2.
    #>

    param ([Parameter(ValueFromPipeline=$true)][string[]] $ComputerName = '.')

    begin {
    function Get-SystemInstallDateForOneSystem {
    param ([string] $ComputerName = '.')
    $Report = "" | Select-Object -property Computer,InstallDate
    $Report.Computer = $ComputerName
    $Report.InstallDate = [datetime] (Get-WmiObject -Class Win32_OperatingSystem -ComputerName $ComputerName).InstallDate
    $Report
    }
    }

    process {
    if ($ComputerName -is [array]) {
    $ComputerNames = $ComputerName
    foreach ($ComputerName in $ComputerNames) {
    Get-SystemInstallDateForOneSystem -ComputerName $ComputerName
    }
    }
    else {
    Get-SystemInstallDateForOneSystem -ComputerName $ComputerName
    }
    }

    Gives me this:

    Cannot convert value "20121211102032.000000-480" to type "System.DateTime". Error: "String was not recognized as a valid
    DateTime."
    At C:\Users\\Desktop\PS\GetInstallDate.ps1:38 char:3
    + $Report.InstallDate = [datetime] (Get-WmiObject -Class Win32_OperatingSystem – ...
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : InvalidArgument: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvalidCastParseTargetInvocationWithFormatProvider

    by DonJ at 2013-03-28 07:51:04

    So, your first bet would be to contact the poster of the script. But, in general, WMI dates are tricky to work with.

    Instead of:

    $Report.InstallDate = [datetime] (Get-WmiObject -Class Win32_OperatingSystem -ComputerName $ComputerName).InstallDate

    Try:

    $InstallDate = Get-WmiObject -Class Win32_OperatingSystem -ComputerName $ComputerName
    $Report.InstallDate = $InstallDate.ConvertToDateTime($InstallDate.InstallDate)

    That's the correct way to convert a WMI date/time to a .NET Framework DateTime object.

    by OliAdams at 2013-03-28 16:20:24

    I am not sure if this helps or not and I know Mr Powershell has already answered, if you use Get-CimInstance instead of Get-WmiObject this returns the date in the correct format.

    PS C:\> Get-CimInstance win32_operatingsystem | Select InstallDate

    InstallDate
    -----------
    27.01.2013 23:33:18

    by jeg at 2013-04-11 07:30:10

    Both of these work, thanks a TON!

You must be logged in to reply to this topic.