PowerShell for Windows Admins


September 11, 2013  2:43 PM

Windows 2012 R2–ouch

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

The other day I downloaded Windows Server 2012 R2. Goodie I thought I’ll download that. Download worked. Installed 2012 R2 in to a VM and started to configure it tonight.

Went to activate Windows and it asked for a product key. No biggie as I grabbed that during the download.

Entered the key and it was rejected. Huh. Assumed a mistype and tried again. Still rejecting. Went back to MSDN and got a second key. Rejected again.

Next steps – trash the VM & re-download & rebuild.

I do love doing things a second time for no apparent reason.

September 10, 2013  2:43 PM

Windows 2012 R2 PowerShell Execution policy

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

In Windows 2012 R2 RTM – just installed first VM this evening – the PowerShell execution policy is set to RemoteSigned. One less thing to change when I create new VMs.


September 10, 2013  1:20 PM

Loading the SQLPS module

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

Loading the SQLPS module gives you quick access to the sql cmdlets, sql provider and the SMO assemblies. It also, infuriatingly, moves your location into the sql provider.

I (really) * (lots ) hate things that assume what I want to do.

You can use PowerShell to reverse this annoying, and arrogant action:

Push-Location

Import-Module SQLPS

Pop-Location

Will get you back where you were.

SQLPS is slowly becoming a civilised member of the PowerShell eco-system especially when compared to its first incarnation. A few more versions and it might behave properly.


September 10, 2013  3:27 AM

PowerShell one-liner for virtual disk analysis

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

I needed to look at my virtual machines & their disk sizes – with Windows 2012 R2 upgrade in the works I need to do a bit more tidy up

I found two cmdlets in the Hyper-V module:

get-vmharddiskdrive – can be related to the virtual machine but doesn’t give a size

get-vhd – expects a path to the VHD file

Luckily get-vmharddiskdrive outputs the path. This gives me a nice pipeline:

Get-VM |
Get-VMHardDiskDrive |
Get-VHD |
select Path, @{N=’Size’; E={[math]::Round(($_.FileSize / 1gb), 2) }} |
sort -Descending

Get the VMs pipe through get-vmharddiskdrive and get-vhd then select and sort and you’re done.

I always break my pipelines at a pipe symbol – it acts as a line continuation in the console and ISE so anything thing else is just extra unnecessary work


September 10, 2013  1:29 AM

Windows 2012 R2 on MSDN

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

In case you didn’t see yesterday’s announcement – the RTM versions of Windows 2012 R2 and Windows 8.1 are available for download. From what has been announced so far by Microsoft expect some updates before, or at, General Availability next month


September 9, 2013  12:54 PM

SMO with SQL Server 2012

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

I’ve been working quite a bit with PowerShell and SQL Server 2012 just recently. In earlier versions of SQL Server you had to load the SMO assemblies to get access to the admin functionality.

In SQL Server 2012 –

import-module sqlps

gives you the SQL Server provider, the cmdlets and loads the SMO assemblies for you.

Nice and easy so I don’t have to do anything extra. That’s a good module


September 9, 2013  12:11 PM

Solving partial displays of AD properties

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

Had an interesting question today. The question concerned displaying the StreetAddress property when its multiple lines like this:

PS> Get-ADUser -Identity dgreen -Properties StreetAddress | fl Name, StreetAddress

Name : GREEN Dave
StreetAddress : Floor 5
Buidling 3
Newboro Square

If you try this with format-table instead

PS> Get-ADUser -Identity dgreen -Properties StreetAddress | ft Name, StreetAddress -a

Name StreetAddress
—- ————-
GREEN Dave Floor 5…

The address is truncated

The answer is to use the –Wrap parameter

PS> Get-ADUser -Identity dgreen -Properties StreetAddress | ft Name, StreetAddress -a -Wrap

Name StreetAddress
—- ————-
GREEN Dave Floor 5
Buidling 3
Newboro Square

And all is good


September 9, 2013  11:23 AM

Exercise in frustration

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

If you would like an exercise in frustration – try installing the RSAT tools for Windows 2012 onto Windows 8. If your PS’s language is set to en-US it should work. If it is anything else give up now and go watch the grass grow – it will be infinitely more rewarding.

Many people outside of the US want to administer Windows Servers from their PC – why is it so hard? Especially on an English system!!!

You have to go through so many hoops to try and get the language pack installed – its just not worth the effort. I can spin up a Windows 2012 VM to do my admin quicker!


September 9, 2013  2:04 AM

Getting WMI data from remote machines

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

WMI is great for pulling back data from remote machines. This type of activity is quite common:

$computer = $env:COMPUTERNAME

$comp = Get-WmiObject -Class Win32_ComputerSystem -ComputerName $computer
$os = Get-WmiObject -Class Win32_OperatingSystem -ComputerName $computer
$disk = Get-WmiObject -Class Win32_LogicalDisk -ComputerName $computer

New-Object -TypeName PSObject -Property @{
Name = $computer
OS = $os.Caption
LastReBoot = $os.LastBootUpTime
Type = “$($comp.Manufacturer) : $($comp.Model)”
AvailableRAM = $comp.TotalPhysicalMemory
Disks = ($disk | Measure).Count
}

Define the computer. Run some WMI queries & pull the data together into a single object for output.

Each time you call Get-WmiObject you have to rebuild the connection to the remote machine. There appears to be a little bit of caching of information so its not a complete rebuild of the connection but the system has to do some work.

For a few calls to WMI its not a big deal but what you need to make more calls – say 8 or 10.

The CIM cmdlets introduced the concept of CIM sessions for this scenario. Bu the CIM cmdlets work over WSMAN by default & if you have machines running legacy versions of PowerShell or even no PowerShell you can’t use CIM sessions.

Oh yes you can! (sorry but pantomime season is rapidly approaching)

You just configure you CIM session to work over DCOM – just like WMI cmdlets.

$computer = $env:COMPUTERNAME
$copt = New-CimSessionOption -Protocol Dcom
$csess = New-CimSession -SessionOption $copt -ComputerName $computer

$comp = Get-CIMInstance -ClassName Win32_ComputerSystem -CimSession $csess
$os = Get-CIMInstance -ClassName Win32_OperatingSystem -CimSession $csess
$disk = Get-CIMInstance -ClassName Win32_LogicalDisk -CimSession $csess

$csess | Remove-CimSession

New-Object -TypeName PSObject -Property @{
Name = $computer
OS = $os.Caption
LastReBoot = $os.LastBootUpTime
Type = “$($comp.Manufacturer) : $($comp.Model)”
AvailableRAM = $comp.TotalPhysicalMemory
Disks = ($disk | Measure).Count
}

The session option is defined with the DCOM protocol

The Get-CimInstance calls use –CimSession instead of –ComputerName

Best of all the LastBootUpTime is converted to a proper date

If you’re thinking of using multiple WMI calls to a remote machine – think about a CIM session


September 8, 2013  7:28 AM

How much RAM does my machine have?

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

If you need to determine the RAM in a system you can use the Win32_ComputerSystem class

Get-CimInstance -ClassName Win32_ComputerSystem |
select Name, TotalPhysicalMemory

The answer is in bytes BUT it only shows the memory available to the OS. Many systems steal RAM for graphics cards or other devices.

The true amount of physical memory is obtained from the Win32_PhysicalMemory class which gets data in each memory bank in your system. PowerShell will even add them up for you if you ask nicely.

Get-WmiObject Win32_PhysicalMemory |
Measure-Object -Property Capacity –Sum

If you just need the value:

(Get-WmiObject Win32_PhysicalMemory |
Measure-Object -Property Capacity -Sum).Sum

or

Get-WmiObject Win32_PhysicalMemory |
Measure-Object -Property Capacity -Sum |
select -ExpandProperty Sum


Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to: