I have to write a script that collects data about computer and user objects and need one line output per foreach loop. \nQuick example… \nIf($host.pingsucceeded){ \nwrite-output Ping Sucessful \n} \nelse{ \nwrite-output Ping Failed \n} \nThere are multiple tests for each user and host. How do I store the output and write it to one line at the end of each loop?<\/p>","upvoteCount":5,"answerCount":12,"datePublished":"2020-03-17T20:24:44.000Z","author":{"@type":"Person","name":"atsur415","url":"https://community.spiceworks.com/u/atsur415"},"acceptedAnswer":{"@type":"Answer","text":"
Advertisement
Something like this?<\/p>\n
foreach ($comp in $computers){ \n $MSG = ''\n $testconnection= tnc $comp\n $servicecheck = Get-Service -ComputerName $comp -Name BITS\n if($testconnection.PingSucceeded -eq $true){\n $MSG += \"$comp is ON \"\n if($servicecheck.Status -eq \"Stopped\"){\n $MSG += \"Service is Stopped\"\n }\n if($servicecheck.Status -eq \"Running\"){\n $MSG += \"Service is Running\"\n }\n } \n elseif($testconnection.PingSucceeded -eq $false){\n $MSG += \"$comp is OFF\"\n }\n $MSG\n}\n<\/code><\/pre>","upvoteCount":0,"datePublished":"2020-03-18T13:35:16.000Z","url":"https://community.spiceworks.com/t/powershell-output-format-question/755695/6","author":{"@type":"Person","name":"DoctorDNS","url":"https://community.spiceworks.com/u/DoctorDNS"}},"suggestedAnswer":[{"@type":"Answer","text":"
Advertisement
I have to write a script that collects data about computer and user objects and need one line output per foreach loop. \nQuick example… \nIf($host.pingsucceeded){ \nwrite-output Ping Sucessful \n} \nelse{ \nwrite-output Ping Failed \n} \nThere are multiple tests for each user and host. How do I store the output and write it to one line at the end of each loop?<\/p>","upvoteCount":5,"datePublished":"2020-03-17T20:24:44.000Z","url":"https://community.spiceworks.com/t/powershell-output-format-question/755695/1","author":{"@type":"Person","name":"atsur415","url":"https://community.spiceworks.com/u/atsur415"}},{"@type":"Answer","text":"