I am trying to capture the changing variable ‘$server’ everytime the parameters go through a foreach loop. To summarize, the $sever value is always changing, and I want to capture it and add it into a collective csv file. I have it output the server name, but I in the CSV doc, I get unnecessary elements like (system.object), etc
If you want to output data to a csv file you should use Export-CSV. When you use Out-File it’s just plain text.
What do you mean with “does not do it in a nice format”?
What do you actually try to accomplish with this code? … just get the online status?
if (-not( Convert-QueryToObjects $Server -ErrorAction SilentlyContinue)) {
$server | Out-File 'H:\demo\session\run1.csv' -Append
# this outputs everything but, does not do it in a nice format#
}
Also has an issue with not overriding the previous file. In regards to format, I just want to add some sort of header to the list of server names.
Ex:
server name
server1
server2
I’ve added the rest of the code, including the function here on git
What I am trying to accomplish:
First check to see which servers no users logged on and export the name of that server onto a separate CSV.
If there are users logged in, get the ComputerName, Username, Sessionstate, IdleTime, ID. And export all of that to a separate csv.