I was tasked with determining if computers on a list were workstations or servers. I ran this against a list of 700 machines and it works. About 20 machines returned RPC errors so I am capturing that in the “else” statement but I imagine there is a better way to do this. I am a scripting newbie so I imagine there is a better way to script this whole thing LOL! But your critique would be appreciated.<\/p>\n
#Retrieves list of machinies by product type to determine if workstation or server. .PropertyType to return a number: 1 = workstation, 2 = DC, 3 = server (member)\n\n$outputworkstations = \".\\workstations.csv\"\n$outputservers = \".\\servers.csv\"\n$outputerrors = \".\\errors.csv\"\n\n$computers = Get-Content -Path .\\hosts.txt\n$OutputMessage1 = @()\n$OutputMessage2 = @()\n$OutputMessageError = @()\n\nforeach($computer in $computers){\n\n $ProductType = (Get-WmiObject -ClassName Win32_OperatingSystem -ComputerName $computer).ProductType\n \n if ($ProductType -eq 1) {\n $OutputMessage1 += \"$computer, $ProductType\"\n }\n elseif ($ProductType -eq 2 -or $ProductType -eq 3) {\n $OutputMessage2 += \"$computer, $ProductType\"\n }\n else {\n $OutputMessageError += \"$computer, Error\"\n }\n \n }\n $OutputMessage1 | Out-File $outputworkstations -Encoding utf8 -Append \n $OutputMessage2 | Out-File $outputservers -Encoding utf8 -Append \n $OutputMessageError | Out-File $outputerrors -Encoding utf8 -Append\n\n<\/code><\/pre>","upvoteCount":10,"answerCount":16,"datePublished":"2021-12-22T20:33:13.000Z","author":{"@type":"Person","name":"barryhohstadt2","url":"https://community.spiceworks.com/u/barryhohstadt2"},"acceptedAnswer":{"@type":"Answer","text":"
Advertisement
I’d do it like so:<\/p>\n
$outputworkstations = \"workstations.csv\"\n$outputservers = \"servers.csv\"\n$outputerrors = \"errors.csv\"\n$computers = Get-Content \"hosts.txt\"\n$step = 1\n\n$computerReport = \nforeach($computer in $computers){\n $progress = @{\n ID = 1\n Activity = \"Progress\" \n Status = \"$([math]::Round((($step / $($computers|measure-object).count * 100)),0))% Complete\"\n PercentComplete = (($step / $($computers|measure-object).count) * 100)\n CurrentOperation = \"Working on '$computer' - $step of $(($computers|measure-object).count)\"\n }\n Write-Progress @progress\n\n if(Test-Connection $computer -Count 1 -Quiet){\n $ProductType = \n try{\n (Get-ciminstance -ClassName Win32_OperatingSystem -ComputerName $computer -ErrorAction Stop).ProductType\n }\n catch{\n [pscustomobject]@{\n ComputerName = $computer\n ProductType = \"Error-$($error[0])\"\n }\n }\n\n if($ProductType){\n [pscustomobject]@{\n ComputerName = $computer\n ProductType = $ProductType\n }\n }\n }\n else{\n [pscustomobject]@{\n ComputerName = $computer\n ProductType = \"Error-Computer Unreachable\"\n }\n }\n $step++\n }\n\n$computerReport |\nWhere-Object {$_.ProductType -eq 1} |\nexport-csv \"$outputworkstations\" -NoTypeInformation\n\n$computerReport |\nWhere-Object {$_.ProductType -eq 2 -or $_.ProductType -eq 3} |\nexport-csv \"$outputservers \" -NoTypeInformation\n\n$computerReport |\nWhere-Object {$_.ProductType -like \"*error*\" } |\nexport-csv \"$outputerrors\" -NoTypeInformation\n<\/code><\/pre>","upvoteCount":4,"datePublished":"2021-12-22T20:53:40.000Z","url":"https://community.spiceworks.com/t/please-critique-my-script/820401/6","author":{"@type":"Person","name":"Neally","url":"https://community.spiceworks.com/u/Neally"}},"suggestedAnswer":[{"@type":"Answer","text":"
Advertisement
I was tasked with determining if computers on a list were workstations or servers. I ran this against a list of 700 machines and it works. About 20 machines returned RPC errors so I am capturing that in the “else” statement but I imagine there is a better way to do this. I am a scripting newbie so I imagine there is a better way to script this whole thing LOL! But your critique would be appreciated.<\/p>\n
#Retrieves list of machinies by product type to determine if workstation or server. .PropertyType to return a number: 1 = workstation, 2 = DC, 3 = server (member)\n\n$outputworkstations = \".\\workstations.csv\"\n$outputservers = \".\\servers.csv\"\n$outputerrors = \".\\errors.csv\"\n\n$computers = Get-Content -Path .\\hosts.txt\n$OutputMessage1 = @()\n$OutputMessage2 = @()\n$OutputMessageError = @()\n\nforeach($computer in $computers){\n\n $ProductType = (Get-WmiObject -ClassName Win32_OperatingSystem -ComputerName $computer).ProductType\n \n if ($ProductType -eq 1) {\n $OutputMessage1 += \"$computer, $ProductType\"\n }\n elseif ($ProductType -eq 2 -or $ProductType -eq 3) {\n $OutputMessage2 += \"$computer, $ProductType\"\n }\n else {\n $OutputMessageError += \"$computer, Error\"\n }\n \n }\n $OutputMessage1 | Out-File $outputworkstations -Encoding utf8 -Append \n $OutputMessage2 | Out-File $outputservers -Encoding utf8 -Append \n $OutputMessageError | Out-File $outputerrors -Encoding utf8 -Append\n\n<\/code><\/pre>","upvoteCount":10,"datePublished":"2021-12-22T20:33:13.000Z","url":"https://community.spiceworks.com/t/please-critique-my-script/820401/1","author":{"@type":"Person","name":"barryhohstadt2","url":"https://community.spiceworks.com/u/barryhohstadt2"}},{"@type":"Answer","text":"+= is to be avoided<\/p>\n