I can not get a windows installation date from powershell script

Hi folks

I can not get a windows installation date from script

The script:

$Computers = Get-Content -Path C:\powershell\scripts\Computers.txt
$AllComputers = @()
foreach($machine in $Computers)
{
 $objCompSys = Get-WmIObject -class "Win32_ComputerSystem" -cn $machine -EA silentlyContinue  -namespace "root\CIMV2"
 foreach($item in $objCompSys)
 {
  $compname = $item.Name
  $physicalRAM = [Math]::Ceiling($item.TotalPhysicalMemory / 1GB)
  $CurrentUser = $Item.username  
 }
 $objPhydrive = Get-WmIObject -class "Win32_DiskDrive" -cn $machine -EA silentlyContinue  -namespace "root\CIMV2"
 foreach($item in $objPhydrive)
 {
 $PhyDrive = $item.size
 }
 
 $objCompOS = Get-WmIObject -class "Win32_OperatingSystem" -cn $machine -EA silentlyContinue  -namespace "root\CIMV2"
 foreach($item in $objCompOS)
 {
 $WindowsVersion = $Item.Caption
 $ServicePack =  $Item.csdversion
 $WinInsDate = ([WMI] "").ConvertToDateTime((Get-WmiObject Win32_OperatingSystem -ComputerName $item.InstallDate)) | Get-Date -Format d
   }
 
 #$DiskItems = gwmi Win32_LogicalDisk -filter "DriveType=3" -Comp $machine
 
$DiskItems = Get-WMIObject -Class Win32_DiskDrive -Filter {DeviceId = '\\\\.\\PHYSICALDRIVE0'} | select -ExpandProperty size
                              
  foreach($item in $DiskItems) {
                    
         $DiskSize = $Item.Size/1GB
       }
   $objProc = Get-WMIObject -class "Win32_Processor" -cn $machine
 foreach($item in $objProc)
 {
  $procType = $item.Name
 }
 
 $Object = New-Object PSObject -Property @{
 
  CompName     = $compname
  WindowsVersion = $WindowsVersion
  ServicePack = $ServicePack
  WinInsDate = $WinInsDate
   CurrentUser = $CurrentUser
     }
 
$AllComputers += $Object

$AllComputers | select CompName,WindowsVersion,ServicePack,WinInsDate | Out-GridView
nesher13Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

footechCommented:
You can change that line to
$WinInsDate = ([WMI] "").ConvertToDateTime(($item.InstallDate)) | Get-Date -Format d
#or
$WinInsDate = $item.ConvertToDateTime(($item.InstallDate))

Open in new window


BTW, I don't know why you're doing a foreach there.  You can just do
$objCompOS = Get-WmIObject -class "Win32_OperatingSystem" -cn $machine -EA silentlyContinue  -namespace "root\CIMV2"
$WindowsVersion = $objCompOS.Caption
$ServicePack =  $objCompOS.csdversion
$WinInsDate = $objCompOS.ConvertToDateTime($objCompOS.InstallDate) | Get-Date -Format d

Open in new window

There are other places in your code that could be similarly simplified.
0
nesher13Author Commented:
Hi  footech

Thank you so much for your response.
I corrected the script according to your recommendations.
quote it in full :

$Machine = Get-Content -Path C:\powershell\scripts\Computers.txt
$AllComputers = @()

 $objCompSys = Get-WmIObject -class "Win32_ComputerSystem" -cn $machine -EA silentlyContinue  -namespace "root\CIMV2"
  $compname = $objCompSys.Name
  $physicalRAM = [Math]::Ceiling($objCompSys.TotalPhysicalMemory / 1GB)
  $CurrentUser = $objCompSys.username  
 
$objCompOS = Get-WmIObject -class "Win32_OperatingSystem" -cn $machine -EA silentlyContinue  -namespace "root\CIMV2"
$WindowsVersion = $objCompOS.Caption
$ServicePack =  $objCompOS.csdversion
$WinInsDate = $objCompOS.ConvertToDateTime($objCompOS.InstallDate) | Get-Date -Format d                        
 
$DiskItems = Get-WMIObject -Class Win32_DiskDrive -Filter {DeviceId = '\\\\.\\PHYSICALDRIVE0'} | select -ExpandProperty size
$DiskSize = $DiskItems.Size/1GB
$PhyDrive = $objPhydrive.size
 
 $NetAdapters = Get-WmIObject -class "Win32_NetworkAdapterConfiguration" -cn $machine  | where{$_.IPEnabled -eq "True"}
 $IPAddress =   $NetAdapters.IPAddress

 $objProc = Get-WMIObject -class "Win32_Processor" -cn $machine
   $procType = $objProc.Name
 # $procSpeed = $objProc.MaxClockSpeed
 
 $Object = New-Object PSObject -Property @{
 
  CompName     = $compname
  WindowsVersion = $WindowsVersion
  ServicePack = $ServicePack
  WinInsDate = $WinInsDate
  IPAddress = $IPAddress
  CurrentUser = $CurrentUser
 
  ProcType = $procType
  DiskSize = $DiskSize
  MemoryCapacity = $physicalRAM
#DiskSize = $Item.Size/1024/1024/1024
# FreeSpace = $Item.FreeSpace/1024/1024/1024
}
 $AllComputers += $Object

#$AllComputers | select CompName,IPaddress,WindowsVersion,ServicePack,WinInsDate,CurrentUser,ProcType,DiskSize,MemoryCapacity  | Export-Csv c:\Powershell\change2.csv
$AllComputers | select CompName,IPaddress,WindowsVersion,ServicePack,WinInsDate,CurrentUser,ProcType,DiskSize,MemoryCapacity  | Out-GridView

get an error when execution the script:

Method invocation failed because [System.Object[]] doesn't contain a method named 'ConvertToDateTime'.
At C:\Powershell\Scripts\Change2.ps1:12 char:43
+ $WinInsDate = $objCompOS.ConvertToDateTime <<<< ($objCompOS.InstallDate) | Get-Date -Format d                        
    + CategoryInfo          : InvalidOperation: (ConvertToDateTime:String) [], RuntimeException
    + FullyQualifiedErrorId : MethodNotFound
0
footechCommented:
You took out one too many foreach loop.  You still need something to process each computer separately.  It looks like you were missing the -computername parameter for $diskitems, so I added that as well.
$Machines = Get-Content -Path C:\powershell\scripts\Computers.txt
$AllComputers = @()

foreach ($machine in $machines)
{
    $objCompSys = Get-WmIObject -class "Win32_ComputerSystem" -cn $machine -EA silentlyContinue  -namespace "root\CIMV2"
    $compname = $objCompSys.Name
    $physicalRAM = [Math]::Ceiling($objCompSys.TotalPhysicalMemory / 1GB)
    $CurrentUser = $objCompSys.username   
 
    $objCompOS = Get-WmIObject -class "Win32_OperatingSystem" -cn $machine -EA silentlyContinue  -namespace "root\CIMV2"
    $WindowsVersion = $objCompOS.Caption
    $ServicePack =  $objCompOS.csdversion
    $WinInsDate = $objCompOS.ConvertToDateTime($objCompOS.InstallDate) | Get-Date -Format d                        
 
    $DiskItems = Get-WMIObject -Class Win32_DiskDrive -Filter {DeviceId = '\\\\.\\PHYSICALDRIVE0'} -computername $machine | select -ExpandProperty size
    $DiskSize = $DiskItems.Size/1GB
    $PhyDrive = $objPhydrive.size
 
    $NetAdapters = Get-WmIObject -class "Win32_NetworkAdapterConfiguration" -cn $machine  | where{$_.IPEnabled -eq "True"}
    $IPAddress =   $NetAdapters.IPAddress 

    $objProc = Get-WMIObject -class "Win32_Processor" -cn $machine 
    $procType = $objProc.Name
    # $procSpeed = $objProc.MaxClockSpeed
  
    $Object = New-Object PSObject -Property @{
 
    CompName     = $compname
    WindowsVersion = $WindowsVersion 
    ServicePack = $ServicePack
    WinInsDate = $WinInsDate
    IPAddress = $IPAddress
    CurrentUser = $CurrentUser 
  
    ProcType = $procType
    DiskSize = $DiskSize
    MemoryCapacity = $physicalRAM
    #DiskSize = $Item.Size/1024/1024/1024
    # FreeSpace = $Item.FreeSpace/1024/1024/1024
    }
    $AllComputers += $Object
}
#$AllComputers | select CompName,IPaddress,WindowsVersion,ServicePack,WinInsDate,CurrentUser,ProcType,DiskSize,MemoryCapacity  | Export-Csv c:\Powershell\change2.csv
$AllComputers | select CompName,IPaddress,WindowsVersion,ServicePack,WinInsDate,CurrentUser,ProcType,DiskSize,MemoryCapacity  | Out-GridView

Open in new window

Quick suggestion here, when posting code more than a line or two, it helps to put it in a code block.  Just highlight the text and click the "Code" button in the formatting bar.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.