Title + “, as per usual.”<\/p>\n
$comps = \"NonExistentPC-01\"\n\n$Output = Foreach($C in $comps){\n\n\t$Flag = 0\n\t\n\tTrap {\n\n\t\t$Flag = 1\n\t\tContinue\n\t\t\n\t}\n\t\n\t$System = Get-WmiObject Win32_ComputerSystem -ComputerName $C | Select-Object -Property Name,Model\n\t$BIOS = Get-WmiObject Win32_BIOS -ComputerName $C | Select-Object -Property Manufacturer,SerialNumber\n\t\n\tIf ($Flag -eq 0) {\n\t\n\t\t[PSCustomObject]@{\n\t \n\t\t\tName = $System.Name\n\t\t\tMake = $BIOS.Manufacturer\n\t\t\tModel = $System.Model\n\t\t\tSerialNum = $BIOS.SerialNumber\n\t\t\n\t\t}\n\t\t\n\t\t$Out = $C + ' Processed - machine found'\n\t\tWrite-Host $Out\n\n\t} Else {\n\t\n\t\t[PSCustomObject]@{\n\t \n\t\t\tName = \"$C\"\n\t\t\tMake = \" \"\n\t\t\tModel = \"Machine not found\"\n\t\t\tSerialNum = \" \"\n\t\t\n\t\t}\n\t\t\n\t\t$Out = $C + ' Processed - no connection'\n\t\tWrite-Host $Out\n\t\n\t}\n\t\n}\n\n$Output | Export-Csv -Path \"c:\\Temp\\CorpWorkstationInfo.csv\" -NoTypeInformation\n\n# CLS\nWrite-Host\nWrite-Host\nWrite-Host\nWrite-Host \"All done! See file \\C:\\Temp\\SerialNumbers.txt for results\"\nWrite-Host\nWrite-Host\nWrite-Host\n<\/code><\/pre>\n
Advertisement
I’m trying to create a CSV with basic information drawn remotely from my network workstations. The magic happens in these two lines:<\/p>\n
$System = Get-WmiObject Win32_ComputerSystem -ComputerName $C | Select-Object -Property Name,Model\n$BIOS = Get-WmiObject Win32_BIOS -ComputerName $C | Select-Object -Property Manufacturer,SerialNumber\n<\/code><\/pre>\n
Advertisement
However, some of my workstations are online, sometimes not. When a workstation is online, I want to create an object that has 4 attributes, drawn from the information gathered and stored in the $Output array. When it’s not, I want a different object created that has the name of the PC and a message saying that it’s not there.<\/p>\n
Later, I want to feed it every computer in my AD, but at the moment I’m still just testing, so I’m only giving it a PC name I know doesn’t exist. Now, I have tried to encapsulated my differing states with all the permutations of If-Else, try-catch, trap, etc. statements that I could think of, but nothing seems to work quite the way I want it to. When it does work, I get the correct info for the awake PCs in the CSV & console output, and blank lines, without names or anything else, for those that aren’t answering hails at that moment.<\/p>\n
The way it’s written now, I get the headers and the blank line in the CSV, but the output to the console still says that the machine was found. Obviously, I’m misunderstanding something. Anyone want to point towards that misunderstanding?<\/p>","upvoteCount":3,"answerCount":5,"datePublished":"2020-07-23T19:13:11.000Z","author":{"@type":"Person","name":"patrickdeno2","url":"https://community.spiceworks.com/u/patrickdeno2"},"acceptedAnswer":{"@type":"Answer","text":"
then I don’t understand the issue, maybe like so?<\/p>\n
$comps = \"NonExistentPC-01\"\n\n$Output = \nForeach ($C in $comps) {\n $system = $null\n $bios = $null\n\n if (test-connection $c -Quiet -Count 1) {\n $System = Get-WmiObject Win32_ComputerSystem -ComputerName $C\n $BIOS = Get-WmiObject Win32_BIOS -ComputerName $C\n\n [PSCustomObject]@{\n Name = if($system){$System.Name}else{' '}\n Make = if($bios){$BIOS.Manufacturer}else{' '}\n Model = if($system){$System.Model}else{' '}\n SerialNum = if($bios){$BIOS.SerialNumber}else{' '}\n }\n }\n Else {\n [PSCustomObject]@{\n Name = \"$C\"\n Make = \" \"\n Model = \"Machine not found\"\n SerialNum = \" \"\n }\n }\n}\n\n$Output | Export-Csv -Path \"c:\\Temp\\CorpWorkstationInfo.csv\" -NoTypeInformation\n\n<\/code><\/pre>","upvoteCount":0,"datePublished":"2020-07-23T19:34:56.000Z","url":"https://community.spiceworks.com/t/new-fun-little-project-that-i-cant-get-to-work/770280/4","author":{"@type":"Person","name":"Neally","url":"https://community.spiceworks.com/u/Neally"}},"suggestedAnswer":[{"@type":"Answer","text":"Title + “, as per usual.”<\/p>\n
$comps = \"NonExistentPC-01\"\n\n$Output = Foreach($C in $comps){\n\n\t$Flag = 0\n\t\n\tTrap {\n\n\t\t$Flag = 1\n\t\tContinue\n\t\t\n\t}\n\t\n\t$System = Get-WmiObject Win32_ComputerSystem -ComputerName $C | Select-Object -Property Name,Model\n\t$BIOS = Get-WmiObject Win32_BIOS -ComputerName $C | Select-Object -Property Manufacturer,SerialNumber\n\t\n\tIf ($Flag -eq 0) {\n\t\n\t\t[PSCustomObject]@{\n\t \n\t\t\tName = $System.Name\n\t\t\tMake = $BIOS.Manufacturer\n\t\t\tModel = $System.Model\n\t\t\tSerialNum = $BIOS.SerialNumber\n\t\t\n\t\t}\n\t\t\n\t\t$Out = $C + ' Processed - machine found'\n\t\tWrite-Host $Out\n\n\t} Else {\n\t\n\t\t[PSCustomObject]@{\n\t \n\t\t\tName = \"$C\"\n\t\t\tMake = \" \"\n\t\t\tModel = \"Machine not found\"\n\t\t\tSerialNum = \" \"\n\t\t\n\t\t}\n\t\t\n\t\t$Out = $C + ' Processed - no connection'\n\t\tWrite-Host $Out\n\t\n\t}\n\t\n}\n\n$Output | Export-Csv -Path \"c:\\Temp\\CorpWorkstationInfo.csv\" -NoTypeInformation\n\n# CLS\nWrite-Host\nWrite-Host\nWrite-Host\nWrite-Host \"All done! See file \\C:\\Temp\\SerialNumbers.txt for results\"\nWrite-Host\nWrite-Host\nWrite-Host\n<\/code><\/pre>\nI’m trying to create a CSV with basic information drawn remotely from my network workstations. The magic happens in these two lines:<\/p>\n
$System = Get-WmiObject Win32_ComputerSystem -ComputerName $C | Select-Object -Property Name,Model\n$BIOS = Get-WmiObject Win32_BIOS -ComputerName $C | Select-Object -Property Manufacturer,SerialNumber\n<\/code><\/pre>\nHowever, some of my workstations are online, sometimes not. When a workstation is online, I want to create an object that has 4 attributes, drawn from the information gathered and stored in the $Output array. When it’s not, I want a different object created that has the name of the PC and a message saying that it’s not there.<\/p>\n
Later, I want to feed it every computer in my AD, but at the moment I’m still just testing, so I’m only giving it a PC name I know doesn’t exist. Now, I have tried to encapsulated my differing states with all the permutations of If-Else, try-catch, trap, etc. statements that I could think of, but nothing seems to work quite the way I want it to. When it does work, I get the correct info for the awake PCs in the CSV & console output, and blank lines, without names or anything else, for those that aren’t answering hails at that moment.<\/p>\n
The way it’s written now, I get the headers and the blank line in the CSV, but the output to the console still says that the machine was found. Obviously, I’m misunderstanding something. Anyone want to point towards that misunderstanding?<\/p>","upvoteCount":3,"datePublished":"2020-07-23T19:13:11.000Z","url":"https://community.spiceworks.com/t/new-fun-little-project-that-i-cant-get-to-work/770280/1","author":{"@type":"Person","name":"patrickdeno2","url":"https://community.spiceworks.com/u/patrickdeno2"}},{"@type":"Answer","text":"
without reviewing the code,<\/p>\n
I guess you have a loop in it and a variable does not empty out as you run it.<\/p>\n
let’s say you have 2 computers, 1 is online 2 is offline, you set the variable in computer1 and it’s all good, however computer 2 fails, but it does not reset the variable to it has the same value.<\/p>\n
foreach($computer in $computers){\n$var1 = $null\n$var2 = $null\n\n# other stuff\n}\n<\/code><\/pre>\nthat will ensure the variable is empty at each run and no previous assigned info stays in it.<\/p>","upvoteCount":0,"datePublished":"2020-07-23T19:22:41.000Z","url":"https://community.spiceworks.com/t/new-fun-little-project-that-i-cant-get-to-work/770280/2","author":{"@type":"Person","name":"Neally","url":"https://community.spiceworks.com/u/Neally"}},{"@type":"Answer","text":"