Category: powershell

Send a message to users RDP’d to server

There are 2 users currently logged on a server via remote desktop protocol (RDP) and want to asked them to log out while I perform maintenance. There is a utility in Windows called MSG . It is a utility to send a message to a user that’s currently logged onto a system.

To send a message to a remote server, from the terminal:

$>  msg * /server:SERVER1 /time:30 /v "Could either USER1 or USER4 logout until 1PM? I need access to prepare for the migration next week"
  • * : I’m sending the message to all sessions
  • /server: The name of the server I’m sending the message to.
  • /time:30 : I’m giving the users 30 seconds to acknowledge the message. If no time is listed, the message will stay on the screen until the users click OK.
  • /v : verbose
  • The message to the users should be in quotes
The message as it appears to the users logged into the server

This works in both PowerShell and command prompt.

Import your own module into a PowerShell Script

I wrote a script to get the mac address off of several servers, so it required my username and password. I don’t like to put my password in a script, but the credentials pop-up box for me to enter my password got a bit cumbersome. I decided to put it an a separate file for 2 reasons, 1) so my password is not saved in a script (I delete from the file once I’m done) and 2) making scripts modular is what it’s all about.

$user='domain\username'
Import-Module C:\Users\username\Documents\WindowsPowerShell\cred.ps1
$servers= @('sugar', 'milk', 'eggs', 'server7', 'buttons')

foreach ($server in $servers) {
    Get-WmiObject -ClassName Win32_NetworkAdapterConfiguration -Filter "IPEnabled='True'" -ComputerName $server | 
    Select-Object -Property MACAddress, Description
    Write-host $server
}

I wrote my script in one file and my password in a variable in another.

$pwd='$upr$trong$3cure'

To import it into my script, I added the Import-Module command and the path to my password file and ran the script. No more password prompts.

User Powershell to see which AD groups a users belongs to

For the past upteen years, to get a list of the groups a user belongs to, I’ve used:

> net user /domain userID

Today, I just stumbled upon a powershell cmdlet that does just that.
Get-ADPrincipalGroupMembership

To get a list of groups a users belongs to run the following at a powershell prompt.

PS c:\ >Get-ADPrincipalGroupMembership userID | select name

 

Screenprint of Powershell window

Note: You will need the Active Directory Powershell module and that comes from installing RSAT.

Compare 2 files with PowerShell

I’m building a new 2012R2 server that will replace an existing 2008R2 application server. I want to see what roles I need to install on the new server, so I run the Get-WindowsFeature cmdlet in PowerShell to give me what I need.

PowerShell Get-WindowsFeature Output

I have saved the output of Get-WindowsFeature to .csv on 2 servers, a new server and the old, production server.

PS> Import-Module servermanager ; get-windowsfeature | where-object {$_.Installed -eq $True} | format-list DisplayName > old-roles.csv

PS> Import-Module servermanager ; get-windowsfeature | where-object {$_.Installed -eq $True} | format-list DisplayName > new-roles.csv

To compare the files, use the compare-object cmdlet.

From Microsoft:

The Compare-Object cmdlet compares two sets of objects. One set of objects is the “reference set,” and the other set is the “difference set.”

PS> compare-object $(Get-Content K:\old-roles.csv) $(Get-content K:\new-roles.csv) -includeequal

 

Side Indicator Meaning
== Item appears in both files
=> Item only appears in the difference set
<= Item only appears in the reference set

PowerShell compare-object output

The -ReferenceObject and -DifferenceObject options weren’t needed in this example. I got the same output with and without it.

So, on both new and old server, File Server, Remote Server Administration tools, are installed.
The new server has its own default roles and features installed and the old server has the list of roles that must be installed onto the new server, if applicable. The old server is 2008 and the new server is 2012 R2. Many features are now either default (PowerShell) or updated (.NET Framework 4.5). This list isn’t one to one, but it gives me an idea what needs to be installed in order to migrate a service from one OS to another. T

There are many useful applications for diff’ing files. What are some of your most common uses?

In addition:

You can assign the files to variables and use the split() function to get rid everything up to a delimiter and output the ‘newly cleaned’ file to a new file.

$old = Get-Content .\old-roles.csv
$new = Get-Content .\new-roles.csv

Get-Content $old | ForEach-Object {$_.split(":")[1] } > sorted-old-roles.txt
Get-Content $new | ForEach-Object {$_.split(":")[1] } > sorted-new-roles.txt

$newsort = ".\sorted-new-roles.txt"
$oldsort = ".\sorted-old-roles.txt"
Compare-Object $(Get-content $oldsort) $(Get-Content $newsort) -includeequal

Split Fucnction results

Of course, just using the diff command on both files in Linux would have given me what I needed in 5 seconds, but as we know, it’s all about learning the process.

Linux diff output
Linux would have made comparing the files a little bit easier

Gutter Markings Meaning
White space These lines are common in both files. In both files, you have chicken breast, tofu and naan.
> The files differ here. the second file listed in the command has this entry. The first file doesn’t, like milk, eggs, bread and broccoli.
< The files differ. The first file only has this entry. The second file doesn’t, like avocado, almond mil and peanut butter.
| These lines differ, meaning if we were to merge the two files, we’d have to resolve if we want to keep one or both entries.

-ShowWindow in Powershell help

The adage, you learn something new everyday is very true.

I’m reading a book called Learn Powershell in a Month of Lunches by Don Jones and Jeff Hicks and I just discovered a helpful nugget I didn’t know existed, -ShowWindow.

Example:
help Get-ADGroupMember -showwindow

The -ShowWindow parameter will give you a popup window with the help topic you’re researching.  You can search words and phrases within the help topic. The found term is highlighted for easy reading and you can move between terms using the previous and next buttons. The window can be resized and you can increase or decrease the text with the slider at the bottom of the window. The description is a bit shorter for some cmdlets, but there are even some command examples displayed in the help window to get you going without coming out of your prompt.

The PowerShell help window
-ShowWindow

Gone are the days of opening a second Powershell window to reference the help while crafting command line syntax. -ShowWindow is a great too in the Powershell arsenal.

 

~Note~ This works differently in PS 4.0 and 5.0. and within different builds of Windows 10. 
Some contents are missing or out of order. It appears it is a known issue.
Your mileage may vary.

A curved arrow pointing right Don’t forget Help Cmdletname -online It launches help in the browser. Keeping you in your PS window without taking you away from your prompt.

 

Check the hash on a file using PowerShell

Checking the hash of a file you’ve downloaded from the internet is very good practice. It’s a way to ensure the file you’re downloading hasn’t been tampered with, according to the hash provided on the site you’re downloading from.

Since Powershell 4.0, Get-FileHash has been a way to check the hash signature of a file.

A powershell windows showing the Get-FileHash command

Get-FileHash -Path C:\Path\to\file\file.exe -Algorithm SHA256

Get-FileHash can check the following signatures:

  • SHA1
  • SHA256 (default value)
  • SHA384
  • SHA512
  • MACTripleDES
  • MD4
  • RIPEMD160
Social media & sharing icons powered by UltimatelySocial