Skip to content

Instantly share code, notes, and snippets.

@drlsdee
drlsdee / Format-Guid.ps1
Created March 17, 2023 13:23
This gist just explains values of IFormatProvider argument of the ToString() method of the .NET type System.Guid. See more: https://learn.microsoft.com/ru-ru/dotnet/api/system.guid.tostring
# This gist just explains values of IFormatProvider argument of the ToString() method of the .NET type System.Guid
# See more: https://learn.microsoft.com/ru-ru/dotnet/api/system.guid.tostring
enum PSGuidFormatProvider {
None = 0 # Only alphanumeric characters: 00000000000000000000000000000000
Braces = 1 # Figure brackets, "{}": {00000000-0000-0000-0000-000000000000}
Dashes = 2 # Hyphens, "-": 00000000-0000-0000-0000-000000000000
Parentheses = 3 # Round brackets, "()": (00000000-0000-0000-0000-000000000000)
Xadecimal = 4 # Groups of heXadecimal values: {0x00000000,0x0000,0x0000,{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}}
}
@drlsdee
drlsdee / Set-UriUserInfo.ps1
Created February 17, 2023 08:25
The function appends the username and password, both URL-encoded, to the specified URI. It can be useful for cases where you need to specify a username and password in an HTTP request, and either the username or the password or both contain special characters that need to be escaped.
#Requires -Version 5.1
#Requires -Assembly "System.UriBuilder, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
#Requires -Assembly "System.Web.HttpUtility, System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
<#
.SYNOPSIS
The function appends the username and password, both URL-encoded, to the specified URI.
.DESCRIPTION
The function appends the username and password, both URL-encoded, to the specified URI.
It can be useful for cases where you need to specify a username and password in an HTTP request, and either the username or the password or both contain special characters that need to be escaped.
@drlsdee
drlsdee / PSComparers.psm1
Created February 17, 2023 08:13
This PowerShell module contains a set of classes for comparing .NET objects that do not implement the IComparable interface, such as [System.Net.IPAddress], [System.Net.NetworkInformation.PhysicalAddress], [System.Security.Cryptography.Oid], [System.Uri].
#Requires -Version 5.1
<#
This PowerShell module contains a set of classes for comparing .NET objects that do not implement the IComparable interface, such as [System.Net.IPAddress], [System.Net.NetworkInformation.PhysicalAddress], [System.Security.Cryptography.Oid], [System.Uri].
#>
enum PhysicalAddressCase {
Default = 0
UpperCase = 1
LowerCase = 2
@drlsdee
drlsdee / Get-EnumValuesTable.ps1
Created February 10, 2023 12:49
A small and simple PowerShell function to convert enum types to a dictionary with enum numeric values as dictionary keys mapped to corresponding enum string names.
<#
.SYNOPSIS
A small and simple PowerShell function to convert enum types to a dictionary with enum numeric values as dictionary keys mapped to corresponding enum string names.
.DESCRIPTION
A small and simple PowerShell function to convert enum types to a dictionary with enum numeric values as dictionary keys mapped to corresponding enum string names.
.PARAMETER Type
Specifies the type as a [System.Type] object, or as a string containing the typename. Note that the function may throw an exception, if it fails to resolve a type by its short type name.
.EXAMPLE
PS C:\> Get-EnumValuesTable -Type "System.Security.AccessControl.AccessControlType"
The function returns a sorted dictionary:
@drlsdee
drlsdee / PSCmdletParameterHelper.psm1
Created February 8, 2023 15:13
A small PowerShell module to get the list of valid parameter names for any specified PS cmdlet, excluding the common and optional parameters, such as "Verbose","WhatIf" etc.
#Requires -Version 5.1
[System.FlagsAttribute()]
enum PSCmdletCommonParameterSet {
Common = 1 # 01
Optional = 2 # 10
}
<#
.SYNOPSIS
@drlsdee
drlsdee / Get-PadString.ps1
Created February 2, 2023 08:39
The function takes a list of strings and pads each string with the Unicode space (0x0020) or any other specified character up to the length of the longest string in the list. The function may be useful for formatting the console output of various objects.
<#
.SYNOPSIS
The function takes a list of strings and pads each string with the Unicode space (0x0020) or any other specified character up to the length of the longest string in the list.
.DESCRIPTION
The function takes a list of strings and pads each string with the Unicode space (0x0020) or any other specified character up to the length of the longest string in the list.
The function may be useful for formatting the console output of various objects.
.PARAMETER InputList
Specifies a list of input strings
.PARAMETER Padding
Specifies a padding direction. The acceptable values are "PadLeft" and "PadRight", which are .NET method names of type [System.String].
@drlsdee
drlsdee / Get-ADObjGroupMembership.ps1
Created January 27, 2023 07:00
The function recursively returns almost all groups for the specified AD object. Default groups such as "Domain Users" are not included in the output.
function Get-ADObjGroupMembership {
[CmdletBinding()]
[OutputType([Microsoft.ActiveDirectory.Management.ADObject])]
param (
# Parameter help description
[Parameter(
Mandatory = $true,
ValueFromPipeline = $true,
Position = 0
)]
@drlsdee
drlsdee / Get-GuidEscapedForLDAP.ps1
Last active January 11, 2024 13:27
The function receives a GUID as an object or byte array and returns an escaped string for use in LDAP queries.
<#
.SYNOPSIS
The function receives a GUID as an object or byte array and returns an escaped string for use in LDAP queries.
.DESCRIPTION
The function receives a GUID as an object or byte array and returns an escaped string for use in LDAP queries. In both cases, the function accepts pipeline input.
.EXAMPLE
PS C:\> Get-GuidEscapedForLDAP -Guid "bf967961-0de6-11d0-a285-00aa003049e2"
The function returns the string: "\61\79\96\bf\e6\d\d0\11\a2\85\0\aa\0\30\49\e2".
.EXAMPLE
PS C:\> Get-GuidEscapedForLDAP -Byte (([guid]'bf967961-0de6-11d0-a285-00aa003049e2').ToByteArray())
@drlsdee
drlsdee / PSQuser.psm1
Last active January 23, 2023 15:34
Just another wrapper for the old good command "quser"
#Requires -Version 5.1
# This enum can be interpreted as [System.Boolean], where "False" means that the current user session is in a disconnected state
enum QuserSessionState {
Idle = 0
Active = 1
}
class QuserSession {
# Property: stores the computer name
@drlsdee
drlsdee / Get-DateTimeInPast.ps1
Created January 12, 2023 07:27
The function returns a date and time value that is older than the current date and time by the specified number of hours, minutes, seconds, years etc. This function may be useful for working with items older than a specified date, e.g., for log rotation.
<#
.SYNOPSIS
The function returns a date and time value that is older than the current date and time by the specified number of hours, minutes, seconds, years etc.
.DESCRIPTION
The function returns a date and time value that is older than the current date and time by the specified number of hours, minutes, seconds, years etc.
This function may be useful for working with items older than a specified date, e.g., for log rotation.
.PARAMETER Years
Specifies max item age in years.
.PARAMETER Months
Specifies max item age in months.