Skip to content

Instantly share code, notes, and snippets.

View deangrant's full-sized avatar

Dean Grant deangrant

View GitHub Profile
Function Get-GeoIP {
<#
.SYNOPSIS
Retrieves IP geolocation data.
.DESCRIPTION
Connects to GeoIP REST API (www.telize.com/geoip) and retrieves JSON-encoded IP geolocation data and converts to custom objects.
.PARAMETER IP Address
Specify IP address or addresses to retrieve IP geolocation data.
.PARAMETER Protocol
@deangrant
deangrant / 0115-September Scripting Games Puzzle.ps1
Last active September 6, 2015 16:16
0115-September Scripting Games Puzzle.ps1
# Single line using one pair of curly brackets.
Import-Csv -Path .\Input.csv | ForEach-Object {$_ | Add-Member -MemberType NoteProperty -Name OSVERSION -Value (Get-WmiObject -Class Win32_OperatingSystem -ComputerName $_.MACHINENAME).Caption -PassThru} | Export-Csv .\Output.csv -NoTypeInformation
# Single line using no semicolons or curly brackets if host names are specified in the CSV file to import.
Get-WmiObject -Class Win32_OperatingSystem -ComputerName (Import-Csv .\Input.csv).MachineName | Select-Object PSComputerName,Caption | ConvertTo-Csv -NoTypeInformation | Select -Skip 1 | ConvertFrom-Csv -Header MACHINENAME, OSVERSION | Export-Csv .\Output.csv -NoTypeInformation
Exit ("1")
[Environment]::Exit("1")
@deangrant
deangrant / InheritVerbosePreference
Created October 3, 2015 10:38
Allows for verbose preference to inherited by functions in Windows PowerShell module
[CmdletBinding()]
Param (
$VerbosePreference = $PSCmdlet.GetVariableValue('VerbosePreference')
)
@deangrant
deangrant / Write-Log-AdvancedFunctionSnippet
Created October 3, 2015 11:10
Parameter block snippet from Write-Log function
[CmdletBinding()]
Param (
[ValidateScript({Get-Item (Get-Item $_).DirectoryName})]
[String]$LogFile = "",
[Parameter(Mandatory=$True)]
[String]$Message,
[ValidateSet('Info','Error','Warning')]
[String]$Level = "Info"
)
@deangrant
deangrant / Write-Log-SwitchConditionSnippet
Created October 3, 2015 11:19
Switch condition snippet from Write-Log function
Switch ($Level)
{
'Info' {$Event = ("" + (Get-Date -Format s) + ": INFORMATION: " + $Message + ".")}
'Warning' {$Event = ("" + (Get-Date -Format s) + ": WARNING: " + $Message + ".")}
'Error' {$Event = ("" + (Get-Date -Format s) + ": ERROR: " + $Message + ".")}
}
@deangrant
deangrant / Write-Log-ActionSnippet
Created October 3, 2015 11:23
Script block invocation for Write-Log function
# Conditional logic to determine event generation method.
If ($LogFile)
{
# Conditional logic to determine if the log file current exists in the location specified.
If (!(Get-Item $LogFile -ErrorAction SilentlyContinue))
{
# Creates the log file in the location specified.
New-Item $LogFile -Force -ItemType File | Out-Null
}
# Generates events in the current Windows PowerShell session specified and the log file specified.
@deangrant
deangrant / Create-ModuleStructure
Last active October 3, 2015 12:02
Snippet for Create-ModuleStructure script
[CmdletBinding()]
Param (
[String]
$ModuleName = "TestModule",
[String]
$Author = "",
[String]
[ValidateScript({Get-Item $_ })]
$Path = "C:\Sandbox\PowerShell\Modules"
)
@deangrant
deangrant / ScriptingGames_2015_October_Get-RSSFeed
Last active October 5, 2015 18:31
Entry to October 2015 Scripting Games
Function Get-RSSFeed {
<#
.SYNOPSIS
Retrieves most recent articles for an RSS feed(s).
.DESCRIPTION
Retrieves most recent articles for an RSS feed(s). By default, this will return the article headline. Optional parameters will retrive the link and description of the article.
This function requires Windows PowerShell 3.0 to in order to retrieve Uniform Resource Identifier (URI) using the Invoke-WebRequest cmdlet.
.PARAMETER Uri
Specify the Uniform Resource Identifier (URI) to send a request to the web page of the RSS feed(s).
@deangrant
deangrant / Create-EmailHash_ValidateScript_Snippet
Last active October 7, 2015 12:26
Snippet for validating email address in Create-EmailHash function
[CmdletBinding()]
Param (
[Parameter(Mandatory=$True, ValueFromPipeline=$True, HelpMessage="Enter an email address to compute hash")]
[ValidateScript({"\b[A-Z0-9._%+-]+@(?:[A-Z0-9-]+\.)+[A-Z]{2,4}\b"})]
[String[]]$Email
)