PowerShell for Windows Admins


February 27, 2014  2:21 PM

Learn AD Management in a Month of Lunches–ebook available

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

The ebook – PDF format – for Learn AD Management in a Month of Lunches has been published – http://www.manning.com/siddaway3/

If you bought the ebook as part of your MEAP you should be able to down load it – you’ll get or have got an email with the link. The printed version is at the printers and will be available on 12 March.

If you want the ebook in Kindle or epub versions they will be available 20 March.

Enjoy.

February 27, 2014  12:30 PM

PowerShell Summit NA 2014

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

The PowerShell Summit is happening in Bellevue (Seattle) – April 28 – 30th.  You will be able to hear, meet and talk to some of the biggest names in PowerShell:

- Jeffrey Snover – the inventor of PowerShell

- PowerShell Team members

- Don Jones

- Jason Helmick

- Jeff Hicks

- Ed Wilson (The Scripting Guy)

- Steven Murawski

- Tome Tanasovski

- James O’Neill

I’ll be there delivering 3 sessions (WSMan cmdlets, cmdletizing the registry and using the network related cmdlets) – its the only chance this year of getting all three authors of PowerShell in Depth in the same place at the same time.

If you haven’t booked a place yet – you can register and view the rest of the sessions at http://powershell.org


February 26, 2014  4:47 PM

Useful storage cmdlets

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

Scanning through the Storage module there is a bunch of useful cmdlets – starting with the Get* cmdlets:

Get-Command -Module Storage get*

Get-Disk

Get-DiskImage

Get-FileIntegrity

Get-FileStorageTier

Get-InitiatorId

Get-InitiatorPort

Get-MaskingSet

Get-OffloadDataTransferSetting

Get-Partition

Get-PartitionSupportedSize

Get-PhysicalDisk

Get-ResiliencySetting

Get-StorageJob

Get-StorageNode

Get-StoragePool

Get-StorageProvider

Get-StorageReliabilityCounter

Get-StorageSetting

Get-StorageSubSystem

Get-StorageTier

Get-StorageTierSupportedSize

Get-SupportedClusterSizes

Get-SupportedFileSystems

Get-TargetPort

Get-TargetPortal

Get-VirtualDisk

Get-VirtualDiskSupportedSize

Get-Volume

Get-VolumeCorruptionCount

Get-VolumeScrubPolicy

 

£> Get-PhysicalDisk | Format-List FriendlyName, CanPool, OperationalStatus, HealthStatus, Usage, Size

 

 

FriendlyName : PhysicalDisk0

CanPool : False

OperationalStatus : OK

HealthStatus : Healthy

Usage : Auto-Select

Size : 256060514304

 

 

 

£> Get-Disk | ft -a

 

Number Friendly Name OperationalStatus Total Size Partition Style

—— ————- —————– ———- —————

0 HFS256G3AMNB-2200A Online 238.47 GB GPT

 

£> Get-Partition | ft -a

 

 

Disk Number: 0

 

PartitionNumber DriveLetter Offset Size Type

————— ———– —— —- —-

1 1048576 350 MB Recovery

2 368050176 200 MB System

3 577765376 128 MB Reserved

4 C 711983104 231.85 GB Basic

5 249663848448 5.96 GB Recovery

 

£> Get-Volume | ft -a

 

DriveLetter FileSystemLabel FileSystem DriveType HealthStatus SizeRemaining Size

———– ————— ———- ——— ———— ————- —-

C Windows NTFS Fixed Healthy 160.2 GB 231.85 GB

Windows RE tools NTFS Fixed Healthy 56.86 MB 350 MB

Recovery image NTFS Fixed Healthy 445.72 MB 5.96 GB

 


February 26, 2014  1:38 PM

Delete all but the last N

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

I was asked a question about deleting files from a folder based on age.  The requirement was to delete all but the youngest N files.

One solution is a classic PowerShell one-liner. It is actually one PowerShell pipeline though I’ve split it across multiple lines for ease of reading.

Get-ChildItem -Path c:\temp -Filter *.tmp -File | sort LastWriteTime -Descending | select -Skip 5 | Remove-Item

Use Get-Childitem to read the files you’re interested in. I’ve used the –File parameetr to restrict the examination to files.

Sort the files on LastWriteTime – you could use CreationTime if the files haven’t been changed since creation. You want the results sorted in descending order so the latest files are at the top of the list. Younger dates are greater than older dates.

use Select-Object to skip the first N (in this case 5) files and pipe everything else into remove-item.  Add a –whatif parameter to Remove-Item if want to see how its working before letting it loose.

A nice simple answer with a use for Select-Object’s –Skip parameter that I hadn’t thought of before.


February 25, 2014  4:51 PM

Requires

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

You may have seen this at the top of scripts or modules

#Requires –Version 3

This will stop the script running if the PowerShell version is 2.0 or below.

Other options are available

#Requires –PSSnapin      can be used to force the loading of a snapin

#Requires –Modules     can be used to force importing of modules

#Requires –Shellid    controls which shells the script can run in.  Default PowerShell installs have the ISE and console

#Requires –RunAsAdminstrator  – script must be running in instance of PowerShell with elevated privileges

These provide a simple and easy way to control execution without lots of code. I particularly like the last one.


February 25, 2014  12:16 PM

Waiting for restart

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

Some times you need to reboot a remote machine as part of your process.  PowerShell provides the Restart-Computer cmdlet to perform that task:

Restart-Computer -ComputerName server03

If you want you process to pause until the reboot has finished then you add the –Wait parameter:

Restart-Computer -ComputerName server03

You final options when pausing are to wait until a specific service – PowerShell, WinRM or WMI is avialable:

Restart-Computer -ComputerName server03 -Wait -For PowerShell

Restart-Computer -ComputerName server03 -Wait -For WinRM

Restart-Computer -ComputerName server03 -Wait -For Wmi

These options should cover most situations as these services are among the last to become available when a machine starts.


February 24, 2014  12:19 PM

AD Month of Lunches on the way

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

My Learn AD management in a Month of Lunches has gone to the printers – expect it soon


February 21, 2014  11:42 AM

DSC and WMI

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

Like all well behaved technology Desired State Configuration (DSC) requires access to WMI. Specifically it needs root/Microsoft/Windows/DesiredStateConfiguration

This is available on the Windows 2012 R2 DSC server you configure but isn’t available on older versions of Windows.

You must install Windows Management Framework 4.0  (PowerShell 4.0) on any machine that you are going to configure through DSC. Windows Server 2012 already has it installed but for Windows 2008 R2 & Windows 2012 you’ll need to download the appropriate version of WMF 4.0 and install it.


February 21, 2014  2:53 AM

PowerShell Summit NA 2014 approaching

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

With the PowerShell 65 days away there is still plenty of time to get registered. This is the only event this year that will feature such a line up of PowerShell experts – including the PowerShell team themselves.

The attendees at last year’s Summit decided it was an excellent experience – this years will be just as good.

Registration details from http://powershell.org/wp/community-events/summit/


February 19, 2014  12:15 PM

Oh–so that’s what its for

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

I’ve never really understood the logic behind the Wait-Job cmdlet. The idea of PowerShell Jobs is to kick of tasks that grind away in the background and you get the prompt back so you can keep on working. Wait-Job stops the prompt being returned until one or more jobs have finished.

It clicked this morning – I need to run some tasks in parallel and decided that a number of jobs was the best way (I looked at workflow but there are some complications such that I didn’t pursue that route). The only complication with using Jobs is that you don’t know when they finish (unless you check with Get-Job) and I needed a number of Jobs to finish before starting the last job in the series – this is a data dependency issue that I can’t avoid.

Wait-Job is the answer. Start my first set of jobs. Use:

Get-Jobs | Wait-Job

which means nothing else is done until all my jobs finish and then start the last job.

As I’m going to be running this through a scheduled task I don’t care about the prompt being frozen.

Nice to finally find a use for a cmdlet I’ve ignored since it appeared.


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: