Gwith

Understanding the code

May 6th, 2014
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.70 KB | None | 0 0
  1. Function Get-SystemInfo { #Create Function
  2. param( #Create Paramater
  3. [string[]]$ComputerName = 'localhost'#Create an Array of Strings ALL named $ComputerName: ie $ComputerName = @("ComputerName","ComputerName")
  4. )
  5.  
  6.  
  7. begin{ #Start giant script block, I'm confused by begin an end, why is this needed? If it is all in the function, isn't that enough?
  8. $comps = @() #Create new empty array to be used at a later time
  9. }
  10.  
  11. process{ #This represents another script block?
  12. ForEach ($computer in $ComputerName){ #ForEach loop For every $computer which we JUST created in this ForEach loop: $ComputerName run the switch below.
  13.  
  14. $AdminPasswordStatus = Get-CimInstance -ClassName Win32_ComputerSystem -ComputerName $computer | Select-Object -ExpandProperty AdminPasswordStatus #Create variable = to this value
  15. switch ($AdminPasswordStatus) #Switch, this one is pretty simple.
  16. {
  17. 1 {$AdminPasswordStatus = "Disabled"}
  18. 2 {$AdminPasswordStatus = "Enabled"}
  19. 3 {$AdminPasswordStatus = "Not Available"}
  20. 4 {$AdminPasswordStatus = "Unknown"}
  21. default {"Possible Error"}
  22. }
  23.  
  24. $OS = Get-CimInstance -ClassName Win32_OperatingSystem -ComputerName $computer #Create variable = to this value
  25. $BIOS = Get-CimInstance -ClassName Win32_BIOS -ComputerName $computer #Create variable = to this value
  26.  
  27. #Everything below this point is new to me but I'm going to try to follow the logic.
  28.  
  29. $obj = [pscustomObject]@{ #Create a custom object, This is neat I did not know this.
  30. ComputerName = $computer
  31. AdminPasswordStatus = $AdminPasswordStatus
  32. OSVersion = $OS.Version #Get-CimInstance -Classname Win32_Operatingsystem.property which is Version
  33. OSPatch = $OS.ServicePackMajorVersion #Get-CimInstance -Classname Win32_Operatingsystem.property which is ServicePackMajorVersion
  34. Bios = $BIOS.SerialNumber #Get-CimInstance -Classname Win32_BIOS.property which is SerialNumber
  35. }
  36.  
  37. #end of the ForEach loop, we now store this $obj in $comps
  38. $comps += $obj #This one is confusing. This means I have more than one $comps so you will need to make an $obj for each $comps?
  39. }
  40. }
  41.  
  42. end{#End giant script block, I'm confused by begin an end, why is this needed? If it is all in the function, isn't that enough?
  43. Write-host ("`$comps contains " + ($comps.Count) + " Objects") #Output how many computers you are running this script on.
  44. $comps | Format-Table #Change output to Format-Table
  45. }
  46. }
  47.  
  48.  
  49. Get-SystemInfo #Call Function
Advertisement
Add Comment
Please, Sign In to add comment