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

Windows Spotlight Wallpapers

If you are anything like me then you were super impressed by the new wallpapers Windows has been pushing out to Windows 10 machines. Well here is a PowerShell function that you can run to find them all, rename them and then sort them into two folders based on the width of the images, some will go into a phone wallpaper folder while the larger images will be saved in the default folder on your desktop.



Function Get-WindowsSpotlightWallpapers{
<#
.Synopsis
   Grabs Windows Spotlight Wallpapers
.DESCRIPTION
   Grabs Microsoft Spotlight Wallpapers and moves them to your desktop, filters out the wallpapers by width and moves the skinny images to a folder for phone wallpapers.
.EXAMPLE
   PS :\> Get-WindowsSpotlightWallpapers
#>
    [CmdletBinding()]
    Param (
            # Path to spotlight files
            [Parameter(Mandatory=$false, ValueFromPipelineByPropertyName=$true,Position=0)]
            $Path = "C:\Users\$env:USERNAME\AppData\Local\Packages\Microsoft.Windows.ContentDeliveryManager_cw5n1h2txyewy\LocalState\Assets"
          )
            # Moves to the directory the wallpapers are stored in.
            Set-Location -Path $Path
                # copies wallpapers to  folder on you desktop so we don't currupt the originals at all.
                Copy-Item -Path $Path -Recurse -Destination "C:\Users\$env:USERNAME\Desktop\Windows Spotlight Wallpapers\" -Verbose
                    # renames files with .jpeg extention.
                    Get-ChildItem -Path "C:\Users\$env:USERNAME\Desktop\Windows Spotlight Wallpapers\" | Rename-Item -NewName { [io.path]::ChangeExtension($_.name, "jpeg") } -Verbose
                        # deletes files smaller than 25000
                        Get-ChildItem -Path "C:\Users\$env:USERNAME\Desktop\Windows Spotlight Wallpapers\" | Where-Object {$_.Length -lt "25000"} | Remove-Item -Verbose
                           # creates a folder for the phone wallpapers
                            New-Item -Path "C:\Users\$env:USERNAME\Desktop\Windows Spotlight Wallpapers\Windows Spotlight Phone Wallpapers" -ItemType Directory -Verbose | Out-Null
                            # filter files by image width then moves small files to phone wallpaper directory.
                            Function Get-Image{
                                     [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing") | Out-Null
                                      $fi=[System.IO.FileInfo]$_          
                                      if( $fi.Exists){
                                                       $img = [System.Drawing.Image]::FromFile($_)
                                                       $img.Clone()
                                                       $img.Dispose()      
                                      } else { Write-Host "File not found: $_" -fore yellow }  
                            }
                            $PhoneWallpapers = Get-ChildItem "C:\Users\$env:USERNAME\Desktop\Windows Spotlight Wallpapers\*.jpeg" -Recurse | % {
                                $image = $_ | Get-Image
                                    New-Object PSObject -Property  @{
                                                                        File = $_.name
                                                                        Fullname = $_.Fullname
                                                                        Height = $image.Height
                                                                        Width = $image.Width
                                    };
                            }
                        # starts 5 second sleep timer
                        Start-Sleep -Seconds 5
                # Goes through wallpapers and moved the smaller width files to the phone wallpaper directory.
                foreach($p in $PhoneWallpapers){
                    if(($p).Width -lt "1081"){
                        Move-Item -Path ($p).fullname -Destination "C:\Users\$env:USERNAME\Desktop\Windows Spotlight Wallpapers\Windows Spotlight Phone Wallpapers\" -Verbose -Force
                        }
                    if(($p).Width -eq "272"){
                        Move-Item -Path ($p).fullname -Destination "C:\Users\$env:USERNAME\Desktop\Windows Spotlight Wallpapers\Windows Spotlight Phone Wallpapers\" -Verbose -Force
                        }
               # starts 5 second sleep timer
               Start-Sleep -Seconds 5
                    if(($p).Width -lt "1081"){
                        Move-Item -Path ($p).fullname -Destination "C:\Users\$env:USERNAME\Desktop\Windows Spotlight Wallpapers\Windows Spotlight Phone Wallpapers\" -ErrorAction SilentlyContinue -Verbose -Force
                        }
                    if(($p).Width -eq "272"){
                        Move-Item -Path ($p).fullname -Destination "C:\Users\$env:USERNAME\Desktop\Windows Spotlight Wallpapers\Windows Spotlight Phone Wallpapers\" -ErrorAction SilentlyContinue -Verbose -Force
                        }
                }
Set-location "C:\Users\$env:USERNAME\Desktop\Windows Spotlight Wallpapers\"
Invoke-Item "C:\Users\$env:USERNAME\Desktop\Windows Spotlight Wallpapers\"
} # end of function.
Get-WindowsSpotLightWallpapers



After running the script I would recommend setting the newly created folder on your desktop as the folder windows uses to grab you wallpapers from.

Simply hit the windows key and start tyoing 'wallpaper' and you should see something that says choose background....

Enjoy!