Author Posts

August 2, 2017 at 4:04 am

[CmdletBinding(SupportsShouldProcess=$true)]Param()$CsvFile = 'C:\Temp\servers.csv'$LogFile = "C:\Temp\RDPSessions_$((Get-Date).ToString('MM-dd-yyyy_hh-mm-ss')).txt"Import-Csv -Path $CsvFile | ForEach-Object {  $Server = $_.Server  Write-Host "Processing server $($Server)"  $(& 'C:\Windows\system32\qwinsta.exe' /server:$Server |    Where-Object {$_ -match '(?.)(?.{16})\s+(?.{20})\s+(?\d+)\s+(?\S+)'} |    Where-Object {('0', '1', '65536', '65537' -notcontains $Matches['Id']) -and ($Matches['State'] -eq 'Disc')} |    ForEach-Object {      If ($PSCmdlet.ShouldProcess($Server, "Logoff user '$($Matches['Username'].Trim())' in disconnected session '$($Matches['Id'])'")) {        & 'C:\Windows\system32\logoff.exe' $Matches['Id'] /server:$Server        "[$($Server)] Logged off user '$($Matches['Username'].Trim())' in disconnected session '$($Matches['Id'])'"      } Else {        "[WHATIF] [$($Server)] Would have logged off user '$($Matches['Username'].Trim())' in disconnected session '$($Matches['Id'])'"      }    } | Out-File -FilePath $LogFile -Append  ) 2>&1 | Write-Warning} 

I have a powershell script above that will logoff disconnected users in the list of servers. The output will show text files with values will only show if there are server/s in which user/s is disconnected, if not it will just be 0 KB or the results will just be blank. Can anyone help me to to output the result to csv with header and for no users disconnected it will have note: "No users/ disconnected"

August 2, 2017 at 8:54 am

Your code is just about unreadable. if you could format it using the


tags it would be a big help

August 3, 2017 at 2:22 am

how can i post it with a clearer format?