Skip to content

Instantly share code, notes, and snippets.

View gist:50c07e415e42478833d1
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 Sep 6, 2015
0115-September Scripting Games Puzzle.ps1
View 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
View ExitCodeSnippet
Exit ("1")
[Environment]::Exit("1")
@deangrant
deangrant / InheritVerbosePreference
Created Oct 3, 2015
Allows for verbose preference to inherited by functions in Windows PowerShell module
View InheritVerbosePreference
[CmdletBinding()]
Param (
$VerbosePreference = $PSCmdlet.GetVariableValue('VerbosePreference')
)
@deangrant
deangrant / Write-Log-AdvancedFunctionSnippet
Created Oct 3, 2015
Parameter block snippet from Write-Log function
View Write-Log-AdvancedFunctionSnippet
[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 Oct 3, 2015
Switch condition snippet from Write-Log function
View Write-Log-SwitchConditionSnippet
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 Oct 3, 2015
Script block invocation for Write-Log function
View Write-Log-ActionSnippet
# 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 Oct 3, 2015
Snippet for Create-ModuleStructure script
View Create-ModuleStructure
[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 Oct 5, 2015
Entry to October 2015 Scripting Games
View ScriptingGames_2015_October_Get-RSSFeed
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 Oct 7, 2015
Snippet for validating email address in Create-EmailHash function
View Create-EmailHash_ValidateScript_Snippet
[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
)