Skip to content

Instantly share code, notes, and snippets.

@nzbart
nzbart / ComEmuTasks
Last active February 18, 2018 21:31
ConEmu task for launching PowerShell with the Visual Studio environment variables and path correctly configured.
%comspec% /k ""C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\Tools\VsDevCmd.bat" & powershell" -new_console:d:D:\Dev
Admin:
%comspec% /k ""C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\Tools\VsDevCmd.bat" & powershell" -new_console:a
@nzbart
nzbart / CollectPerformanceMetrics.ps1
Last active February 8, 2017 17:44
Starts logging performance metrics on Windows using perfmon logging (logman).
<#
Starts logging performance metrics on Windows using perfmon logging (logman).
A variety of metrics are collected to assist with diagnosing a performance bottleneck.
The metrics are logged to %systemdrive%\PerfLogs\Admin on a clean Windows install.
The script must be run with administrative credentials.
This script is intended for interactive execution. If you want to run it within an automated process, you'll need to check the exit code of logman.exe. The stop / delete steps are expected to fail if the job does not already exist.
I use the .blg file format to capture data because it can capture new processes that start during the capture. Because it is not possible to add new columns to the .csv, new processes that launch during the logging period cannot be added when logging to .csv.
The .blg file can be opened in Windows Performance Monitor (perfmon).
The perfmon .blg file can be converted into a .csv file by the following:
@nzbart
nzbart / NetworkCapture.psm1
Last active May 12, 2020 15:45
Run a network capture on Windows without installing Wireshark or any other tools on your servers.
#requires -version 3
$ErrorActionPreference = 'Stop'
$networkTraceFileName = 'NetworkTrace'
function LaunchSingleRemoteCommand([string][parameter(mandatory)]$ComputerName, [pscredential]$Credential, [System.Management.Automation.Runspaces.AuthenticationMechanism]$Authentication, [scriptblock][parameter(mandatory)]$ScriptBlock)
{
$args = @{
ComputerName = $ComputerName
@nzbart
nzbart / IsAdministrator.ps1
Created July 9, 2014 01:59
Determine whether user has administrative privileges in PowerShell
(New-Object Security.Principal.WindowsPrincipal ([Security.Principal.WindowsIdentity]::GetCurrent())).IsInRole([Security.Principal.WindowsBuiltinRole]::Administrator)
@nzbart
nzbart / Plotting a chart with R.Net.r
Last active August 29, 2015 14:12
This sample plots a simple chart to a PNG file using the R statistical program and R.Net. It requires the R.NET.Community NuGet package to be installed.
using RDotNet;
/*
d:/temp/plot.r:
suppressPackageStartupMessages(library(ggplot2))
library(ggplot2)
p <- qplot(Sepal.Length, Petal.Length, data = iris, color = Species)
png(filename=fileName, width=500, height=500)
iislog <- read.delim("Filtered.log", header=TRUE, sep=" ")
iislog <- within(v, { localdatetime=as.POSIXct(paste(date, time)) + 13*60*60 })
h <- hist(
iislog$localdatetime,
freq = TRUE,
breaks=24,
xlab = "Time of day",
ylab = "Requests per hour",
axes = F,
@nzbart
nzbart / GetSqlAgentJobs.ps1
Last active August 29, 2015 14:13
Get list of SQL Agent jobs and their corresponding schedules.
#requires -modules sqlps
#requires -version 3
param([string]$ServerToQuery = ([Environment]::MachineName), [PSCredential]$SqlCredential)
$ErrorActionPreference = 'Stop'
pushd
ipmo sqlps -DisableNameChecking
popd
@nzbart
nzbart / Add-TabExpansion.ps1
Created May 14, 2015 00:19
Chain a PowerShell script block to the tabexpansion2 function, calling the original implementation if the new one doesn't return a result.
function Add-TabExpansion
{
param([ScriptBlock][parameter(mandatory)]$ScriptBlock)
$originalFunction = (gcm TabExpansion2).ScriptBlock
$chainedBlock = {
$userResult = & $ScriptBlock @Args
if($userResult) {
return $userResult
}
@nzbart
nzbart / MapKeys.reg
Created May 15, 2015 12:44
Registry file that maps the right Windows key to the Apps (context menu) key, and the scroll-lock key to the numeric keypad asterisk key (one of the most useful keys on the keyboard). These mappings are useful the the tenkeyless (TKL) Coolermaster CM QuickFire Rapid-i keyboard, which comes inexplicably with two Windows keys, and no Apps key. Not…
Windows Registry Editor Version 5.00
[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Keyboard Layout]
"Scancode Map"=hex:00,00,00,00,00,00,00,00,03,00,00,00,5d,e0,5c,e0,37,00,46,00,\
00,00,00,00
@nzbart
nzbart / CreateGitIgnores.ps1
Last active September 17, 2015 22:17
Creates more optimal .gitignore files. Run from within your repository, and delete any .gitignore files you are not sure about first. You should make sure that all code is committed first.
$ErrorActionPreference = 'Stop'
$filesToIgnore = git status | ? { $_.StartsWith("`t") } | % { $_.SubString(1) } | ? { !$_.StartsWith("deleted:") }
$filesToIgnore | % {
$fileName = [IO.Path]::GetFileName($_)
$directory = [IO.Path]::GetDirectoryName($_)
if($fileName.length -eq 0) {
#The path is actually a directory
$fileName = [IO.Path]::GetFileName($directory) + "/"
$directory = [IO.Path]::GetDirectoryName($directory)