Tag Archives: FridayFun

Friday Fun – Get Happy Holiday

Today’s Friday Fun is my version of a script originally posted a few years ago by The PowerShell Guy. I’ll list the code but you really need to run it to enjoy it.


#requires -version 2.0

#This must be run from the PowerShell console. NOT in the ISE.

#this is based on a script originally posted by The PowerShell Guy
#http://thepowershellguy.com/blogs/posh/default.aspx

$Greeting = "$([char]14) **Happy Holidays** $([char]14)"

Clear-Host
Write-host "`n"
$Peek = " ^ "
$tree = "/|\"
$i = 20
$pos = $host.ui.rawui.CursorPosition
#adjust to center the display
$offset = ($host.ui.rawui.WindowSize.Width - 72)/2

write-host -fore 'red' ($peek.PadLeft($i-1).PadRight(36) * 2)
write-host -fore 'green' ($tree.PadLeft($i-1).PadRight(36) * 2)

1..16 | Foreach {
#build out the tree
$tree = $tree -replace "/(.*)\\",'//$1\\'
write-host -fore 'green' ($tree.PadLeft($i).PadRight(36) * 2)
$i++
}

write-host -fore 'green' ("|||".PadLeft(19).PadRight(36) *2 )
write-host -fore 'green' ("|||".PadLeft(19).PadRight(36) *2)

$rect = New-Object System.Management.Automation.Host.Rectangle
$rect.top = $pos.y
$rect.Right = 70
$rect.Bottom = $pos.y + 19
$buffer = $host.ui.rawui.getbuffercontents($rect)
#random number object
$R = new-object system.random
$ball = new-object System.Management.Automation.Host.BufferCell
$ball.backgroundColor = $host.ui.rawui.BackgroundColor

1..150 | ForEach {
sleep -Milliseconds 100
#get a random position
$rx = $r.Next(19)
$ry = $r.Next(70)

#define a collection of figures to be used as ornaments
$ornaments = '@','*','#',":"
#get a random ornament
$ball.Character = Get-Random $ornaments
$ball.ForegroundColor = $r.next(16)

if ($buffer[$rx,$ry].Character -eq '/') {$buffer[$rx,$ry] = $ball}
if ($buffer[$rx,$ry].Character -eq '\') {$buffer[$rx,$ry] = $ball}
$host.ui.rawui.SetBufferContents($pos,$buffer)
}

#write the greeting centered
$pos.y = $pos.y + 22
$pos.x = 36 - (($Greeting.Length)/2)
$host.ui.rawui.CursorPosition=$pos

Write-Host $Greeting -Fore 'red'

This is probably the epitome of PowerShell fun: not practical in any way but still done with PowerShell. Enjoy.

Download Get-HappyHoliday

Friday Fun: 13 More Scriptblocks

In celebration of Friday the 13th I thought I would offer up a menu of 13 more script blocks. If you missed the first course, you can find the original 13 scrptblocks here. I’m not going to spend a lot of time going over these. Many of them are simple one liners. Some of them take parameters just like functions and scripts. The easiest way to execute any of the scriptblocks is to use the & operator. But these might also come in handy with any cmdlet that takes a scriptblock as a parameter value such as Invoke-Command.

I think of scriptblocks as “quick and dirty” blocks of re-usable code. If you find something very useful, you might expand it into a full-blown function complete with error handling and verbose output. Or you might find a handy technique in one of these examples.


#1 Get top problem source from the last 500 event log entries
$topprob={Param($log="System") Get-EventLog -LogName $log -newest 500 -entrytype Error |
Group Source -NoElement | Sort Count | Select -last 1}
#&$topprob

#2 Get folder usage by owner in MB
$usage={Param($path=".") dir $path -recurse | Where {-Not $_.PSIsContainer} |
Select Fullname,Length,@{N='Owner';E={($_ | Get-ACL).Owner}} |
Group Owner | Sort Count -descending|
Select Count,Name,@{N='SizeMB';E={(($_.Group | Measure length -sum).sum)/1MB}}
}
#&$usage

#3 get empty event logs
$emptylog={get-eventlog -list | where {$_.entries.count -eq 0}}
#&$emptylog

#4 Get OS Install date
$install={Param($Computername=$env:computername) Get-WmiObject win32_operatingsystem -comp $computername |
select CSName,Caption, @{N="Install";E={$_.ConvertToDateTime($_.InstallDate)}}}
#&$install

#5 Test if running Windows 8
$test8={Param($Computername=$env:computername)
(Get-WmiObject win32_operatingsystem -comp $computername).Caption -match "Windows 8"
}
#&$test8 MyWin8

#6 Test if running PowerShell v3
$testPS3={Param($Computername=$env:computername)
(test-wsman -ComputerName $computername).Productversion -match "Stack: 3.0"}
#&$testPS3 MyWin8

#7 Get code snippets from help examples
$excode={Param($command="get-service") (get-help $command).examples.example | select code}
#&$excode get-process

#8 Count by 13
$countup={Param($count=5) $x=0; For ($i=0;$i -lt $Count; $i++) {$x+=13;$x}}
#&$countup 13

#9 get a 13 character random password
$randpass={ Param($length=13) $chars=[char[]](33..126) ; -join ($chars | get-random -count $length)}
#&$randpass

#10 Test if profile scripts exist
$profileCheck={
$profile | Select @{N="Type";E={"AllUsersAllHosts"}},@{N="Path";E={$_.AllUsersAllHosts}},@{N="Exists";E={Test-Path $_.AllUsersAllHosts}}
$profile | Select @{N="Type";E={"AllUsersCurrentHost"}},@{N="Path";E={$_.AllUsersCurrentHost}},@{N="Exists";E={Test-Path $_.AllUsersCurrentHost}}
$profile | Select @{N="Type";E={"CurrentUsersAllHosts"}},@{N="Path";E={$_.CurrentUserAllHosts}},@{N="Exists";E={Test-Path $_.CurrentUserAllHosts}}
$profile | Select @{N="Type";E={"CurrentUserCurrentHost"}},@{N="Path";E={$_.CurrentUserCurrentHost}},@{N="Exists";E={Test-Path $_.CurrentUserCurrentHost}}
}
#&$profileCheck | format-list

#11 get default printer
$defaultPrint={get-wmiobject win32_printer -filter "Default='True'"}
#&$defaultPrint

#12 get timezone
$tz={Param($computername=$env:computername) Get-WmiObject win32_timezone -computername $computername |
Select @{N="Computername";E={$_.__SERVER}},Description}
#&$tz

#13 find expired certificates
$expired={dir cert:\ -recurse | where {$_.NotAfter -AND $_.NotAfter -lt (Get-date)}}
#&$expired
#Invoke-command $expired -comp server01

Download the script file and watch out for black cats under ladders!

Friday Fun: Get Next Available Drive Letter

A few days ago I saw on question, I think on Facebook, about using PowerShell to find the next available drive letter that could be used for mapping a network drive. Before I show you my approach, let me state that if you need to map a drive in PowerShell for use only within your PowerShell solution, you don’t need a drive letter. You can name the drive anything you want when using New-PSDrive.


new-psdrive Backup -PSProvider Filesystem -Root "\\NAS01\backup"

But of course that won’t work if you plan on using the NET USE command. So let’s figure out how to determine the next available drive letter. First, let’s build an array of possible drive letters. I’m too lazy to type the letters C-Z (skipping A and B for the sake of nostalgia). So I’ll “create” them like this:


$letters=[char[]](67..90)

This creates an array of letters C-Z, albeit technically [CHAR] objects, but we’re good. Next we need a list of currently used drive letters which we can retrieve from WMI.


$devices=get-wmiobject win32_logicaldisk | select -expand DeviceID

I’m expanding the DeviceID property so $devices is a simple array and not a collection of objects with a DeviceID property. I did that so that I could use the -Contains operator.


$letters | where {$devices -notcontains "$($_):"} | Select -first 1

With this simple command I’m piping the collection of characters to Where-Object, testing if each letter (with the appended :) is NOT found in the array $devices. The letters are already sorted so all I need to do is select the first 1 and that will be the next available drive letter in my current PowerShell session.

Before I let you go today, let me point out one thing: I could combine these two lines into a single PowerShell one-liner.


[char[]](67..90) | Where {(get-wmiobject win32_logicaldisk | select -expand DeviceID) -notcontains "$($_):"} | Select -first 1

I’ll get the same result…BUT…just because you can do something in PowerShell doesn’t mean you should. This one liner takes almost 2400MS to execute. But when I break it into two lines, as I showed you, I get the end result in 110MS which is a dramatic and noticeable difference. You can test for yourself using Measure-Command. The take-away is to not be afraid to use multiple commands, especially in a script. It may be faster and it will certainly be easier to understand.