Skip to content

Instantly share code, notes, and snippets.

@midnightfreddie
midnightfreddie / MultiTableHtmlReport.ps1
Created December 10, 2016 19:47
Another from an unsaved ISE scratchpad. Was in reply to reddit post wanting multiple tables in an html page.
$Tables = @()
$Tables += Get-Process | ConvertTo-Html -Fragment | Out-String
$Tables += Get-Service | ConvertTo-Html -Fragment | Out-String
$Tables += Get-ChildItem $env:SystemDrive\Users | ConvertTo-Html -Fragment | Out-String
$Tables += Get-ChildItem HKLM:\SOFTWARE\ODBC\ODBC.INI | ConvertTo-Html -Fragment | Out-String
$Head = @"
<!doctype html>
<html>
@midnightfreddie
midnightfreddie / ConditionalColorChart.ps1
Created December 10, 2016 19:44
Another ISE scratch item. Adapted from web example to show can conditionally color chart bars based on value. In reply to something on reddit.
# load the appropriate assemblies
[void][Reflection.Assembly]::LoadWithPartialName(“System.Windows.Forms”)
[void][Reflection.Assembly]::LoadWithPartialName(“System.Windows.Forms.DataVisualization”)
# create chart object
$Chart = New-object System.Windows.Forms.DataVisualization.Charting.Chart
$Chart.Width = 500
$Chart.Height = 400
$Chart.Left = 40
$Chart.Top = 30
@midnightfreddie
midnightfreddie / JoinUrlParams.ps1
Created December 10, 2016 19:39
ISE scratch work example of using hashtable to create URL parameters. Was in reply to somewhere on reddit
$Parameters = @{
a = '{14}'
b = '{22}'
}
$UrlParameters = ($Parameters.Keys | ForEach-Object {
"{0}={1}" -f [System.Web.HttpUtility]::UrlEncode($_), [System.Web.HttpUtility]::UrlEncode($Parameters[$_])
}) -join "&"
$Uri = "https://<rootURI>/calc/add?$UrlParameters"
# Keeping this scratch ISE data. The WMI time conversion may be of interest (see also [Management.ManagementDateTimeConverter]::ToDateTime() )
# filterxml and filterxpath from https://www.reddit.com/r/PowerShell/comments/5hgr1g/help_getwinevent_filterxml_i_dont_get_it/ may also be of interest
$ElevenDaysAgo = [Management.ManagementDateTimeConverter]::ToDmtfDateTime((Get-Date ) - (New-TimeSpan -Days 11))
$ElevenDaysAgo = [Management.ManagementDateTimeConverter]::ToDmtfDateTime((Get-Date -Hour 0 -Minute 0 -Second 0 -Millisecond 0) - (New-TimeSpan -Days 11))
# Get-WmiObject -Class win32_ntlogevent -Filter "(logfile='System') and (SourceName='Microsoft-Windows-Winlogon') and (TimeGenerated > '$ElevenDaysAgo')"
Get-CimInstance -Class win32_ntlogevent -Filter "(logfile='System') and (SourceName='Microsoft-Windows-Winlogon') and (TimeGenerated > '$ElevenDaysAgo')"
break
@midnightfreddie
midnightfreddie / s3.sh
Last active August 22, 2016 03:58 — forked from chrismdp/s3.sh
Forked from someone else for my own reference. Originally from http://tmont.com/blargh/2014/1/uploading-to-s3-in-bash
# You don't need Fog in Ruby or some other library to upload to S3 -- shell works perfectly fine
# This is how I upload my new Sol Trader builds (http://soltrader.net)
# Based on a modified script from here: http://tmont.com/blargh/2014/1/uploading-to-s3-in-bash
S3KEY="my aws key"
S3SECRET="my aws secret" # pass these in
function putS3
{
path=$1
@midnightfreddie
midnightfreddie / VsCodeReg.ps1
Last active July 17, 2016 07:00
Add "Open in Code" to explorer context menu
<#
I've been having trouble getting the "Open in Code" Explorer context menus
to appear for Visual Studio Code. I think the problem is permissions on
the HKEY_CLASSES_ROOT\*\shell\VSCode reg key because I can see it as
admin but not as a user. (My admin accounts are different than my user
accounts.)
Setting these two keys/three properties in HKCU makes it work for me and
doesn't require admin permissions, so I made this rather than run the
reg scripts I'm seeing online.
# Given an array of rss xml document objects, return one rss document object with all the items sorted by date, descending
# Because of auto-deserialization, may need to build xml doc arrays with @(,$XmlDoc) trick
function Invoke-CombineRssXml {
[cmdletbinding()]
param (
[Parameter(Mandatory=$true,ValueFromPipeline=$true)]
[xml[]]$Xml
)
begin {
Write-Verbose "Combining rss xml documents"
# Based on http://www.powershellmagazine.com/2015/05/12/natively-query-csv-files-using-sql-syntax-in-powershell/
$csv = "$PSScriptRoot\test.csv"
$firstRowColumnNames = "Yes"
$delimiter = ","
$provider = (New-Object System.Data.OleDb.OleDbEnumerator).GetElements() | Where-Object { $_.SOURCES_NAME -like "Microsoft.ACE.OLEDB.*" }
if ($provider -is [system.array]) { $provider = $provider[0].SOURCES_NAME } else { $provider = $provider.SOURCES_NAME }
$connstring = "Provider=$provider;Data Source=$(Split-Path $csv);Extended Properties='text;HDR=$firstRowColumnNames;';"
$NinjaJs = @"
var NinjaQuerySelectorAll = function(selector) {
nodeList = document.querySelectorAll(selector);
output = [];
for (i=0; i < nodeList.length; i++) output.push(nodeList.item(i));
console.log("Ninja JS function output");
console.log(output);
return output;
}
"@
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
# I hate Write-Host, but the console output stream is uncoupled from the form, so we'll do this for illustration
function Write-MyOutput ($x) {
Write-Host "Entered Text: $x"
}
$objForm = New-Object System.Windows.Forms.Form
$objForm.Text = "Data Entry Form"