Gwith

Lab 6

May 13th, 2014
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.66 KB | None | 0 0
  1. Function Test {
  2. #Allow Verbose
  3. [cmdletbinding()]
  4. param(
  5. #Make computer name parameter mandatory
  6. [parameter(Mandatory=$True,
  7. Valuefrompipeline=$True)]
  8. #allow user to use hostname alias
  9. [Alias('hostname')]
  10. [string[]]$computername,
  11. #log errors
  12. [switch]$ErrorLog
  13. )
  14.  
  15. BEGIN{
  16. #If errorlog siwtch is on start
  17. if ($ErrorLog) {
  18. #use [cmdletbinding()] to display write-verbose
  19. Write-Verbose "Error Log On"
  20. $i = 0
  21. #Do this loop if Errorlog switch is on
  22. Do {
  23. $LogFile = "names-$i.txt"
  24. $i++
  25. #Check for path if exist create new file
  26. } While (Test-Path $LogFile)
  27. Write-Verbose "LogFile name is $LogFile"
  28. } else {
  29. #If Errorlog switch is off
  30. Write-Verbose "Error Log Off"
  31. }
  32.  
  33. }
  34. PROCESS{
  35. Write-Verbose "Connecting to $Computer"
  36. #log file to $logfile
  37. if ($ErrorLog) {
  38. $Computer | Out-File $LogFile -Append
  39. }
  40. #set specific output to this variable
  41. $adminpasswordstatus = Get-WmiObject -class Win32_ComputerSystem | Select-Object -ExpandProperty AdminPasswordStatus
  42. switch ($adminpasswordstatus)
  43. {
  44. 1 {'Disabled'}
  45. 2 {'Enabled'}
  46. 3 {'NA'}
  47. 4 {'Unknown'}
  48. default {'Try Again'}
  49. }
  50. #for each computer grab this information
  51. foreach ($comps in $computername) {
  52. $cs = Get-WmiObject -Class Win32_ComputerSystem -ComputerName $computername | Select-Object Domain
  53. $os = Get-WmiObject -Class Win32_OperatingSystem -ComputerName $computername | Select-Object Version,ServicePackMajorVersion
  54. $bios = Get-WmiObject -Class Win32_Bios -ComputerName $computername | Select-Object SerialNumber
  55. $properties = @{'ComputerName' =$computername;
  56. 'Domain' =$cs.Domain;
  57. 'AdminPassword'=$AdminPasswordStatus;
  58. 'OSVersion' =$os.version;
  59. 'ServicePack' =$os.servicepackmajorversion;
  60. 'SerialNumber' =$bios.SerialNumber}
  61. #take objects from hash table and convert to object
  62. $obj = New-Object -TypeName PSObject -Property $properties
  63.  
  64. }
  65. }
  66.  
  67. END{
  68. #write object named $obj
  69. Write-Host "$obj"
  70.  
  71. }
  72.  
  73. }
  74.  
  75. test -computername localhost -errorlog -verbose
Advertisement
Add Comment
Please, Sign In to add comment