Skip to content

Instantly share code, notes, and snippets.

@DBremen
DBremen / Get-TextWithin.ps1
Created February 8, 2021 17:09
PowerShell function to extract text between delimiters
function Get-TextWithin {
<#
.SYNOPSIS
Get the text between two surrounding characters (e.g. brackets, quotes, or custom characters)
.DESCRIPTION
Use RegEx to retrieve the text within enclosing characters.
.PARAMETER Text
The text to retrieve the matches from.
.PARAMETER WithinChar
Single character, indicating the surrounding characters to retrieve the enclosing text for.
@DBremen
DBremen / extractingTextSurroundingChars.ps1
Created February 8, 2021 16:28
PowerShell example to extract text between delimiters using RegEx
$testText = 'Peter <peter@gmail.com>, Paul <paul@gmail.com>, Zoe <zoe@gmx.com>'
$pattern = '(?<=\<).+?(?=\>)'
[regex]::Matches($testText, $pattern).Value
<#
output
peter@gmail.com
paul@gmail.com
zoe@gmx.com
#>
@DBremen
DBremen / FlaUICalculatorPowershell.ps1
Created February 3, 2021 14:02
FlaUI calculator example
#assuming the script is in the same folder as the FlaUI assemblies
Add-Type -Path "$PSScriptRoot\FlaUI.Core.1.3.0\lib\net45\FlaUI.Core.dll"
Add-Type -Path "$PSScriptRoot\FlaUI.UIA3.1.3.1\lib\net45\FlaUI.UIA3.dll"
$calc = [Diagnostics.Process]::Start('calc')
#wait for the UI to appear
$null = $calc.WaitForInputIdle(5000)
sleep -s 2
$calcWindowId = ((Get-Process).where{ $_.MainWindowTitle -eq 'Calculator' })[0].Id
@DBremen
DBremen / FlaUIInstallation.ps1
Created February 3, 2021 13:58
FlaUI and FlaUInspect package download via PowerShell
$null = mkdir test
cd test
Install-Package -Name FlaUI.Core -ProviderName NuGet -RequiredVersion 1.3.1 -SkipDependencies -Destination $pwd -Source nuget.org
Install-Package -Name FlaUI.CORE -ProviderName NuGet -RequiredVersion 1.3.0 -SkipDependencies -Destination $pwd -Source nuget.org
Find-Package flauinspect | Install-Package
@DBremen
DBremen / UIAutomationCalculator.ps1
Created February 2, 2021 16:36
UIAutomation PowerShell example
Add-Type -AssemblyName UIAutomationClient
Add-Type -AssemblyName UIAutomationTypes
$calc = [Diagnostics.Process]::Start('calc')
#wait for the UI to appear
$null = $calc.WaitForInputIdle(5000)
sleep -s 2
$calcWindowId = ((Get-Process).where{$_.MainWindowTitle -eq 'Calculator'})[0].Id
$root = [Windows.Automation.AutomationElement]::RootElement
$condition = New-Object Windows.Automation.PropertyCondition([Windows.Automation.AutomationElement]::ProcessIdProperty, $calcWindowId)
$calcUI = $root.FindFirst([Windows.Automation.TreeScope]::Children, $condition)
@DBremen
DBremen / ConvertTo-LocalTime.ps1
Created June 29, 2020 21:09
Function to convert a remote time zone into a local time
class DateTransformAttribute : System.Management.Automation.ArgumentTransformationAttribute {
# property to take additional format strings for transformations
[string[]]$AdditionalFormatStrings
# default constructor:
DateTransformAttribute() : base() { }
# 2nd constructor with parameter AdditionalFormatStrings
DateTransformAttribute([string[]]$AdditionalFormatStrings) : base() {
@DBremen
DBremen / customArgumenTransformationAttribute_DateTime_example.ps1
Created June 29, 2020 21:06
PowerShell custom ArgumentTransformation attribute for DateTime example
# define a format that is supported by our attribute
$stringDate = '4/14 4:12PM'
# try it first with the normal datetime type
[datetime]'4/14 4:12PM'
# this threw an error
# define a variable that utilizes the attribute
[datetime][DateTransform()]$dt = $stringDate
# no error
$dt
@DBremen
DBremen / customArgumenTransformationAttribute_DateTime.ps1
Last active June 29, 2020 20:24
PowerShell custom argument transformation attribute for DateTime
class DateTransformAttribute : System.Management.Automation.ArgumentTransformationAttribute {
# property to take additional format strings for transformations
[string[]]$AdditionalFormatStrings
# default constructor:
DateTransformAttribute() : base() { }
# 2nd constructor with parameter AdditionalFormatStrings
DateTransformAttribute([string[]]$AdditionalFormatStrings) : base() {
@DBremen
DBremen / parseExact.ps1
Created June 29, 2020 19:41
DateTime.parseExact example
[datetime]::ParseExact('08|12|2009 4:33 PM','dd|MM|yyyy h:m tt',$Null)
#returns Tuesday, December 8, 2009 4:33:00 PM on an English system
@DBremen
DBremen / test.ps1
Last active June 28, 2020 18:28
Custom ArgumentCompleter attribute
Param(
[Parameter(Mandatory)]
[ArgumentCompleter(
{
param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameter)
$allTimezones = Get-TimeZone -ListAvailable | Group-Object DisplayName -AsHashTable -AsString
$allTimezones.GetEnumerator() | Where-Object { $_.Name -like "*${wordToComplete}*" } |
Sort-Object { $_.Value.Displayname }-Unique | ForEach-Object {
New-Object System.Management.Automation.CompletionResult (
"'$($_.Value.Id)'",