Skip to content

Instantly share code, notes, and snippets.

View PanosGreg's full-sized avatar

Panos Grigoriadis PanosGreg

  • Dublin, Ireland
View GitHub Profile
@PanosGreg
PanosGreg / ParamSet.ps1
Created June 24, 2024 08:41
PowerShell Parameter Sets
## Parameter Sets Examples
# Example #1
# Choose 1 or 2 out of 2
# order is not important
<# Combinations: 3 in total
@PanosGreg
PanosGreg / Get-WindowsVersion.ps1
Last active April 15, 2024 10:18
It provides the equivalent information to winver.exe
function Get-WindowsVersion {
<#
.SYNOPSIS
It provides the equivalent information to winver.exe
.EXAMPLE
Get-WindowsVersion
#>
[cmdletbinding()]
param ()
@PanosGreg
PanosGreg / Get-WinUpdate.ps1
Last active March 26, 2024 14:46
Get a list of the windows updates with a KB ID using the COM Microsoft.Update.Searcher
function Get-WinUpdate {
<#
.SYNOPSIS
Get a list of all the windows updates on the system
Note: This function will only show the updates that have a KB ID
Any hotfixes without an update ID won't be included in the results
.EXAMPLE
Get-WinUpdate | where Title -like '*cumulative update for windows*'
.EXAMPLE
@PanosGreg
PanosGreg / Get-RuntimeDiagnostics.ps1
Last active February 22, 2024 21:50
Collect the count and memory size of the .NET objects used by a process
function Get-RuntimeDiagnostics {
<#
.SYNOPSIS
Collect the count and memory size of the .net objects used by a process
.DESCRIPTION
Collect the count and memory size of the .net objects used by a process
This function is using the Microsoft.Diagnostics.Runtime .NET library from nuget:
https://www.nuget.org/packages/Microsoft.Diagnostics.Runtime
@PanosGreg
PanosGreg / Get-ADPrincipal.ps1
Last active April 24, 2024 08:37
Function that finds AD Principals, namely Users, Computers or Groups
function Get-ADPrincipal {
<#
.SYNOPSIS
Get users,computers or groups from Active Directory, without the need of the ActiveDirectory module.
The examples show the use of the .Members property, the .GetGroups() method and also
the .Add() and .Remove() methods from the group members list along with the .Save() method.
.EXAMPLE
Get-ADPrincipal *test*
@PanosGreg
PanosGreg / Get-AwsTempCredential.ps1
Last active April 24, 2024 08:46
Get AWS Credentials (access,secret,token) by assuming the IAM Role attached to the instance
function Get-AwsTempCredential {
<#
.SYNOPSIS
This function gets the AWS Access,Secret and Token by assuming the IAM Role that is attached to the current EC2 instance.
This should be used on EC2 instances only, not on any other VMs. (like Azure,GCP,Hyper-V or VMWare)
There should be an IAM Role attached to the instance, otherwise it won't work.
Once you get the temporary credentials, you can then login to AWS with "Set-AWSCredential"
or alternatively you can use the .LoginAWS() method from this object.
@PanosGreg
PanosGreg / Microsoft.Extensions.Configuration.ps1
Created December 2, 2023 14:14
Microsoft.Extensions.Configuration in PowerShell
## Drilling down into the Microsoft.Extensions.Configuration namespace from a PowerShell point-of-view
## Note: the inspiration for this came up by watching a .net video about configuration from Chris Ayers
# [.NET Configuration In Depth | .NET Conf 2023](https://www.youtube.com/watch?v=aOXaBZFB0-0)
## How to load a .dll which has some dependencies
## you just have to get the package from nuget which will also retrieve all of the dependencies
@PanosGreg
PanosGreg / Uniques.ps1
Created November 16, 2023 16:50
From a string array get the uniques and retain the order
# from a string array get the uniques and retain the order
# get the unique items from a string array with case-insensitive and also retain the current order
# this approach is to work-around the gap from Select-Object -Unique, which compares the object and not the property
# in the case of string arrays.
# and also to retain the order, whereas the Sort-Object -Unique would change the order
# source string array
@PanosGreg
PanosGreg / ConvertTo-ClassDefinition.ps1
Created November 7, 2023 14:38
Dynamically generate the PowerShell class code from a pscustomobject or hashtable
function ConvertTo-ClassDefinition {
<#
.EXAMPLE
$hash = @{name='aa';size=10}
$code = $hash | ConvertTo-ClassDefinition
Invoke-Expression $code
$h = [My_Hashtable]::new()
# create and load a class from a hashtable
.EXAMPLE
$obj = [pscustomobject]@{name='aa';size=10}
@PanosGreg
PanosGreg / Invoke-ForEachParallel.ps1
Last active May 21, 2024 12:53
ForEach Parallel wrapper with multi-threaded Progress Bars
function Invoke-ForEachParallel {
<#
.SYNOPSIS
This is a wrapper around foreach-parallel from PS v7, but with progress bars.
.DESCRIPTION
This is a wrapper around foreach-parallel from PS v7, but with progress bars.
You can run a scriptblock against an array of objects, just like foreach,
but it also shows multi-threaded progress bars, one for every thread.