Capture variable in foreach loop and add to csv export

Welcome Forums General PowerShell Q&A Capture variable in foreach loop and add to csv export

This topic contains 2 replies, has 2 voices, and was last updated by

 
Participant
1 year, 7 months ago.

  • Author
    Posts
  • #74387

    Participant
    Points: 0
    Rank: Member

    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

    Here is the code I used for the function code in github

    Thank you!

    Here is the code main part of the code that I have.

       $Servers = Get-Content 'H:\demo\computernames.txt'
    
    foreach ($Server in $Servers)
    {
        if (-not( Test-Connection $Server -Count 1 -Quiet )) { continue }
    
        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#
    
    
                    
            }
        else
        {
           Convert-QueryToObjects -Name $Server | select ComputerName, Username, Sessionstate, IdleTime, ID | Export-Csv 'H:\demo\session\run.csv' -NoTypeInformation
        }
       }
       
  • #74396

    Participant
    Points: 642
    Helping Hand
    Rank: Major Contributor

    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?

    • #74399

      Participant
      Points: 0
      Rank: Member

      Hi Olaf,

      Thank you for your reply.

      After continuously testing the script, I noticed a few more bugs.

      First bug, the Else statement refuses to override the data found in original file

        else
          {
           
             Convert-QueryToObjects -Name $Server | Where-Object  {@('Active','Disconnected') -contains $_.SessionState} | select @{Name='Server Name';Expression={$_.ComputerName}},
              @{Name='Username'; Expression={$_.Username}}, @{Name='Session State'; Expression={$_.SessionState}}, @{Name='Idle Time'; Expression={$_.IdleTime}}, @{Name='ID'; Expression={$_.ID}} | Export-Csv 'H:\demo\session\run20.csv' -Append
      
          }
         }
      

      Next bug, The 2nd if statement:

      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.

      Thank you.

The topic ‘Capture variable in foreach loop and add to csv export’ is closed to new replies.

denizli escort samsun escort muğla escort ataşehir escort kuşadası escort