[cmdletbinding()] | |
param ( | |
[parameter(parametersetname='test1')] | |
[switch]$p1, | |
[parameter(parametersetname='test2')] | |
[switch]$p2 | |
) | |
DynamicParam |
#requires -version 7.0 | |
$am = @" | |
_____ __ __ ___ _ __ ______ | |
/ ___/__ ___ ___/ / / |/ /__ _______ (_)__ ___ _ __ / /__ / _/ _/ | |
/ (_ / _ \ _ \ _ / / /|_/ / _ \/ __/ _ \/ / _ \ _ `/ / // / -_) _/ _/ | |
\___/\___\___\_,_/ /_/ /_/\___/_/ /_//_/_/_//_\_, ( ) \___/\__/_//_/ | |
/___/|/ | |
"@ |
# Add a .ToDictionary(KeyType,ValueType) for all hashtables | |
Update-TypeData -TypeName Hashtable -MemberType ScriptMethod -MemberName ToDictionary -Value { | |
param([Type]$KeyType,[Type]$ValueType) | |
[Scriptblock]::Create( | |
"[Enumerable]::ToDictionary( | |
[DictionaryEntry[]]@(`$this.GetEnumerator()), | |
[Func[DictionaryEntry,$($KeyType.FullName)]]{ `$args.Key }, | |
[Func[DictionaryEntry,$($ValueType.FullName)]]{ `$args.Value })" | |
).Invoke() | |
} |
$crypto = @" | |
P k T r 2 s z 2 * c F - | |
r a z 7 G u D 4 w 6 U # | |
g c t K 3 E @ B t 1 a Y | |
Q P i c % 7 0 5 Z v A e | |
W 6 j e P R f p m I ) H | |
y ^ L o o w C n b J d O | |
S i 9 M b e r # ) i e U | |
* f 2 Z 6 M S h 7 V u D | |
5 a ( h s v 8 e l 1 o W |
# mute here: https://bsky.app/moderation | |
election | |
president-elect | |
hurricane | |
storm surge | |
climate crisis | |
Iran | |
Israel | |
ballistic missiles | |
North Korea |
<# | |
Prerequisites: PowerShell v5.1 and above (verified; may also work in earlier versions) | |
License: MIT | |
Author: Michael Klement <mklement0@gmail.com> | |
DOWNLOAD and DEFINITION OF THE FUNCTION: | |
irm https://gist.github.com/mklement0/9e1f13978620b09ab2d15da5535d1b27/raw/Time-Command.ps1 | iex |
Credit: Mark Kraus
Website: https://get-powershellblog.blogspot.com
- Use Arrays if you know the element types and have a fixed length and/or known-up-front collection size that will not change.
- Use ArrayList if you have an unkown collection size with either unknown or mixed type elements.
- Use a Generic List when know the type of the elements but not the size of the collection.
- Use a HashTable if you are going to do key based lookups on a collection and don't know the object type of the elements.
- Use a Dictionary<TKey, TValue> you are going to do key based lookups on a collection and you know the type of the elements.
- Use a HashSet when you know the type of elements and just want unique values and quick lookups and assignmnets.
The simplest way to enforce rules on PowerShell class properties is to set the Type of the property, of course. But sometimes you want something a little bit more clever than that. One solution is to use a Validate*
attribute, like ValidateRange
or ValidateLength
or ValidateSet
attribute...
However, you can write your own, by just deriving from ValidateArguments() or something that derives from that, like the ValidateEnumeratedArguments class allows you to validate a whole array of items.
For instance, a simple validator would be one that validates uniqueness, based on a specific property. Our validator will be created fresh each time it's used, and then each item in the array will be passed through ValidateElement
, so this works:
using namespace System.Collections.Generic
usin
#requires -version 4.0 | |
Function New-GitHubGist { | |
[cmdletbinding(SupportsShouldProcess,DefaultParameterSetName = "Content")] | |
Param( | |
[Parameter(Position = 0, Mandatory, HelpMessage = "What is the name for your gist?",ValueFromPipelineByPropertyName)] | |
[ValidateNotNullorEmpty()] | |
[string]$Name, |