Skip to content

Instantly share code, notes, and snippets.

View josheinstein's full-sized avatar

Josh Einstein josheinstein

  • Einstein Technologies
  • USA
View GitHub Profile
@josheinstein
josheinstein / PSReadLine-AskAI.ps1
Created September 25, 2024 21:03
Adds a key handler to PSReadLine that when pressed, asks the user what they're trying to do, then uses Meta Llama 3 405B (via NVIDIA) to get a short PowerShell snippet (other CLIs work as well) and injects it into the input buffer WITHOUT executing it, so that the user can review and make edits as needed.
# To use:
# - Add an environment variable called NV_LLAMA_API_KEY and set it to a valid API key
# - Add the following script somewhere to your profile
# - At a *blank* PowerShell prompt, press Ctrl+` (backtick)
# - Type what you want to do at the prompt in plain english
# - The code generated by the LLM will be inserted into the input buffer without executing it
# - Make whatever changes are necessary to the generated command(s) and proceed as normal
#
# > Ask AI: recursively get all files in the current directory with the archive bit set
# > Get-ChildItem -Recurse -File | Where-Object { $_.Attributes -match 'Archive' }
@josheinstein
josheinstein / PSReadLine-GPT.ps1
Last active June 11, 2024 01:37
Adds a key handler to PSReadLine that when pressed, asks the user what they're trying to do, then uses GPT4 to get a short PowerShell snippet (other CLIs work as well) and injects it into the input buffer WITHOUT executing it, so that the user can review and make edits as needed.
# To use:
# - Add an environment variable called OPENAI_API_KEY and set it to a valid API key
# - Add the following script somewhere to your profile
# - At a *blank* PowerShell prompt, press Ctrl+` (backtick)
# - Type what you want to do at the prompt in plain english
# - The code generated by GPT4 will be inserted into the input buffer without executing it
# - Make whatever changes are necessary to the generated command(s) and proceed as normal
#
# > Ask GPT: recursively get all files in the current directory with the archive bit set
# > Get-ChildItem -Recurse -File | Where-Object { $_.Attributes -match 'Archive' }
@josheinstein
josheinstein / DynamicSort.cs
Created September 9, 2022 14:50
Adds OrderBy[Descending]/ThenBy[Descending] extension methods to IQueryable types that take a string parameter indicating the selector path, enabling parameterized ORDER BY clauses in LINQ queries.
/// <summary>
/// Adds OrderBy[Descending]/ThenBy[Descending] extension methods to IQueryable types
/// that take a string parameter indicating the selector path, enabling parameterized
/// ORDER BY clauses in LINQ queries.
/// </summary>
public static class DynamicSort
{
private static readonly Lazy<MethodInfo> OrderByMethod = new(() => GetQueryableMethod(nameof(Queryable.OrderBy), 2));
private static readonly Lazy<MethodInfo> OrderByDescendingMethod = new(() => GetQueryableMethod(nameof(Queryable.OrderByDescending), 2));
@josheinstein
josheinstein / Example
Last active March 23, 2022 18:47
Parsing Azure diagnostic blobs in Power BI
let
// Storage Account containing the blobs
Source = AzureStorage.Blobs("mystorageaccount"),
// Select the container containing azuread user risk events
Container = Source{[Name="insights-logs-userriskevents"]}[Data],
// Get only blobs for a specific day (parameter ReportDate must be defined)
// The tenantId below is a random example -- use your own tenantId, or add a parameter
@josheinstein
josheinstein / DateFunctions.sql
Last active June 11, 2024 01:37
Creates various MSSQL scalar functions to get dates relative to the current date.
CREATE FUNCTION [Today]() RETURNS date AS BEGIN RETURN CONVERT(date, GETUTCDATE()) END;
GO
CREATE FUNCTION [Tomorrow]() RETURNS date AS BEGIN RETURN CONVERT(date, DATEADD(day, 1, GETUTCDATE())) END;
GO
CREATE FUNCTION [Yesterday]() RETURNS date AS BEGIN RETURN CONVERT(date, DATEADD(day, -1, GETUTCDATE())) END;
GO
CREATE FUNCTION [ThisMonthStart]() RETURNS date AS BEGIN RETURN CONVERT(date, DATEADD(day, 1-DATEPART(day, GETUTCDATE()), GETUTCDATE())) END;
GO
CREATE FUNCTION [ThisMonthEnd]() RETURNS date AS BEGIN RETURN CONVERT(date, DATEADD(day, -1, DATEADD(month, 1, DATEADD(day, 1-DATEPART(day, GETUTCDATE()), GETUTCDATE())))) END;
GO
@josheinstein
josheinstein / LergDistanceFunction.sql
Last active July 18, 2019 20:07
Calculates the distance, in miles, between two LERG VH coordinates.
-- =============================================
-- Author: Josh Einstein
-- Create date: 2/19/2005
-- Description: Calculates the distance between two VH coordinates.
-- =============================================
ALTER FUNCTION [dbo].[DistanceBetween]
(
@v1 int,
@h1 int,
@v2 int,
@josheinstein
josheinstein / Connect-Office365.ps1
Last active June 11, 2024 01:39
Imports several Office 365-related modules into the session and authenticates using saved or supplied credentials.
<#
.SYNOPSIS
Imports serveral Office365-related modules into the session and authenticates using
saved or supplied credentials.
.DESCRIPTION
This script requires the following modules, which must be installed before running.
They will be imported automatically if they are installed. If they are not installed,
the script will return with an error.
- MSOnline
@josheinstein
josheinstein / SharePointPnpRecursive.ps1
Last active June 11, 2024 01:39
Recursively checks for files in a document library that are checked out to a particular user (or checked out at all).
#Requires -Module SharePointPnPPowerShellOnline
$ErrorActionPreference = 'Stop'
$SPUrl = 'https://yourtenant.sharepoint.com/sites/yourgroup'
# Recursively calls Get-PnpFolderItem for a given Document Library
function Get-PnpFolderItemRecursively($FolderSiteRelativeUrl) {
$Items = @(Get-PnPFolderItem -FolderSiteRelativeUrl $FolderSiteRelativeUrl)
foreach ($Item in $Items) {
@josheinstein
josheinstein / Http.csx
Created April 26, 2017 19:17
A C# script that contains helper extension methods for the HttpRequestMessage class, to simplify Azure Function development.
#r "System.Management.Automation"
#r "System.Web"
using System.Net;
using System.Net.Http;
public static string Query(this HttpRequestMessage request, string name, string defaultValue = null) {
var pairs = request.GetQueryNameValuePairs().Where(q => String.Equals(q.Key, name, StringComparison.OrdinalIgnoreCase));
if (pairs.Any()) { return pairs.First().Value; }
return defaultValue;
#.SYNOPSIS
# Removes all .DS_Store files from the current directory as well
# as any subdirectories.
#.DESCRIPTION
# .DS_Store is an artifact left by Mac OSX. On Mac that is harmless
# to remove, but otherwise pretty annoying. They tend to make their
# way into remote file shares and zip files and are not hidden by
# default in Windows.
function Remove-DSStore {