PowerShell for Windows Admins


April 12, 2013  12:41 PM

Creating a new disk

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

I really like Windows Server Core. The concept has come of age in Windows 2012.

I needed to add a new disk to a virtual machine  – that’s easy using the Hyper-V cmdlets. But what about formating the disk.

A module new to Windows 2012 & Windows can be used.  Its the Storage module.  I’ve not had chance, or reason, to play with this module yet. So many cmdlets so little time.

Start with viewing the disks:

PS C:\Users\richard> Get-Disk | ft -a

Number Friendly Name          OperationalStatus Total Size Partition Style
—— ————-          —————– ———- —————
0      Virtual HD ATA Device  Online                120 GB MBR
1      Microsoft Virtual Disk Offline               127 GB RAW

 

Disk 1 is the new disk so need to initialise it.

PS C:\Users\richard> Initialize-Disk -Number 1 -PartitionStyle MBR

View the disks again

PS C:\Users\richard> Get-Disk | ft -a

Number Friendly Name          OperationalStatus Total Size Partition Style
—— ————-          —————– ———- —————
0      Virtual HD ATA Device  Online                120 GB MBR
1      Microsoft Virtual Disk Online                127 GB MBR

 

Create a partition on the disk -   -useMaximimSize means use all of the disk for this partition

PS C:\Users\richard> New-Partition -DiskNumber 1 -UseMaximumSize -DriveLetter R

Now view the partitions

PS C:\Users\richard> Get-Partition | ft -a

   Disk Number: 0

PartitionNumber DriveLetter Offset         Size Type
————— ———– ——         —- —-
1                           1048576      350 MB IFS
2               C           368050176 119.66 GB IFS

   Disk Number: 1

PartitionNumber DriveLetter Offset    Size Type
————— ———– ——    —- —-
1               R           1048576 127 GB Logical

And finally format the new disk:

PS C:\Users\richard> Get-Volume | where DriveLetter -eq R | Format-Volume -FileSystem NTFS -NewFileSystemLabel Backup

Confirm
Are you sure you want to perform this action?
Warning, all data on the volume will be lost!
[Y] Yes  [A] Yes to All  [N] No  [L] No to All  [S] Suspend  [?] Help (default is "Y"): Y

You get a nice friendly warning (you could bypass using –Confirm $false) and the format happens

You could pipe the cmdlets together to do everything in one pass. Best of all – the cmdlets are WMI based.

April 11, 2013  1:51 PM

Windows Server Backup

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

Windows Server 2012 has a PowerShell enabled backup utility. When you enable the feature you get a module called WindowsServerBackup.  It has the cmldets you would expect for creating and managing backups. No surprise you may say as this was avialable in Windows 2008 R2.

The difference with Windows Server 2012 is that you can do restores from PowerShell cmdlets whcih wasn’t available in the earlier version.

The restore cmdlets are

Start-WBFileRecovery

Start-WBHyperVRecovery

Start-WBSystemStateRecovery

Start-WBVolumeRecovery

 

This might not replace your currebt backup system but is very useful for backing up test environments and experimenting with things like authorative AD restores.


April 8, 2013  11:16 AM

Running workflows

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

I tripped over an interesting issue recently regarding the running of PowerShell workflows.

Consider the world’s simplest workflow

workflow test-w1 {"hello world"}

If I run this on a 32bit Windows 8  PowerShell machine – it works

If I run this on Windows 2012 (64bit) on PowerShell it works

if I run this on Windows 2012 PowerShell (x86) – it doesn’t work!

Be aware of how you are running your workflows


April 6, 2013  9:38 AM

AD Management in a Month of Lunches

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

The MEAP marches on with chapter 8 now released:

Chapter 8 – creating Group Policies

details from http://www.manning.com/siddaway3/


April 4, 2013  2:44 PM

Manning Deal of the Day – April 6 2013

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

My PowerShell and WMI book will be Manning’s deal of the day for 6 April 2013.  The deal will go live at Midnight US ET and will stay active for about 48 hours.

This is your chance to get the book with a 50% discount.

Use code dotd0406au at manning.com/siddaway2/

The Deal of the Day offer also applies to SharePoint Workflow in Action (http://www.manning.com/wicklund/).

Enjoy


April 3, 2013  1:17 PM

Putting the date in a file name

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

I often need to create file names that include the date & time the file was created in the name. I’ve come up with all sorts of ways to do but this I think is the simplest.

I want the date in this format:  year-month-day-hour-minute-second.  In other words a format that is easily sortable. I discovered that if you convert a data to a string there is a formatter that does most of the work for you.  That’s a lower case s.

PS> (Get-Date).ToString("s")
2013-04-03T20:09:31

You can’t have a : symbol in a file name so need to get rid of those

PS> (Get-Date).ToString("s").Replace(":","-")
2013-04-03T20-10-02

To complete the file name

PS> $datestring = (Get-Date).ToString("s").Replace(":","-")
PS> $file = "c:\folder\Prefix_$datestring.txt"
PS> $file
c:\folder\Prefix_2013-04-03T20-16-48.txt
PS>

I’ve done this as a two step process otherwise when you replace the : you also take out the one for the disk drive – oops

Enjoy


April 3, 2013  1:01 PM

PowerShell excerpt week

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

The Scripting Guy is running a series of excerpts from the PowerShell books published by Manning.  Today is PowerShell in Practice http://blogs.technet.com/b/heyscriptingguy/archive/2013/04/03/excel-spreadsheets.aspx

Check out the deals all this week on Manning PowerShell books


April 1, 2013  10:45 AM

MVP renewal

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

This afternoon I received the email notifying me that my MVP award had been renewed for another year.

Thank you to Microsoft – I regard the award as a great honour.

And thank you to the PowerShell community – its a great place to be


April 1, 2013  5:17 AM

Shutting down a remote computer

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

PowerShell provides the Stop-Computer cmdlet for closing down a remote machine. I find this especially useful in my virtual test environment. I’ll have several machines running but won’t necessarily have logged onto them. Using Stop-Computer means that I can shut them down cleanly without the hassle of logging onto them.

In modern Windows systems you have to explicitly enable remote WMI access through the Windows firewall. Stop-Computer uses WMI. If the WMI firewall ports aren’t enabled you can’t use Stop-Computer. I’ve taken to use the CIM cmdlets rather than WMI so sometimes don’t open the WMI firewall ports.

One quick function later and I have an answer

function invoke-cimshutdown {            
[CmdletBinding()]            
param (            
 [string]$computername            
)            
$comp = Get-CimInstance win32_operatingsystem -ComputerName $computername            
Invoke-CimMethod -InputObject $comp -MethodName Shutdown            
}

Pass the computer name as a parameter – I deliberately didn’t put a default

Use Get-CimInstance to get the Win32_operatingsystem class and use Invoke-CimMethod to call the Shutdown method.

Another reason not to enable WMI on my server 2012 firewalls.

You can use this on legacy versions of Windows if you have PowerShell v3, and therefore WSMAN v3, installed


March 26, 2013  3:02 PM

CIM cmdlets

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

The CIM cmdlets are found in the CIMcmdlets module.

Get-Command -Module CimCmdlets  produces this list of names.  I’ve added some information on the tasks they perform

Get-CimAssociatedInstance  is for working with WMI associated classes
Get-CimClass  is for discovering the properties and methods of a WMI class
Get-CimInstance    is analogous to  Get-WmiObject
Get-CimSession 
Invoke-CimMethod    is analogous to Invoke-WMIMethod   
New-CimInstance  can be used for creating a new WMI instance in certain circumstances
New-CimSession
New-CimSessionOption
Register-CimIndicationEvent    is analogous to Register-WMIEvent
Remove-CimInstance  is analogous to Remove-WMIObject
Remove-CimSession
Set-CimInstance  is analogous to Set-WMIInstance

The CIM session cmdlets are for working with the CIm sessions which are analogous to PowerShell remoting sessions but are used by the CIM cmdlets AND the new WMI based cmdlets in Windows 8/2012 such as the networking cmdlets


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: