Remote Logoff Windows User

I ran into an issue were my RDP session go corrupted on a test server. I couldn’t logoff. I found this page that tells you how to force a logoff through PowerShell.

# Username to logoff
$userName = 'administrator'
# Get the session id from the server.  In this case the Server ID is 'DC'
$sessionId = ((quser /server:DC | Where-Object { $_ -match $userName }) -split ' +')[2]
# Display the session
$sessionId
# Logoff the Session ID from server DC
logoff $sessionId /server:DC

If you think hard about this you could put this into a script. Just don’t call it ‘logoff.ps1’ since that is a reserved keyword in PowerShell.

Download String or File with C#

Download a String:
https://docs.microsoft.com/en-us/dotnet/api/system.net.webclient.downloadstring?view=netframework-4.7.2

using System.Net;
...
/// <summary>
/// Download a string with a GET request
/// </summary>
/// <param name="address">URL</param> URL
/// <returns>Requested string</returns>
public static string DownloadString(string address)
{
WebClient client = new WebClient();
string reply = client.DownloadString(address);
return reply;
}

Download a File:
https://docs.microsoft.com/en-us/dotnet/api/system.net.webclient.downloadfile?view=netframework-4.7.2

string remoteUri = "http://www.contoso.com/library/homepage/images/"; string fileName = "ms-banner.gif", myStringWebResource = null; 
// Create a new WebClient instance.
WebClient myWebClient = new WebClient();
// Concatenate the domain with the Web resource filename. myStringWebResource = remoteUri + fileName;
Console.WriteLine("Downloading File \"{0}\" from \"{1}\" .......\n\n", fileName, myStringWebResource);
// Download the Web resource and save it into the current filesystem folder.
myWebClient.DownloadFile(myStringWebResource,fileName); Console.WriteLine("Successfully Downloaded File \"{0}\" from \"{1}\"", fileName, myStringWebResource);
Console.WriteLine("\nDownloaded file saved in the following file system folder:\n\t" + Application.StartupPath);