Tuesday, June 27, 2017

AutoHotKey - Control Your Mouse With Your Keyboard

     A month ago I bought a 40% keyboard. Some people might be wondering what a 40% keyboard is, it is just what you would think it is, it is 40% of the size of the standard 105 key layout. All the keys that have been removed are now stored in programmable layers. so for instance to make an exclamation mark I have to hold down, Fn1 + right shift + Capslock, this might seem insane but it is actually very intuitive after a week or so and now that I have fully adjusted I can hit any key without moving my fingers from the home row. Good-bye wrist pain!

Vortex Core



To be completely honest I did not think I would like using a 40% keyboard, especially as a daily driver. But after a week or two I found myself enjoying typing like seriously, typing is now fun and enjoyable.


Vortex Core



Alright now that we have gotten that out of the way, the point of this post is to share an AutoHotKey script that I have been running which allows me to control my mouse without actually having a mouse!

This script was developed using the AutoHotKey programming language and was aimed towards a 40% keyboard but with some slight modifications, it can work on any keyboard. Please review the comments and adjust accordingly for a full-size keyboard.









Here's the script in all its glory!
;
; MouseControl.ahk
; Hold down Control and then use the arrow keys to move the mouse cursor around the screen
; Updated on June 22nd, 2017 by Matt Kerfoot.
; Updated on June 27nd, 2017
; www.TheOvernightAdmin.com
;

#Persistent ; Keeps the script permanently running (that is, until the user closes it or ExitApp is encountered)

SetDefaultMouseSpeed, 1 ; Mouse speed, kind of like reaction time I do believe
distance = 30        ; how far the mouse moves each turn of the timer
multiplier = 1.16    ; how much farther (exponentially) the mouse moves in a direction the longer you hold that direction down
CFKM = 30            ; how often to run the timer
SetTimer, CheckForKeyMouse, %CFKM% ; Controls mouse movement speed
return ; end of mouse control movement control function

CheckForKeyMouse: ; Beginning of Function statement
if not GetKeyState("Control") ; Requires Ctrl to be held down
return ; required otherwise down control will not work

GetKeyState("Down")  ? (d*=multiplier) : (d:=1) ; While holding down shift, the down arrow will allow you to move the mouse to the down
GetKeyState("Up")    ? (u*=multiplier) : (u:=1) ; While holding down shift, the up arrow will allow you to move the mouse to the up
GetKeyState("Right") ? (r*=multiplier) : (r:=1) ; While holding down shift, the right arrow will allow you to move the mouse to the down
GetKeyState("Left")  ? (l*=multiplier) : (l:=1) ; While holding down shift, the left arrow will allow you to move the mouse to the left

y := (d-u) * distance ; math to tell how far to move the cursor
x := (r-l) * distance ; more math to define how far the cursor moves per key press

MouseMove, x, y, , R ; Initiates the action of moving the mouse
^PgDn::Click,      right ; Sets Ctrl + PgDn to perform a right click
^BackSpace::Click, right ; Sets Ctrl + backspace to perform a left click
^PgUp::Click,      left  ; Sets Ctrl + PgUp to perform a left click
^Esc::Click,       left  ; Sets Ctrl + Escape to perform a left click
^r::Reload  ; Assign Ctrl-R as a hotkey to restart the script.
Return ; End of Function statement


 I hope you enjoy it as much as I do! Any questions, just ask!

-Matt Kerfoot

Monday, June 12, 2017

Add Devices to LogicMonitor with Speed!

Here is a short PowerShell function that was written to speed up the time it takes to add servers to LogicMonitor.

I use this when a Domain Admin account cannot be obtained, this helps to keep security tight. This gets around it by adding the domain user account to the local Administrators and Distributed COM users group. It does even more than that even, it sets the PS execution policy, PS Remoting and it allows WMI through the windows firewall.

This script was developed for use with Server 2012R2 and newer however it should be backwards compatible all the way back to PowerShell version 2.0!

Function Add-ToLogicMonitor {
<#
.Synopsis
   Prepares a server to be added to LogicMonitor.
.DESCRIPTION
   This function will prepare a server to be added to LogicMonitor by enabling PS remoting, `
   setting the PS execution policy, adding LogicMonitorServiceAccountName to both the local administrator's group `
   and the Distributed DCOM Users group.
.EXAMPLE
   PS C:\> Add-ToLogicMonitor
.FUNCTIONALITY
   Used to speed up onboarding of servers into  LogicMonitor
#>

    [CmdletBinding()]
      
        Param(
               $Computer = $env:computername,
               $User = "LogicMonitorServiceAccountName",
               $ErrorActionPreference = "SilentlyContinue"
             )

                        # Enables PowerShell Remoting
                        Enable-PSRemoting -Force
                        Write-Host "PowerShell Remoting has been enabled." -BackgroundColor Black -ForegroundColor Green

                    # Sets the PowerShell Scripting Execution Policy to allow from the domain.
                    Set-ExecutionPolicy remotesigned -force
                    Write-Host "The Windows PowerShell Execution Policy has been set to allow scripts that are remotesigned." -BackgroundColor Black -ForegroundColor Green

                # Opens firewall for LogicMonitor support.
                netsh advfirewall firewall set rule group="windows management instrumentation (wmi)" new enable=yes
                netsh firewall set service RemoteAdmin enable

                Write-Host "Enables the required firewall rules for LogicMonitor to speak to the collector." -BackgroundColor Black -ForegroundColor Green

            # Enables remote WMI and DCOM
            Start-Service RemoteRegistry -Verbose
            Set-Service RemoteRegistry -StartupType Automatic -Verbose
            Write-Host "Remote WMI and DCOM have been enabled, the remote registry service has also been started and set to automatic startup." -BackgroundColor Black -ForegroundColor Green

        # Adds $User to the local administrators group
        $Administrators = [ADSI]("WinNT://$Computer/Administrators,group")
        $Administrators.add("WinNT://$User,user")
        Write-Host "$User has been added to the local Administrators group." -BackgroundColor Black -ForegroundColor Green

    # Adds $User to the Distributed COM users group
    $Administrators = [ADSI]("WinNT://$Computer/Distributed COM Users,group")
    $Administrators.add("WinNT://$User,user")
    Write-Host "$User has been added to the Distributed COM Users group." -BackgroundColor Black -ForegroundColor Green

}

Sunday, September 4, 2016

Download Earthporn wallpapers

This little PowerShell function will go out to Reddit and download all jpeg images and save them into the folder of your choosing. I recommend running it for about 5 minutes to get about 100 HD wallpapers. I say this because if you do not stop the script by hitting '[Ctrl] + c' or close the PowerShell window this script will slowly go through every page of this subreddit and could possibly fill up your hard drive.


Enjoy!

Function Download-EarthPorn {
<#
.Synopsis
   Downloads EarthPorn images from Reddit
.DESCRIPTION
   Goes out to reddit's EarthPorn subreddit and downloads all jpg images, `
   then it removes any smaller images that might not look so well as a wallpaper.
.EXAMPLE
   PS:\> Download-EarthPorn
#>
    [CmdletBinding()]
        Param(
            # URL to download images from.
            [Parameter(Mandatory=$False, ValueFromPipelineByPropertyName=$true, Position=0)]
            [string]$URL = "www.reddit.com/r/earthporn",
            # File location that you would like to save the downloaded images to.
            [Parameter(Mandatory=$False, ValueFromPipelineByPropertyName=$true, Position=)]
            [String]$Destination = "C:\Users\$env:USERNAME\Desktop\Earthporn_Wallpapers"
        )
    Begin {
            # Imports bittransfer cmdlets
            Import-Module BitsTransfer -verbose
          }
    Process{
             # checks if the file exists, else creates it.
             IF (!(Test-Path $Destination)) {New-Item -Path $Destination -ItemType Directory -Force | Out-Null}
                 Do {
                      $Links = (Invoke-WebRequest -Uri $URL).links
                      ($links | Where { $_.href -match ".jpg" } | Where { $_.class -match "title" }).href | Foreach {Start-BitsTransfer -Source $_ -Destination $Destination }
                      $URL = ($Links | Where { $_.innerHTML  -eq "next ›" }).href
                      $URL
                    } While (1 -eq 1)
           }
    End {
          # Removes smaller images.
          (Get-ChildItem -Filter *.jpg).FullName | % { $img = [Drawing.Image]::FromFile($_); if ($img.Width -lt 1680 -OR $img.Height -lt 1050) { Remove-Item $_ }}
        }
} # End of Download-EarthPorn function
Download-EarthPorn