Skip to content

Instantly share code, notes, and snippets.

@peaeater
peaeater / csv-split.ps1
Created March 12, 2024 00:27
Split csv files with or without header rows, multiline columns with newlines ok.
<#
Split csv files with or without header rows, multiline columns with newlines ok.
#>
param (
[string]$in,
[string]$outdir = [System.IO.Path]::GetDirectoryName($in),
[int]$count = 1000,
[string]$delimiter = ',',
[string[]]$header = @()
@peaeater
peaeater / ftp-dir-to-remote.ps1
Created December 12, 2022 18:03
Powershell using WinSCP to sync directories over FTP with file mask.
<#
Sync a directory to remote server in FTP mode
Peter Tyrrell
#>
param(
[Parameter(Mandatory = $false, Position = 0)]
[string]$logsrc = "",
@peaeater
peaeater / tif2dzi.ps1
Created June 22, 2022 18:44
DZI Deep Zoom Images from TIFs, requires libvips (https://libvips.github.io/libvips/)
<#
1. Leaf
Given a text file of TIF/TIFF filenames, create DZI (Deep Zoom Image) tiles from TIFs
and create a mirror directory structure for DZI file outputs.
* Handles filenames with entry separators
* Ignores TIF older than its DZI mirror unless -force param is used
* Requires libvips (https://libvips.github.io/libvips/)
.\tif2dzi.ps1 -in c:\dev\abc\extract\extracted\tifs\abc-tifs-1.txt `
<#
Peter Tyrrell, Andornot
Replaces old sql server name with new in textbase binary *.cbs file.
Note: Uses PowerShell Core syntax. See comments for Windows PowerShell.
Note: Server names of different lengths corrupt the file.
#>
param (
@peaeater
peaeater / force-latest-tls.ps1
Created December 1, 2021 22:46
Tells .net to use strongest available TLS. Global setting - modifies registry keys.
<#
Modifies regedit to tell all versions of .NET to use strongest available TLS. Global setting.
Requires admin privileges.
Peter Tyrrell
#>
# check for admin
if (-NOT ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")) {
Write-Warning ("You do not have admin privileges. Re-run this script as an administrator.")
@peaeater
peaeater / sftp-dir-to-remote.ps1
Created October 19, 2020 23:52
Powershell using WinSCP to sync directories over SFTP with file mask.
<#
Sync a local to remote directory in SFTP mode
#>
param(
[Parameter(Mandatory = $false, Position = 0)]
[string]$logsrc = "",
[Parameter(Mandatory = $true, Position = 1)]
[string]$hostname,
[Parameter(Mandatory = $true, Position = 2)]
@peaeater
peaeater / Global.asax.cs
Created September 28, 2020 23:20
Force ImageResizer to authorize images larger than thumbnail based on user role in ASP.NET MVC
protected void Application_Start()
{
// various setup bits here...
Config.Current.Pipeline.AuthorizeImage +=
delegate (IHttpModule sender, HttpContext context, IUrlAuthorizationEventArgs e)
{
// only restrict ~/media
if (!e.VirtualPath.StartsWith(VirtualPathUtility.ToAbsolute("~/media")))
{
@peaeater
peaeater / Global.asax.cs
Created May 8, 2020 22:05
Castle Windsor and Web API 2 with ASP.NET MVC
using System.Web.Http;
using System.Web.Mvc;
using System.Web.Routing;
using Andi.MVC.Core;
using Andi.MVC.Core.Infrastructure.Globalization;
namespace Andi.MVC.Web
{
public class MvcApplication : System.Web.HttpApplication
{
@peaeater
peaeater / MediaApiController.cs
Created May 8, 2020 19:09
Web API 2 media controller that handles partial content requests
public class MediaApiController : ApiController
{
public HttpResponseMessage Get(string filepath)
{
var path = HostingEnvironment.MapPath($"~/media/{filepath}") ?? string.Empty;
var mediaType = MediaTypeHeaderValue.Parse(MimeMapping.GetMimeMapping(filepath));
FileStream stream;
try
@peaeater
peaeater / helper.logging.ps1
Last active April 24, 2020 18:07
Log Helper writes to Event Log, console host or file depending on log source.
<#
Logging functions.
#>
function logError([string]$logsrc, [string]$msg) {
if ([String]::IsNullOrWhiteSpace($logsrc)) {
write-log -msg $msg -level "ERROR"
}
elseif (@(".txt", ".log").Contains([System.IO.Path]::GetExtension($logsrc)) -eq $true) {
write-log -msg $msg -level "ERROR" -file $logsrc