0

I am having issues with the script overwriting itself and not holding onto the values. The only thing exported to the CSV is the list of active users on the last server, instead of all the users and servers together.

Thank you!

What this script does: It filters out users that have been active in for more than 60min or are disconnected. For the disconnect script, I used a simple open source script from a MS contributor, found here. I've added the complete code with all the functions to my github gist:https://gist.github.com/ruslive109/a56837bb84187b522797b2abb19d6acf

$Servers = Get-Content 'H:\demo\computernames.txt'
$openservers =@()
foreach ($Server in $Servers)
{
    if (-not( Test-Connection $Server -Count 1 -Quiet )) { continue }

    if (-not( Convert-QueryToObjects $Server -ErrorAction SilentlyContinue))
    {

     $openservers += $server
     $openservers | Out-File 'H:\demo\session\openservers.txt'
 }

    else
    {  
      Convert-QueryToObjects -Name $Server |Where-Object{ {@('Disconnected','Active') -contains $_.SessionState} | Select-Object {@{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\run11.csv' -NoTypeInformation -Force


    Import-Csv 'H:\demo\session\run11.csv' | Where-Object { ($_.SessionState -eq 'Disconnected') -or (($_.IdleTime -like "*:*") -and ($_.IdleTime -gt "00:59"))} |
    ForEach-Object {
        Disconnect-LoggedOnUser -ComputerName $_.ComputerName -Id $_.ID -Verbose 
    }

   }

   } 
3
  • What is your question?
    – Xavierjazz
    Commented Jul 11, 2017 at 18:09
  • @Xavierjazz export-csv continuously overwrites itself, thus the data does not stay in the document until the last iteration. I want the csv doc to capture everything flowing through it, not just the last thing.
    – russell
    Commented Jul 11, 2017 at 18:17
  • @Xavierjazz how can I export to csv every time it goes through a loop? Right now, only the last server that goes through the loop is stored in the csv. I want the properties of every server to be stored in the csv as they go through the loop. I -append works, but it causes issues with the disconnect function (the same user disconnected multiple times) and it does not overwrite itself every time the function is ran.
    – russell
    Commented Jul 11, 2017 at 18:47

1 Answer 1

0

I think what you want is is to append to the current file, currently it overwrites the file every time.

To append: Export-Csv 'H:\demo\session\run11.csv' -NoTypeInformation -Force -append

If you need the data to be unique each time the script runs you can clear the contents of the file if it exists. So at the beginning of your script something like.

if (Test-Path 'C:\users\map.txt')
{
    Clear-Content 'C:\users\map.txt'
}
5
  • that creates the issue with same users being disconnected continuously. If I do '-append' alone, it will store all the data, however, it will also result in the user issue mentioned above and will not allow for an overwrite everytime the script is ran
    – russell
    Commented Jul 11, 2017 at 19:03
  • If you want the output to be unique each time the script is run you could clear the file when run. Clear-Content c:\scripts\test.txt something like that I believe.
    – Confuzing
    Commented Jul 11, 2017 at 19:08
  • wouldn't this give you an error if no file has been created prior?
    – russell
    Commented Jul 11, 2017 at 19:11
  • Then test if it exists before trying.
    – Confuzing
    Commented Jul 11, 2017 at 19:15
  • Added the code to my answer for testing and clearing content.
    – Confuzing
    Commented Jul 11, 2017 at 19:19

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .