PowerShell for Windows Admins


March 10, 2012  5:18 AM

Migrating to CIM–part 1

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

I have created many WMI posts on this blog over the years. With PowerShell v3 a new API has been introduced and new cmdlets are available for working with WMI. The new functionality offers many improvements and advantages over using the WMI cmdlets so I thought that a series of articles showing the evolution of a piece of WMI based PowerShell might be useful.

Much of our work with WMI is based on retrieving data e.g.

Get-WmiObject -Class Win32_ComputerSystem

In PowerShell v3 this could become

Get-CimInstance -ClassName Win32_ComputerSystem

 

OK that seems simple but what about using the WMI type accelerators

$HKLM = 2147483650            
$key = "SYSTEM\CurrentControlSet\Services"            
$reg = [wmiclass]'\\.\root\default:StdRegprov'            
$subkeys = $reg.EnumKey($HKLM, $key)            
$subkeys.snames

These don’t exist in the CIM cmdlets.  What we do have is Invoke-CimMethod so we can do this

[uint32]$HKLM = 2147483650            
$key = "SYSTEM\CurrentControlSet\Services"            
Invoke-CimMethod -Namespace "root\default" -ClassName StdRegProv -MethodName EnumKey  -Arguments @{hDefKey=$HKLM; sSubKeyName =$key} |            
select -ExpandProperty sNames

There are a few important points we need to note:

  • The value to define the hive has to be an unsigned integer
  • we have to supply the correct argument name
  • The arguments are provided in a hash table
  • ClassName is used instead of class
  • The results are a collection that we have to expand

So how do we find this information – see part 2

March 8, 2012  1:36 PM

PowerShell web access

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

Would you like to be able to access a PowerShell console in a web browser to manage your remote machines?

You would – then PowerShell web access in Windows Server 8 is just for you

Introductory details here:

http://blogs.msdn.com/b/powershell/archive/2012/03/07/introducing-windows-powershell-web-access-in-windows-server-8-beta.aspx


March 6, 2012  12:50 PM

PowerShell v3 in Depth–offer

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

 

I announced the availability of PowerShell v3 in Depth on Manning’s Early Access Programme

http://msmvps.com/blogs/richardsiddaway/archive/2012/03/02/powershell-v3-in-depth.aspx

Now you have an opportunity to get this book by Don Jones, Jeff Hicks and myself at a significant bargain.  Buy from www.manning.com and get a 50% discount using the code is "PSID12". The offer is valid through Friday 9 March 2012.


March 6, 2012  12:45 PM

UK PowerShell group–March 2012

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

Here are the details of the March meeting


When: Tuesday, Mar 27, 2012 7:30 PM (GMT)


Where: Virtual

*~*~*~*~*~*~*~*~*~*

PowerShell v3 introduces a new way of working with WMI plus the option of creating your own cmdlets directly from WMI classes

Notes


Richard Siddaway has invited you to attend an online meeting using Live Meeting.
Join the meeting.
Audio Information
Computer Audio
To use computer audio, you need speakers and microphone, or a headset.
First Time Users:
To save time before the meeting, check your system to make sure it is ready to use Microsoft Office Live Meeting.
Troubleshooting
Unable to join the meeting? Follow these steps:

  1. Copy this address and paste it into your web browser:
    https://www.livemeeting.com/cc/usergroups/join
  2. Copy and paste the required information:
    Meeting ID: BDFCK7
    Entry Code: rfF^w>$3X
    Location: https://www.livemeeting.com/cc/usergroups

If you still cannot enter the meeting, contact support

Notice
Microsoft Office Live Meeting can be used to record meetings. By participating in this meeting, you agree that your communications may be monitored or recorded at any time during the meeting.


March 4, 2012  2:45 PM

Upload through BITS

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

Downloading through BITS is easy as we have seen,  Uploading requites some work. First the BITS IIS extensions have to be loaded.  Restart IIS and look at the properties of the Virtual Directory being used for the transfer.  At the bottom you should find a BITS upload option in the Other section.  Double click and enable uploads.  Configure other properties as desired.

A single file upload can be achieved like this

Start-BitsTransfer -Source c:\source\transfer\testupload.txt -Destination http://webr201/transfer/testupload.txt -TransferType Upload

For multiple file uploads try this

Import-Module BitsTransfer -Force            
            
$computername = "WebR201"            
$source = "c:\source\transfer\"            
            
Get-ChildItem -Path $source |            
foreach {            
             
$destination =  "http://webr201/transfer/$($_.name)"            
Write-Host "Transferring $($_.Fullname) to $destination"            
            
Start-BitsTransfer -Source $($_.Fullname) -Destination $destination -TransferType Upload            
            
}

Import the module. Set the machine name & source folder. Run Get-ChildItem over the folder and for each file set up a BITS transfer source and destination variables using the name and fullname. Run the transfer.


March 4, 2012  7:27 AM

Windows Server 8–features removed or deprecated

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

When you start your implementation planning for Windows Server 8 make sure to check with this page for functionality that has been removed or deprecated (means it will be removed in a future version)

http://technet.microsoft.com/en-us/library/hh831568.aspx


March 3, 2012  8:26 AM

Windows 8 svchost

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

When you run get-process you will see a number of entries named svchost.  These are processes that host services. In task manager on Windows 7 and below all we see is the svchost entry. In the Windows 8 family we see the services running in a particular host

Now can we do anything similar with PowerShell.

Get-Service doesn’t do anything to help BUT the Win32_Service class has a ProcessID property that we can use

Get-CimInstance -ClassName Win32_Service |             
sort ProcessID -Descending |            
foreach {            
  $proc = Get-Process -Id $_.ProcessID            
  $_ | Add-Member -MemberType NoteProperty -Name ProcessName -Value $($proc.ProcessName) -PassThru            
} |            
Format-Table DisplayName, ProcessName -GroupBy ProcessId

I’ve used Get-CimInstance in this example because of the CIM cmdlets are new to PowerShell v3. For a PowerShell v2 install use Get-WmiObject

Get-WmiObject -Class Win32_Service |             
sort ProcessID -Descending |            
foreach {            
  $proc = Get-Process -Id $_.ProcessID            
  $_ | Add-Member -MemberType NoteProperty -Name ProcessName -Value $($proc.ProcessName) -PassThru            
} |            
Format-Table DisplayName, ProcessName -GroupBy ProcessId

The WMI objects are piped through sort & into foreach where we add the process name. In both cases I’ve used Get-Process – either Get-CIMInstance or Get-WmiObject using the Win32_Process class could be used instead.

Format-table is used with GroupBy to produce a nicely formatted report. This information is important because it shows the services that are related to a particular process and the impact if that process fails..


March 3, 2012  6:04 AM

PowerShell v3 Updateable Help

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

One of the things that we have become used to in PowerShell v1 and v2 is the help files. They are installed with PowerShell, available on line and a graphical version is available for download. The online help files are updated over time but the local files aren’t.  This changes with PowerShell v3.

When you install PowerShell v3 (or install Windows 8/Windows Server 8 ) help files are not installed. Thats right you don’t get help files available. If you try using help you get this

 

PS> get-help get-process

NAME
    Get-Process

SYNTAX
    Get-Process [[-Name] <string[]>] [-ComputerName <string[]>] [-Module] [-FileVersionInfo]  [<CommonParameters>]

    Get-Process -Id <int[]> [-ComputerName <string[]>] [-Module] [-FileVersionInfo]  [<CommonParameters>]

    Get-Process -InputObject <Process[]> [-ComputerName <string[]>] [-Module] [-FileVersionInfo]  [<CommonParameters>]

ALIASES
    gps
    ps

REMARKS
    Get-Help cannot find the Help files for this cmdlet on this computer. It is displaying only partial help.
        — To download and install Help files for the module that includes this cmdlet, use Update-Help.
        — To view the Help topic for this cmdlet online, type: "Get-Help Get-Process -Online" or
           go to
http://go.microsoft.com/fwlink/?LinkID=113324.

 

Using –full doesn’t bring more information and –examples doesn’t help either.

This may seem like a huge step backwards – how are we going to work without the local help files. I use them on a regular basis. Help. I want my help files back.

OK – what we get in PowerShell v3 is updateable help.  This means that you can pull the help files from the web and get them installed. Notice the Remarks section above about using Update-Help. The PowerShell core module has 3 cmdlets related to help

Get-Help
Save-Help  – downloads help files for later use via update-help
Update-Help – downloads and immediately updates help files

To use Save-help or Update-help you need to start PowerShell with elevated privileges (Run as administrator)

NAME
    Save-Help

SYNTAX
    Save-Help [-DestinationPath] <string[]> [[-Module] <string[]>] [[-UICulture] <string[]>] [-UseDefaultCredentials]
    [-Force]  [<CommonParameters>]

    Save-Help [[-Module] <string[]>] [[-UICulture] <string[]>] -LiteralPath <string[]> [-UseDefaultCredentials]
    [-Force]  [<CommonParameters>]

PS> get-help Update-Help

NAME
    Update-Help

SYNTAX
    Update-Help [[-Module] <string[]>] [[-SourcePath] <string[]>] [[-UICulture] <string[]>] [-Recurse]
    [-UseDefaultCredentials] [-Force]  [<CommonParameters>]

    Update-Help [[-Module] <string[]>] [[-UICulture] <string[]>] [-LiteralPath <string[]>] [-Recurse]
    [-UseDefaultCredentials] [-Force]  [<CommonParameters>]

 

Notice that we can update the help for individual modules

Lets pull down the help files first

Save-Help -DestinationPath c:\source\helpfiles -UICulture en-US -Force –Verbose

You will see a progress bar and messages like

VERBOSE: Your connection has been redirected to the following URI:
"http://download.microsoft.com/download/3/4/C/34C6B4B6-63FC-46BE-9073-FC75EAD5A136/"
VERBOSE: Microsoft.PowerShell.Management: Saved
C:\source\helpfiles\Microsoft.PowerShell.Management_eefcb906-b326-4e99-9f54-8b4bb6ef3c6d_en-US_HelpContent.cab. Culture
en-US Version 3.0.0.0

for each help file that is downloaded.

the –Force parameter is interesting as it allows you to run a Save-Help or Update-Help command for the same module more than once each day.

I have found it is sometimes necessary to run save-help or update-help a couple of times to get all of the files downloaded

The help files can then be updated like this

Update-Help -SourcePath c:\source\helpfiles -UICulture en-US -Force –Verbose

 

You will get messages like this

VERBOSE: Microsoft.PowerShell.Management: Updated
C:\Windows\System32\WindowsPowerShell\v1.0\en-US\Microsoft.PowerShell.Commands.Management.dll-help.xml. Culture en-US
Version 3.0.0.0

 

VERBOSE: Microsoft.PowerShell.Core: Updated
C:\Windows\System32\WindowsPowerShell\v1.0\en-US\about_WMI_Cmdlets.help.txt. Culture en-US Version 3.0.0.0

 

Now

get-help Get-Process –full

will give you the help files you know and love

That is a two stage process but ahs the advantage that the help files can be pulled once and put on a network share for access by all systems. This is especially important for my servers where I don’t normally allow access to the Internet

The above tests were performed on PowerShell v3 on Windows 7 SP 1

The alternative is to update the help files directly

Update-Help  -UICulture en-US -Force –Verbose

 

Notice that in all cases I’ve given the UICulture – the help files come as a pair e.g.

Microsoft.PowerShell.Host_56d66100-99a0-4ffc-a12d-eee9a6718aef_en-US_HelpContent.cab
Microsoft.PowerShell.Host_56d66100-99a0-4ffc-a12d-eee9a6718aef_HelpInfo.xml

You need to get the culture right to pull down the correct help files. I use English culture but I’m not sure what other localised versions are available  at this stage in the development cycle.

If you are running a 64 bit OS it looks like the help for 64 and 32 bit versions of PowerShell are updated simultaneously.

The help files may not be complete at this stage of Powershell v3 development so don’t be surprised if you don’t get help updates  for all modules – especially on Windows 8 server


March 2, 2012  4:29 PM

Windows 8 Modules

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

We saw the modules that come with Windows server 8

 

This is the list of modules available in a vanilla Windows 8 32 bit install

AppLocker
Appx
BitLocker
BitsTransfer
BranchCache
CimCmdlets
DirectAccessClientComponents
Dism
DnsClient
International
iSCSI
Kds
Microsoft.PowerShell.Diagnostics
Microsoft.PowerShell.Host
Microsoft.PowerShell.Management
Microsoft.PowerShell.Security
Microsoft.PowerShell.Utility
Microsoft.WSMan.Management
MMAgent
MsDtc
NetAdapter
NetLbfo
NetQos
NetSecurity
NetSwitchTeam
NetTCPIP
NetworkConnectivityStatus
NetworkTransition
PKI
PrintManagement
PSDiagnostics
PSScheduledJob
PSWorkflow
ScheduledTasks
SecureBoot
SmbShare
SmbWitness
Storage
TroubleshootingPack
TrustedPlatformModule
Wdac
WindowsDeveloperLicense

Again the core PowerShell modules are italicised – though that concept is becoming redundant as PowerShell v3 auto loads all modules it finds on the module path


March 2, 2012  1:09 PM

PowerShell v3 in Depth

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

Hot on the heels on the announcements of that PowerShell v3 beta is available for download either as a free standalone product for installation on Windows 7, Windows 2008 & Windows 2008 R2 or as a commercial product where an operating system is included – Windows 8 or Windows server 8 to suit your mood

http://msmvps.com/blogs/richardsiddaway/archive/2012/03/01/windows-8-and-powershell-3-betas.aspx

comes the announcement from Manning of a new PowerShell book

PowerShell v3 in Depth

Written by Don Jones, Jeffrey Hicks and myself it covers PowerShell v3 in great detail – how to get the absolute maximum out of PowerShell. The 40 chapters will cover all aspects of PowerShell v2 and v3. This book is about PowerShell it self – we won’t cover working with AD, Exchange etc.

This is a book with a single voice as all 3 authors work on each chapter – with individual anecdotes, comments and asides as a bonus-   you get our collected and individual experience and knowledge.  If you only buy one PowerShell book it should be this one

Access to the early drafts is available from

http://www.manning.com/jones2/


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: