Skip to content

Instantly share code, notes, and snippets.

View fearthecowboy's full-sized avatar
🏠
Happily Working from home

Garrett Serack fearthecowboy

🏠
Happily Working from home
View GitHub Profile
@fearthecowboy
fearthecowboy / PowerShellWebServer.ps1
Last active September 9, 2021 00:40
PowerShell Webserver that restarts when you edit the script.
#
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@fearthecowboy
fearthecowboy / universal.ps1.cmd
Created March 9, 2021 21:02
This file can be saved as .ps1 or .cmd and works for both. Even supports cmdline arguments!
@(echo off) > $null
if #ftw NEQ '' goto :init
($true){ $Error.clear(); }
# unpack arguments if they came from CMD
$hash=@{};
get-item env:argz* |% { $hash[$_.name] = $_.value }
if ($hash.count -gt 0) {
$args=for ($i=0; $i -lt $hash.count;$i++) { $hash["ARGZ[$i]"] }
}
@fearthecowboy
fearthecowboy / sample.cmd
Last active March 3, 2021 22:48
This file is a legal .cmd script and a legal powershell script.
@(echo off) > $null
set null( New-Module -ScriptBlock { function goto { }; function :{ } } )#=
: # This file is both a .cmd script and a powershell script. if you save it as
: # sample.cmd and run it it from cmd.exe it will run the same as if you
: # saved it as sample.ps1 and run it.
: # well, in this case, I added some echos on the cmd side...
goto :CMDSTART
Set-ExecutionPolicy -Scope LocalMachine Unrestricted -force
$ProgressPreference=0
function ResolvePath {
param (
[string] $FileName
)
$FileName = Resolve-Path $FileName -ErrorAction SilentlyContinue `
-ErrorVariable _frperror
Object.defineProperties(Array.prototype, {
where: { value: Array.prototype.filter },
select: { value: Array.prototype.map },
any: { value: Array.prototype.some },
all: { value: Array.prototype.every },
insert: { value: function (position: number, items: Array<any>) { return (<Array<any>>this).splice(position, 0, ...items); } },
selectMany: { value: function (callbackfn: (value: any, index: number, array: Array<any>) => Array<any>) { return (<Array<any>>this).select(callbackfn).flat(); } },
groupByMap: { value: function (keySelector: (each: any) => any, selector: (each: any) => any) {
const result = new Map<any, Array<any>>();
@fearthecowboy
fearthecowboy / split.js
Last active February 12, 2020 17:47
Split identifier into words
const identifier = "SomethingNOTGarrettButTLAsInASetOfEverLongerWordsTLAWithIsOfAPIsAPIssNotNOManThatYouUseWith_a_TLA";
// split identifier into words
identifier.replace(/([a-z]+)([A-Z])/g, '$1 $2').
replace(/(\d+)/g, ' $1 ').
replace(/\b([A-Z]+)([A-Z])s([^a-z])(.*)/g, '$1$2« $3$4').
replace(/\b([A-Z]+)([A-Z])([a-z]+)/g, '$1 $2$3').
replace(/«/g, 's').trim().
split(/[\W|_]+/)
// returns:
@fearthecowboy
fearthecowboy / elevate.ps1
Last active September 3, 2019 11:34
Better elevation script for powershell
#
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@fearthecowboy
fearthecowboy / new-alias.ps1
Created February 1, 2019 00:48
A better new-alias for PowerShell
<#
.SYNOPSIS
Creates a new alias. Better than original New-Alias
.DESCRIPTION
Creates a new Function Alias. Unlike the original New-Alias, this will let you
create an alias that can be a command line or script, in a single command.
@fearthecowboy
fearthecowboy / profiles.json
Last active August 7, 2019 22:40
My Terminal profiles.json
{
"globals": {
"alwaysShowTabs": true,
"defaultProfile": "{61c54bbd-c2c6-5271-96e7-009a87ff44b0}",
"initialCols": 160,
"initialRows": 50,
"keybindings": [
{
"command": "closePane",
"keys": [
@fearthecowboy
fearthecowboy / example.msbuild.xml
Created January 6, 2015 15:32
Super Simple PowerShell task for MSBuild.
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<!-- @FearTheCowboy's Simple PowerShell Task for MSBuild -->
<UsingTask TaskName="PowerShell" TaskFactory="CodeTaskFactory" AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.v12.0.dll">
<ParameterGroup><ScriptBlock ParameterType="System.String" Required="true" /></ParameterGroup>
<Task><Reference Include="$(MSBuildToolsPath)\Microsoft.Build.Tasks.v12.0.dll"/><Code Type="Class" Language="cs"><![CDATA[
public class PowerShell : Microsoft.Build.Tasks.Exec {
public string ScriptBlock {set { EchoOff=true; Command = string.Format( "@powershell \"Invoke-Command -ScriptBlock {{ $errorActionPreference='Stop'; {0} ; exit $LASTEXITCODE }} \"", value.Replace("\"","\"\"").Replace("\r\n",";").Replace("\n",";").Replace("\r",";")); } }
}]]></Code></Task>
</UsingTask>