Skip to content

Instantly share code, notes, and snippets.

View LawrenceHwang's full-sized avatar
☁️

Lawrence Hwang LawrenceHwang

☁️
View GitHub Profile
# Fix the locked changed block tracking file for vms in a VMWare environment.
# It it achieved by flipping the changed block tracking settings.
# Snapshot is used as an alternative so the setting can be changed.
# Requires VMWare PowerCLI 5.1 and PowerShell V3 or above.
#Add the required PowerPLI cmdlets
Add-PSSnapin VMware.VimAutomation.Core
#Parameters
$vCenter="vCenter"
# !/bin/bash
# Please manually download the omi-1.0.8.4.
# The wget doesn't work because of the javascript used in the site
# https://collaboration.opengroup.org/omi/documents/34607/omi-1.0.8.4.packages.tar.gz
yum -y install pam-devel
yum -y install openssl-devel
# wget https://collaboration.opengroup.org/omi/documents/34607/omi-1.0.8.4.packages.tar.gz
<#
This is not a complete code and inteneded to be used with other codes.
The goal is to grab a collection of names, vms..etc and then:
- Do some work to the collection in smaller batches
- The batch work should be sent to background for "parallel" execution
- Have a way to check the status of each object in the batch work
- If completed work(s) is found, pull next object(s) from the collection
The status of last batch is not tracked. Improvements can be made in future version.
#>
function Get-PSWindowsUpdateLog
{
<#
.Synopsis
Parsing WindowsUpdate.log to objects for easy reporting. Default will retrieve the most recent 100 entires only.
.DESCRIPTION
This function will retrieve and then parse the c:\windows\system32\WindowsUpdate.log on local or remote computers. Using the -all parameter, the full log will be retrieved but this could be lengthy.
.EXAMPLE
PS E:\_PSTemp> Get-PSWindowsUpdateLog -ComputerName localhost
# Source: https://blogs.msdn.microsoft.com/mssmallbiz/2016/07/10/how-to-download-all-of-the-free-ebooks-and-resources-in-my-free-ebooks-giveaway/
# Source: http://www.mssmallbiz.com/ericligman/Key_Shorts/MSFTFreeEbooks.txt
# Script for convenience. No proper error handling whatsoever. Saves to user's download\FreeEbook folder.
# Parsing the http links
$LinkSource = 'http://www.mssmallbiz.com/ericligman/Key_Shorts/MSFTFreeEbooks.txt'
$link = ((Invoke-WebRequest -Uri $LinkSource).content -split "`n" | where {$_ -like 'http://*'}).trim()
$path = '~\Downloads\FreeEBook'
@LawrenceHwang
LawrenceHwang / get-publicIP.ps1
Created December 30, 2016 00:02
PowerShell one-liner to get machine's public IP
[ipaddress](((invoke-webrequest -Uri 'http://checkip.amazonaws.com/').Content | % {[char]$_}) -join '').trim()
$instances = Get-ec2instance -Filter @(@{name = 'platform'; value = 'windows'})
foreach ($i in $instances){
$prop = @{
Name = $i.Name
password = Get-EC2PasswordData -InstanceId $i.InstanceId -PemFile "C:\path\to\private\keys\$($i.KeyName).pem"
InstanceId = $i.instances.InstanceId
}
$obj = New-Object -TypeName psobject -Property $prop
$obj

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

Collection Type Guidence

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.
@LawrenceHwang
LawrenceHwang / FibI.ps1
Created May 30, 2018 04:15
[PowerShell] Fibonacci number with iteration
# Fibonacci - Interation
Function FibI {
[CmdletBinding()]
[Parameter(Position = 0)]
param ([int]$index)
Set-StrictMode -Version Latest
if ($index -eq 0) {
@LawrenceHwang
LawrenceHwang / CompareResult.PS1
Created May 30, 2018 04:47
[PowerShell] Recursion vs Iteration
# This is iteration
9:42 PM> Measure-Command -Expression {FibI 20}
...
TotalSeconds : 0.0093811
TotalMilliseconds : 9.3811
# This is recursion
9:43 PM> Measure-Command -Expression {FibR 20}
...
TotalSeconds : 22.7287975