Powershell Code

Hi All,

I am trying to understand the code below. The foreach - what exactly is it looping through???

 $data = Get-WmiObject -Class Win32_OperatingSystem |  ForEach {
        New-Object PSObject -Property @{
            Computername = $env:computername
            OS = $_.Caption
            Version = $_.Version
            SystemDirectory = $_.systemdirectory
            Serialnumber = $_.serialnumber
            InstalledOn = ($_.ConvertToDateTime($_.InstallDate))            
            LastReboot = ($_.ConvertToDateTime($_.LastBootUpTime))            
        }

Open in new window


Regards,
K
Kelly GarciaSenior Systems AdministratorAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
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.

Jason CrawfordTransport NinjaCommented:
That doesn't make any sense.  Try this instead:

Get-WmiObject -Class Win32_OperatingSystem | ForEach-Object -Process {
  [array]$object += New-Object -TypeName PSObject -Property @{
    Computername = $env:computername
    OS = $_.Caption
    Version = $_.Version
    SystemDirectory = $_.systemdirectory
    Serialnumber = $_.serialnumber
    InstalledOn = ($_.ConvertToDateTime($_.InstallDate))            
    LastReboot = ($_.ConvertToDateTime($_.LastBootUpTime))            
  }
}

Open in new window

1
Chris DentPowerShell DeveloperCommented:
It makes perfect sense. It's looping through the result of get-WmiObject, which, in this case is a bit pointless because there's one object to "loop" through.

You could replace it with the more modern Get-CimInstance (Get-WmiObject is deprecated).
Get-CimInstance Win32_OperatingSystem | Select-Object @{n='ComputerName';e={ $env:COMPUTERNAME }}, @{Name='OS';Expression={ $_.Caption }}, Version, SystemDirectory, SerialNumber, InstallDate, LastBootUpTime

Open in new window

1

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
Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
The important part is that you do not read it like this
($data = Get-WMIObject ...) | foreach { ... }

Open in new window

but
$data = ( Get-WMIObject ...) | foreach { ... } )

Open in new window

i.e. the expression including pipeline is executed, and the result assigned to the var.
2
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.