Skip to content

Instantly share code, notes, and snippets.

View figueroadavid's full-sized avatar

David Figueroa figueroadavid

View GitHub Profile
@figueroadavid
figueroadavid / Get-HResultMessage.ps1
Created April 26, 2022 14:15
Get HResult Error Message information
function Get-HResultMessage {
[cmdletbinding()]
param(
[parameter(Mandatory, ValueFromPipelineByPropertyName)]
[int]$HResult
)
$Result = [ComponentModel.Win32Exception]::new($HResult)
if ($PSVersionTable.PSVersion.Major -le 5) {
@{
Data = $Result.Data
@figueroadavid
figueroadavid / Test-Port.ps1
Created December 30, 2021 16:07
Simple TCP port test with a timeout
function Test-Port
{
<#
.SYNOPSIS
Tests TCP ports and returns a True/False result (boolean)
.DESCRIPTION
Uses a TCP net connection client specifying a timeout to allow the user to test specific ports, and
to specify a timeout.
@figueroadavid
figueroadavid / Invoke-WithImpersonation.ps1
Created December 21, 2021 21:53 — forked from jborean93/Invoke-WithImpersonation.ps1
Invoke a scriptblock in powershell with impersonation
# Copyright: (c) 2020, Jordan Borean (@jborean93) <jborean93@gmail.com>
# MIT License (see LICENSE or https://opensource.org/licenses/MIT)
Function Invoke-WithImpersonation {
<#
.SYNOPSIS
Invoke a scriptblock as another user.
.DESCRIPTION
Invoke a scriptblock and run it in the context of another user as supplied by -Credential.
@figueroadavid
figueroadavid / CreateCredential.md
Last active July 7, 2021 21:39
Create Credential without New-Object
@figueroadavid
figueroadavid / Get-QWinstaInfo.ps1
Created February 18, 2021 22:08
Get-QWinstaInfo converts the output into objects
function Get-QwinstaInfo {
<#
.SYNOPSIS
Gets QWinsta information as objects
.DESCRIPTION
Parses qwinsta.exe output for a given computer and outputs it as objects
.EXAMPLE
PS C:\> Get-QwinstaInfo -ComputerName Server1
SessionName : services
@figueroadavid
figueroadavid / Get-QUserInfo.ps1
Last active February 18, 2021 22:05
Converts QUser data into PSCustomObjects
function Get-QuserInfo {
<#
.SYNOPSIS
Gets QUser information as objects
.DESCRIPTION
Parses quser.exe output for a given computer and outputs it as objects
.EXAMPLE
PS C:\> Get-QuserInfo -ComputerName Server1
UserName : user2
function New-ShortCut
{
<#
.DESCRIPTION
The script uses the ComObject WshShell to create a shortcut,
When the shortcut is created, the ComObject is disposed of.
.SYNOPSIS
Creates a new shortcut
@figueroadavid
figueroadavid / New-TempPassword.ps1
Created November 7, 2019 16:40
Generates new temporary passwords, and optionally validates they are AD compliant for complexity
Function New-TempPassword {
param(
[parameter(ValueFromPipelineByPropertyName, ValueFromPipeline)]
[ValidateScript({ $_ -ge 8 })]
[int32]$PasswordLength = 20,
[parameter(ValueFromPipelineByPropertyName)]
[int32]$MinimumNonAlphaNumericCharacters = 1,
[parameter(ValueFromPipelineByPropertyName)]
@figueroadavid
figueroadavid / Update-VMHardware.ps1
Created September 4, 2019 14:42
Script to upgrade VMware hardware
function Update-VMHardware
{
<#
.SYNOPSIS
A script to programmatically upgrade the vm hardware level
.DESCRIPTION
The script uses powercli to programmatically schedule the hardware upgrade to the specified hardware level.
It presumes that the user is already connected to the vSphere server hosting the vm's to upgprade, and it automatically creates a snapshot of the VM before scheduling the upgrade.
.EXAMPLE

Credit: Mark Kraus
Website: https://get-powershellblog.blogspot.com

Collection Type Guidance

When to use what

  • 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.