Get System Insall

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

  • Author
    Posts
  • #5729

    by jeg at 2013-03-27 14:47:16

    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\admin\Desktop\PS\GetInstallDate.ps1:38 char:3
    + $Report.InstallDate = [datetime] (Get-WmiObject -Class Win32_OperatingSystem – ...
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : InvalidArgument: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvalidCastParseTargetInvocationWithFormatProvider

    by ps_gregg at 2013-03-28 17:22:18

    Hi Jeg,

    WMI handles dates differently and you will need to use the ConvertToDateTime Method of the WMI object.
    I recommend that you change line #38

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

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

    Hope that helps.

    -Gregg

You must be logged in to reply to this topic.