Skip to content

Instantly share code, notes, and snippets.

View vexx32's full-sized avatar
💜
Turn your faults into faultlore and share them with the world.

Rain Sallow (/u/ta11ow) vexx32

💜
Turn your faults into faultlore and share them with the world.
View GitHub Profile
function Invoke-SqlSelect {
<#
.DESCRIPTION
This is a re-usable function that makes a connection to the database and retrieves a result set.
#>
[CmdletBinding()]
param(
[Parameter(Mandatory, Position = 0)]
[string]
$Command,
namespace PSFSharp
open System.Management.Automation
open System.Management.Automation.Internal
[<Cmdlet(VerbsLifecycle.Invoke, "ConditionalAction",
ConfirmImpact = ConfirmImpact.Medium, SupportsShouldProcess = true)>]
[<Alias("?!")>]
type InvokeConditionalActionCommand() =
inherit PSCmdlet()
Get-Content -Path $File | ForEach-Object {
if ($_ -match '(?<MethodName>\w+)\((<Arguments>.*?)\) *(?={)') {
$matches['Method'] = $matches[0]
$matches.Remove(0)
[PSCustomObject]$matches
}
}
function prompt {
$Success = $?
$ConsoleWidth = ($host.UI.RawUI.WindowSize.Width, $Host.UI.RawUI.BufferSize.Width -gt 0)[0]
$Escape = "`e["
$EndEscape = "m"
$Slash = [IO.Path]::DirectorySeparatorChar
$Path = $ExecutionContext.SessionState.Path.CurrentLocation
$CurrentFolder = Split-Path -Path $Path -Leaf
$Host.UI.RawUI.WindowTitle = "PS $Path"
$string = 'hostname.database.table.schema'
if ($string -match '(?<Hostname>\w+)\.(?<Database>\w+)\.(?<Table>\w+)\.(?<Schema>\w+)') {
$matches.Remove(0)
[PSCustomObject]$matches
}
@vexx32
vexx32 / GetEnclosedArea.cs
Last active March 24, 2020 01:38
Gets the approximate area enclosed by an SKPath instance by comparing its total bounded area to the percentage of evenly-distributed points that are "contained" within the path.
internal static float GetEnclosedArea(this SKPath path)
{
SKRect bounds = path.TightBounds;
var boundedArea = bounds.Width * bounds.Height;
var totalPoints = 10000;
var enclosedPoints = 0;
for (float x = bounds.Left; x < bounds.Right; x += bounds.Width / (float)Math.Sqrt(totalPoints))
{
for (float y = bounds.Top; y < bounds.Bottom; y += bounds.Height / (float)Math.Sqrt(totalPoints))
// Generic this --> multiple type handling
internal static bool TryConvertSByte(double value, out sbyte outValue)
{
if (value < sbyte.MinValue || value > sbyte.MaxValue)
{
outValue = 0;
return false;
}
@vexx32
vexx32 / UnRemovableModule.ps1
Created August 10, 2018 15:42
Creates a test module that unconditionally throws an exception during the remove-module call
$Module = New-Module -Name 'ImportMe' -ScriptBlock {
class TryMeException : System.Exception {
TryMeException() : base() {}
TryMeException([string]$Message) : base($Message) {}
}
$onremove = {
throw [TryMeException]::new("HOW DARE YOU STEAL THAT CAR")
}
$ExecutionContext.SessionState.Module.OnRemove += $onremove
}
function New-Shortcut {
[CmdletBinding()]
Param(
[Parameter(Mandatory)]
[ValidateScript({ Test-Path -IsValid $_ })]
[string]$Path,
[Parameter(Mandatory, ValueFromPipeline)]
[ValidateScript({ Test-Path $_ })]
[string]$Target,
@vexx32
vexx32 / CSV_NoHeader.ps1
Created November 2, 2018 15:07
Trim the header row of a CSV
$Properties = 'GivenName', 'Surname', 'Department', 'EmailAddress', 'Office'
Get-ADUser -Filter 'Department -like "*"' -Properties $Properties |
Select-Object -Property $Properties |
ConvertTo-Csv -NoTypeInformation | # Convert to CSV string data without the type metadata
Select-Object -Skip 1 | # Trim header row, leaving only data columns
Set-Content -Path "c:\emailaddress.csv"