Skip to content

Instantly share code, notes, and snippets.

@ninmonkey
ninmonkey / Base Version pwsh7 does not break.ps1
Last active October 24, 2024 21:46
WinPS: Calls Add-Content with multiple retries. Before failing
# This is a ps5 + ps7 script
# ps5 writes errors when PBI refreshes, ps7 either never does, or its much, much more rare
$dest = 'C:\Users\cppmo_000\AppData\Local\Temp\cults.csv'
$iter = 0
while($true) {
$iter++
$Last = [datetime]::Now
$content = w32tm /stripchart /computer:time.google.com /samples:1 /dataonly
Add-Content -Path $dest -Value (
@ninmonkey
ninmonkey / Pwsh Line Continuations are Permissive.md
Last active October 17, 2024 17:40
Pwsh Line Continuations are Permissive

Sometimes line continuations are quite permissive

Starting code

[datetime]::ParseExact( 'septiembre', 'MMMM', (get-culture 'es') ).ToString('MMMM', (get-culture 'es'))

[a]

@ninmonkey
ninmonkey / Power BI Parsing Spanish Month Names to to Dates.pq.md
Last active October 17, 2024 16:43
How to Importing names like `SEPTIEMBER` as spanish month names, to dates while using english locales

How can you convert a text column with SEPTIEMBER as a spanish month name to a date or even english name?

  1. specifically choose spanish es as your culture/locale in your "transform column types" step
  2. If that's not automatic, pick the format string for month names

Here's Power Query to prove it

[ 
@ninmonkey
ninmonkey / How to export Unicode CSV files for Excel.md
Last active October 17, 2024 15:10
How to export Unicode CSV files, that Excel will automatically open correctly ( 2024 )

TL; DR;

You need to use utf-8 with the BOM. Then excel opens it automatically. No transforms, no get-data is required.

How to generate unicode test files

Here's a powershell script:

$rows = @( 
@ninmonkey
ninmonkey / Find Commands in a script using AST.ps1
Last active October 3, 2024 16:14
Parsing a powershell script using the AST
using namespace System.Management.Automation
using namespace System.Management.Automation.Language
$Tokens = $null
$AstErrors = $Null
$doc = [Parser]::ParseFile(
<# fileName: #> $file,
<# tokens : #> [ref] $tokens,
<# errors : #> [ref] $AstErrors )
[xml]$doc = gc (Join-Path $PSScriptRoot './sample.xml')
[xml]$Doc = @'
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<setting name = "logLevel" value = "verbose" />
<setting name = "root" value = "c:/foo" desc = "users root dir" />
</configuration>
'@
@ninmonkey
ninmonkey / Simple Format Control char Symbols in PowerShell 7.ps1
Last active September 19, 2024 16:18
Show ansi escapes for runes in Powershell 7
function Format-ControlChar {
<#
.synopsis
converts ANSI control chars into human-visible symbols.
.description
Because they are not ansi escapes ( control chars ), they have no special meaning
so they are save to pipe to your terminal or transport.
This is written in for Pwsh7 because it simplifes code
#>
@ninmonkey
ninmonkey / Power Query Sleep using Function.InvokeAfter.pq
Created September 10, 2024 17:43
Testing if Query Diagnostics include sleeping as the full duration for that step. Yes.
let
/* testing delays in query diagnostics */
seconds = 5,
resp1 = [
Bytes = Web.Contents( "https://httpbin.org", [ RelativePath = "/links/3/1" ] ),
Who = Diagnostics.ActivityId()
],
resp2 =
Function.InvokeAfter(
@ninmonkey
ninmonkey / Collect Favicons using DOM Selector Queries.ps1
Last active September 3, 2024 12:39
Combining a simple `CSS` / `XPath` query with regex works fairly well to find `icon` resources
#Requires -Module 'PSParseHtml'
<#
.Synopsis
Combining a simple `CSS` / `XPath` query with regex works fairly well to find `icon` resources
#>
[Uri] $Url = 'https://na.alienwarearena.com'
$xpath_doc ??= ConvertFrom-Html -Url $Url -Engine AgilityPack
$css_doc ??= ConvertFrom-Html -Url $Url -Engine AngleSharp
@ninmonkey
ninmonkey / Teams Invoke-WebRequest returns bytes vs text depending on Powershell Version Summary.md
Last active September 2, 2024 16:26
Stand-Alone test to reproduce similar errors with summary