title: Loop Confusion Comparing Objects
body: <p>I am trying to create my own service comparison script. I see some online but want to do this myself. I've only gotten so far. I keep getting confused. </p>
  
  <p>The desired output is with the following format. It doesn't even have to show what's different. I just want to see what the previous state was compared to the current state. I did a compare-object and it didn't give me the format I desired. I then thought maybe I should just do two nested loops and create a new object with the states I want in it. It didn't work out correctly, it returns an array. So then I thought, maybe a for loop in the foreach loop... I keep confusing myself and it's so close. </p>
  
  <p>You have to provide a csv with some services to compare to to make this work as part of it's paramaters. </p>
  
  <p><strong>Usage</strong></p>
  
  <pre><code>Inspect-ServiceSnapshot -SnapshotPath "C:\YourPath"
  </code></pre>
  
  <p><strong>Desired output</strong></p>
  
  <p>Name CurrentState PreviousState
  app1 Running      Stopped</p>
  
  <p><strong>Code So Far</strong></p>
  
  <pre><code>function Inspect-ServiceSnapshot {
  [CmdletBinding()]
  param (
      #Snapshot
      [Parameter(Mandatory=$true)]
      [ValidatePattern("C:")]
      [string]
      $SnapshotPath,
  
      # timer
      [Parameter(Mandatory=$false)]
      [int]
      $TimeToWait
  )
  
     if($TimeToWait -ne $null) {
      Start-Sleep -Seconds $TimeToWait
      $list = @()
      $old = Import-Csv -Path $SnapshotPath
      foreach($entry in (get-service)) {
          foreach($oldItem in $old) {
              $object = New-Object -TypeName psobject -Property @{
                  Name = $entry.Name
                  CurrentStatus = $entry.status
                  DisplayName = $entry.displayname
                  PreviousStatus = $oldItem.status
          }
          $list += $object
          }
      }
      $list
  
  } else {
      $list = @()
      $old = Import-Csv -Path $SnapshotPath
      foreach($entry in (get-service)) {
          foreach($oldItem in $old) {
              $object = New-Object -TypeName psobject -Property @{
                  Name = $entry.Name
                  CurrentStatus = $entry.status
                  DisplayName = $entry.displayname
                  PreviousStatus = $oldItem.status
          }
          $list += $object
          }
      }
      $list
  }
  </code></pre>
  
  <p>}</p>