Thursday, August 6, 2015

Parameter Validation

To summarize Glenn Sizemore from the Scripting Guys blog, here is how you can validate your PowerShell parameters when you write your own script (requires V2):
Param(
[ValidateSet("Department", "AppData")][string]$type,
[ValidateRange(10, 25)][int]$size,
[ValidateScript({Test-connection $_ -count 2 -quiet})][string]$computername
)

And since version 3, the PowerShell ISE is smart enough (via Intellisense) to show you your choices in a drop-down for a ValidateSet parameter. 

Thursday, July 9, 2015

Rounding Numbers

If you want to round numbers in PowerShell, then you have two choices (at least) with slightly different results.

Let's set the stage:
PS > $a = 8
PS > $b = 6
PS > $a / $b
1.3333333333333

One way is to use the format string approach.
 
PS > "{0:n2}" -f ($a / $b)
1.33

So "{0:n2}" denotes a formatted string, where the 2 is how many decimal places to show.  After the -f, you put your expression to format.  There are other choices for this formatted string, which I will not detail here.  The point is that your output is a string.

Another way is to use the Math .NET class to Round.
PS > [Math]::Round(($a / $b), 2)
1.33

This time, however, you get a Double instead of a string. 

It looks like PowerShell does a great job at sorting number strings as numbers.  So far, it doesn't matter which way you use above; Sort-Object puts the numbers in the right order.  PowerShell even lets you do Math operations on numbers stored as strings!  Isn't it great?

Wednesday, June 3, 2015

New Hyper-V Virtual Machine with PowerShell

Here is the PowerShell for a new VM like the wizard does:
$vmName = "New Virtual Machine"
$vmPath = "C:\ProgramData\Microsoft\Windows\Hyper-V\"
New-VM -Name $vmName -Path "$vmPath\$vmName" -MemoryStartupBytes 512MB -NewVHDPath ` "$vmPath\$vmName\$vmName.vhdx" -NewVHDSizeBytes 127gb -Generation 1 -BootDevice CD
#new Gen 1 VM with files stored in $vmPath, with 512MB static RAM, with 1 CPU, with 127GB dynamic VHDX, #disconnected from network, with empty CD drive

Here is how to make other common configurations (one line at a time):
Set-VMDvdDrive -VMName $vmName -Path "k:\ISO\ISO.iso"; #add ISO file to existing drive
Set-VM -NewVMName $vmName -ProcessorCount 2 -DynamicMemory -MemoryMaximumBytes 4096MB #set dynamic memory and CPU
Set-VMNetworkAdapter -VMName $vmName -Name "SecretNet" #connect to network
Set-VM -NewVMName $vmName -AutomaticStartAction StartIfRunning -AutomaticStopAction ShutDown #change auto actions
Checkpoint-VM -Name $vmName #make a new snapshot/checkpoint

Friday, May 22, 2015

PowerShell Random Sort a List

Maybe you have a list that you want to put in a random order.  I had a list of words that I wanted shuffled without repeating.  At first, I was thinking that I'd have to devise some way of choosing at random without replacement, so that Get-Random wouldn't choose the same word twice.

Then I found that Get-Random has a -Count parameter, so I thought: could I make the count the same as the number of items on my list?

Of course!  Why did I even question?
"Mercury" , "Venus" , "Earth" , "Mars" , "Jupiter" , "Saturn" , "Uranus" , "Neptune" , "Pluto" | Get-Random  -Count  9

However, I did learn that the count cannot exceed the length of the list.  If it does, PowerShell won't complain; it will just stop when it has listed every item once.