Call me...

Friday, June 4, 2021

Set NumLock for All / Current user via Powershell

Keep in mind that the below commands can also be wrapped into an 'Invoke-Command' - You just need to make sure WinRM* is enabled on the remote computer - 

I'll list out below, how I deal with WinRM enablement.*

Get each of these values:

NOTE - Two different Registry locations; 'HKEY_USERS\.DEFAULT', and 'HKEY_CURRENT_USER'

(Get-ItemProperty -Path Registry::"HKEY_USERS\.DEFAULT\Control Panel\Keyboard" -Name InitialKeyboardIndicators).InitialKeyboardIndicators

(Get-ItemProperty -Path Registry::"HKEY_CURRENT_USER\Control Panel\Keyboard" -Name InitialKeyboardIndicators).InitialKeyboardIndicators 

If either of these are set to either '0', or '2147483648'...

Add '2' to them, and set the new value accordingly - 
So, '0' becomes '2'
And '2147483648' becomes '2147483650'


Set-ItemProperty -Path Registry::"HKEY_USERS\.DEFAULT\Control Panel\Keyboard" -Name InitialKeyboardIndicators -Value 2

Set-ItemProperty -Path Registry::"HKEY_USERS\.DEFAULT\Control Panel\Keyboard" -Name InitialKeyboardIndicators -Value 2147483650

Set-ItemProperty -Path Registry::"HKEY_CURRENT_USER\Control Panel\Keyboard" -Name InitialKeyboardIndicators -Value 2

Set-ItemProperty -Path Registry::"HKEY_CURRENT_USER\Control Panel\Keyboard" -Name InitialKeyboardIndicators -Value 2147483650

* The WinRM stuff:
Hint: Your gonna need PsExec from the SysInternals suite...
(Also, forgive the aliases I used here and there...)

$Computer_Name = "ComputerName" 

# WinRM test 
$EAC = $ErrorActionPreference 
$ErrorActionPreference = "SilentlyContinue" 
If (!((Test-NetConnection $Computer_Name).PingSucceeded)) {Write-Host "   $Computer_Name is unreachable..."; break
    $TestCommand = $null; $TestCommand = Test-WSMan -ComputerName $Computer_Name 
    If (!($TestCommand)){ 
    Write-Host " Enabling WinRM... " -No 
    C:\SysInternals\PsExec.exe -s -nobanner \\$Computer_Name /accepteula cmd /c "c:\windows\system32\winrm.cmd quickconfig -quiet" | Out-Null 
    $TestCommand = $null; $TestCommand = Test-WSMan -ComputerName $Computer_Name 
    If (!$TestCommand){Write-Host "Windows Remote Managment (WinRM) did not enable... Can't run this - EXITING." -Fore Yellow -Back DarkMagenta; Break

If ($TestCommand){ Write-Host "  WinRM enabled." -Fore Yellow -Back 2
$ErrorActionPreference = $EAC 
# END WinRM test

Saturday, February 6, 2021

Swap mouse buttons via Powershell

I'm a southpaw, but I can mouse with either hand...
At home, I have the mouse on the left, at work, on the right... For numerous reasons...

Occasionally, I run into situations where I need to swap mouse buttons on my home computer, when I remote connect to something set up for right-hand

This script will do a quick mouse-button swap. 

Though it includes all of the mechanics needed to interact with this setting, for any other scenario.

Note that: 

Just changing the registry value, will work... But, you'd have to reboot.
Pushing that 'true / false' value, to the '
user32.dll' file, does the swap on-the-fly (it also changes the reg key value).
You may need to adjust that '
1..10', (increase the value past '10', I mean) if the second time the 'MouseReport' runs, and the swap does not show as changed yet... It does take a second or two...

= Add-Type -MemberDefinition @'
public static extern bool SwapMouseButton(bool swap);
-Name "NativeMethods" -Namespace "PInvoke" -PassThru
Function MouseReport {$script:MouseButton = $null; $script:MouseButton = (Get-ItemProperty "HKCU:\Control Panel\Mouse").SwapMouseButtons
If ($MouseButton -eq 0) {Write-Host " Mouse button is set for RIGHT-Handed mouse. " -ForegroundColor Red -BackgroundColor Yellow}
If ($MouseButton -eq 1) {Write-Host " Mouse button is set for LEFT-Handed mouse. " -ForegroundColor Green -BackgroundColor Black}}
    $Choice = @('Y','X'); Do { Write-Host "  Swap?: (Y) / (X): " -NoNewline -ForegroundColor Yellow
    $Swap = (Read-Host).Trim()} Until (($Choice.Contains($Swap.ToUpper()))); If ($Swap -eq "x"){break}
# $true for Left-Handed, $false for Right-Handed.
        # Mouse is Right-Handed, change it to Left-Handed
            If ($MouseButton -eq 0) {[bool]$returnValue = $swapButtons::SwapMouseButton($true)}
# Mouse is Left-Handed, change it to Right-Handed
            If ($MouseButton -eq 1) {[bool]$returnValue = $swapButtons::SwapMouseButton($false)}
1..10 | % { #Give the system time to respond to the above change to the "user32.dll" change.
    If ($_ -lt 10) {Write-Host "." -ForegroundColor Cyan -NoNewline};Start-Sleep -Milliseconds 200
    If ($_ -ge 10) {Write-Host "." -ForegroundColor Cyan}}

Monday, December 28, 2020

Powershell - Password generators, a different take.

 A different take on generating random passwords - Let me explain...
I found myself, or the person I was on the phone with...
Struggling to locate, on their keyboards, most of the special characters used by password generators.
I've also had many an eyeroll, talking to someone who got a temporary password emailed to them, but they could not tell the difference between: O and 0, or l, I, and 1... (see comments in the script), and ended up locking their account, thus having to call me, to tell me the password I sent does not work - (queue 'eyeroll')

The below script, just gets rid of those problematic characters.
Making it ~ MUCH EASIER TO COMMUNICATE ~ a random password
I use THIS to create passwords, now.

 And it tests for AD password complexity too... (see comments in the script)

  • Tell it how long the password should be (15 is the max length, but you can add to the 'PWDgen' function... Though, that'd be kinda' mean.)
  • Tell it how many passwords to generate...

= 8 # How many characters long (max is 15).
$HowMany = 6 # So you can pick from an assortment.

   ############## password generator - Some characters removed:
   # '1' (number one), 'I' (letter 'eye' upper case), and 'l' (letter 'el' lower case) - look to similar
   # '0' (number zero), and 'O' (letter 'oh', uppercase) - look to similar
   # Special characters that are ambiguous, less common, difficult to explain on the phone, or harder to locate on the keyboard.

# AD standard password complexity:
   #  One character from at least three, of these four categories: Upper Alpha, Lower alpha, Number, Special character.

SpecChar {("!@#$%&".tochararray()) | Get-Random}
Function UpperAlpha {("ABCDEFGHJKLMNPQRSTUVWXYZ".tochararray()) | Get-Random}
Function LowerAlpha {("abcdefghijkmnopqrstuvwxyz".tochararray()) | Get-Random}
Function Number {("23456789".tochararray()) | Get-Random}
Function WildCard {("!@#$%&23456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz".tochararray()) | Get-Random}

Function PWDgen {
$Password = ((
Number)+(Number)+(Number) +`
WildCard)+(WildCard)+(WildCard)).tochararray() | sort {Get-Random})[0..($Length - 1)] -join ''
Return $Password

Write-Host "Here's a bunch of random, $Length character passwords:" -ForegroundColor Cyan
$Counter = 1
..$HowMany | % {
Write-Host "$($Counter): "
    Do {$Password00 = PWDgen # Force AD password complexity compliance - At least three out of four
    $Rule = 0
    If (($Password00 -cmatch "[A-Z]")) {$Rule++}
If (($Password00 -cmatch "[a-z]")) {$Rule++}
If (($Password00 -match ".*\d+.*")) {$Rule++}
If (($Password00 -match '[^a-zA-Z.*\d+.*]')) {$Rule++}
Until ($Rule -ge 3)
Write-Host $($Password00) -ForegroundColor Yellow