ps logoff inquiry

This topic contains 2 replies, has 2 voices, and was last updated by  Anonymous 11 months, 3 weeks ago.

  • Author
  • #76394


    [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"

  • #76408

    Richard Siddaway

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


    tags it would be a big help

  • #76523


    how can i post it with a clearer format?

You must be logged in to reply to this topic.