Monday, March 12, 2012

PowerShell Snippits

I'm relatively new to PowerShell, the following are PowerShell snippits that I wanted to keep around for my personal reference.  It's short sample code if you will.  I figured I'd post them so that I can always come back here to reference them.  Maybe someone else can find them useful too.  I can elaborate and expand on any of them in another posting if anyone would like.

Hash Tables
$states = @{"Washington" = "Olympia"; "Oregon" = "Salem"; California = "Sacramento"}

$states.Add("Alaska", "Fairbanks")
$states.Remove("Alaska")
$states.Add("Alaska", "Fairbanks")
$states.Set_Item("Alaska", "Juneau")

$states | Sort-Object Name

write-host " "
write-host "The above pipes the object to the sort commandlet, you need to pipe each item in the collect with GetEnumerator()"
write-host " "

$states.GetEnumerator() | Sort-Object Name


Display Hex
"0x{0:x}" -f 500


Format-Table Formating Expression
Get-Process | FT -Property Handles, @{Label="Handles in Hex";Expression={"0x{0:x}" -f $_.Handles}}


DateTime Conversion
[datetime]::FromFileTimeUTC(129760513630045921)



Bitwise AND (-band) with Where-Object Cmdlet
$UserAccountFlags = @{}
$UserAccountFlags.Add(0x00000001, "ADS_UF_SCRIPT")
$UserAccountFlags.Add(0x00000002, "ADS_UF_ACCOUNTDISABLE")
$UserAccountFlags.Add(0x00000008, "ADS_UF_HOMEDIR_REQUIRED")
$UserAccountFlags.Add(0x00000010, "ADS_UF_LOCKOUT")
$UserAccountFlags.Add(0x00000020, "ADS_UF_PASSWD_NOTREQD")
$UserAccountFlags.Add(0x00000040, "ADS_UF_PASSWD_CANT_CHANGE")
$UserAccountFlags.Add(0x00000080, "ADS_UF_ENCRYPTED_TEXT_PASSWORD_ALLOWED")
$UserAccountFlags.Add(0x00000100, "ADS_UF_TEMP_DUPLICATE_ACCOUNT")
$UserAccountFlags.Add(0x00000200, "ADS_UF_NORMAL_ACCOUNT")
$UserAccountFlags.Add(0x00000800, "ADS_UF_INTERDOMAIN_TRUST_ACCOUNT")
$UserAccountFlags.Add(0x00001000, "ADS_UF_WORKSTATION_TRUST_ACCOUNT")
$UserAccountFlags.Add(0x00002000, "ADS_UF_SERVER_TRUST_ACCOUNT")
$UserAccountFlags.Add(0x00004000, "N/A")
$UserAccountFlags.Add(0x00008000, "N/A")
$UserAccountFlags.Add(0x00010000, "ADS_UF_DONT_EXPIRE_PASSWD")
$UserAccountFlags.Add(0x00020000, "ADS_UF_MNS_LOGON_ACCOUNT")
$UserAccountFlags.Add(0x00040000, "ADS_UF_SMARTCARD_REQUIRED")
$UserAccountFlags.Add(0x00080000, "ADS_UF_TRUSTED_FOR_DELEGATION")
$UserAccountFlags.Add(0x00100000, "ADS_UF_NOT_DELEGATED")
$UserAccountFlags.Add(0x00200000, "ADS_UF_USE_DES_KEY_ONLY")
$UserAccountFlags.Add(0x00400000, "ADS_UF_DONT_REQUIRE_PREAUTH")
$UserAccountFlags.Add(0x00800000, "ADS_UF_PASSWORD_EXPIRED")
$UserAccountFlags.Add(0x01000000, "ADS_UF_TRUSTED_TO_AUTHENTICATE_FOR_DELEGATION")

$value = 0x202
$UserAccountFlags.Keys | where { $_ -band $value } | foreach { $UserAccountFlags.Get_Item($_) }

Write-Host ""

$value = 0x10212
$UserAccountFlags.Keys | where { $_ -band $value } | foreach { $UserAccountFlags.Get_Item($_) }

Write-Host ""

$value = 0x200
$UserAccountFlags.Keys | where { $_ -band $value } | foreach { $UserAccountFlags.Get_Item($_) }

NewLine
Environment::NewLine

Operators

  • -lt -- Less than
  • -le -- Less than or equal to
  • -gt -- Greater than
  • -ge -- Greater than or equal to
  • -eq -- Equal to
  • -ne -- Not equal to
  • -like - Like; uses wildcards for pattern matching


I intend to continually update this post with more snippets.

No comments:

Post a Comment