PowerShell for Windows Admins


March 15, 2014  4:48 AM

Module confusion–what’s really in PowerShell 3 and 4

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

If I run

Get-Module –ListAvailable

on my Windows 8.1 system (PowerShell 4.0) I find a total of 58 modules installed in $pshome\modules i.e. C:\windows\system32\WindowsPowerShell\v1.0\Modules

These are the modules installed as part of PowerShell. Right?

Wrong?

Compare this with a Windows 7 machine that’s been upgraded to PowerShell 4.0. Repeating the exercise I find a grand total of 15 modules.

So where has the other 75% of my functionality gone.

This is the breakdown down of the modules installed on the Windows 7 machine – terminology is mine (feel free to disagree with the groupings).

Core PowerShell:

Microsoft.PowerShell.Diagnostic

Microsoft.PowerShell.Host

Microsoft.PowerShell.Management

Microsoft.PowerShell.Security

Microsoft.PowerShell.Utility

CimCmdlets

ISE

Microsoft.WSMan.Management

File Transfer module

BitsTransfer

Troubleshooting:

PSDiagnostics

TroubleshootingPack

Other related engines:

PSDesiredStateConfiguration

PSScheduledJob

PSWorkflow

PSWorkflowUtility

and that’s it. Everything else as far as I can determine is CDXML based and therefore can’t be made available on legacy systems. The following modules are included in that category:

AppBackgroundTask

AppLocker

Appx

AssignedAccess

BitLocker

BranchCache

Defender

DirectAccessClientComponents

Dism

DnsClient

International

iSCSI

Kds

MMAgent

MsDtc

NetAdapter

NetConnection

NetEventPacketCapture

NetLbfo

NetNat

NetQos

NetSecurity

NetSwitchTeam

NetTCPIP

NetWNV

NetworkConnectivityStatus

NetworkTransition

PcsvDevice

PKI

PrintManagement

ScheduledTasks

SecureBoot

SmbShare

SmbWitness

StartScreen

Storage

TLS

TrustedPlatformModule

VpnClient

Wdac

WindowsDeveloperLicense

WindowsErrorReporting

WindowsSearch

A CDXML based module is produced by taking a CIM (WMI) class and wrapping it in some simple XML and saving it as a .cdxml file. This can then be published as a PowerShell module. The CIM classes won’t be made available on downlevel systems so you won’t find this functionality on anything earlier than Windows 8/2012.

If you need the functionality supplied by these modules you will either have to use the old command line tools or a WMI class – much of the functionality of the NetAdapter and NETCPIP modules for instance can be duplicated using Win32_NetworkAdapter and Win32_NetworkAdapterConfiguration..

March 12, 2014  12:23 PM

AD Management in a Month of Lunches in print

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

Manning have released the print edition of Learn Active Directory Management in a Month of Lunches – http://www.manning.com/siddaway3/ – joining the pdf version released earlier.  ePub & Kindle versions scheduled for 20 March


March 12, 2014  4:49 AM

Get-ADUser issue fixed

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

The issue that I discussed in these posts:

http://richardspowershellblog.wordpress.com/2013/11/08/get-aduser-issue-2/

http://richardspowershellblog.wordpress.com/2013/11/06/get-aduser-issue/

With Get-ADUser and –Properties * in a forest level below 2012 R2 has been resolved  in this update:

http://support.microsoft.com/kb/2923122


March 9, 2014  4:59 AM

End of PowerShell Jobs week

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

My series on PowerShell Jobs for the Scripting Guy blog has finished.  The set of articles is:

Jobs week 1 : Introduction to PowerShell jobs http://blogs.technet.com/b/heyscriptingguy/archive/2014/03/02/powershell-jobs-week-introduction-to-powershell-jobs.aspx  http://blogs.technet.com/b/heyscriptingguy/archive/2014/03/02/powertip-control-job-starts-with-powershell.aspx

Jobs week 2 : WMi and CIM jobs http://blogs.technet.com/b/heyscriptingguy/archive/2014/03/03/powershell-jobs-week-wmi-and-cim-jobs.aspx  http://blogs.technet.com/b/heyscriptingguy/archive/2014/03/03/powertip-use-powershell-to-keep-job-data.aspx

Jobs week 3 : Remote Jobs http://blogs.technet.com/b/heyscriptingguy/archive/2014/03/04/powershell-jobs-week-remote-jobs.aspx  http://blogs.technet.com/b/heyscriptingguy/archive/2014/03/04/powertip-use-powershell-to-find-job-help.aspx

Jobs week 4 : Scheduled Jobs http://blogs.technet.com/b/heyscriptingguy/archive/2014/03/05/powershell-jobs-week-scheduled-jobs.aspx  http://blogs.technet.com/b/heyscriptingguy/archive/2014/03/05/powertip-use-powershell-to-discover-tasks-associated-with-scheduled-jobs.aspx

Jobs week 5 : Workflow Jobs http://blogs.technet.com/b/heyscriptingguy/archive/2014/03/06/powershell-jobs-week-jobs-and-workflows.aspx  http://blogs.technet.com/b/heyscriptingguy/archive/2014/03/06/powertip-stop-a-powershell-job.aspx

Jobs week 6 : Job processes http://blogs.technet.com/b/heyscriptingguy/archive/2014/03/07/powershell-jobs-week-job-processes.aspx http://blogs.technet.com/b/heyscriptingguy/archive/2014/03/07/powertip-protect-the-data-produced-by-powershell-jobs.aspx

Jobs week 7 : Jobs in the enterprise http://blogs.technet.com/b/heyscriptingguy/archive/2014/03/08/powershell-jobs-week-jobs-in-the-enterprise.aspx http://blogs.technet.com/b/heyscriptingguy/archive/2014/03/08/powertip-find-more-information-about-windows-powershell-jobs.aspx

 

Enjoy


March 5, 2014  2:34 PM

PowerShell Summit 2014 edging closer

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

Now we’re in March the Summit is getting closer.  Looking forward to see friends old & new and listening to some excellent sessions.

If you haven’t registered there’s still places available.

As a bonus one of the things you’ll take away is a module for working with the registry – filling one of gaps in the PowerShell cmdlets for Windows administrators


March 2, 2014  3:10 PM

PowerShell Jobs Series

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

Today sees the start of my series of articles on PowerShell jobs for the Scripting Guy blog.

First article is here –

http://blogs.technet.com/b/heyscriptingguy/archive/2014/03/02/powershell-jobs-week-introduction-to-powershell-jobs.aspx

 

with the associated Power Tip here

http://blogs.technet.com/b/heyscriptingguy/archive/2014/03/02/powertip-control-job-starts-with-powershell.aspx


February 28, 2014  4:44 PM

do and while

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

Do you understand the difference between do loops and while loops?  On the surface they appear very similar.

£> do{$i++}while($i -lt 10) £> $i 10

£> $i=0 £> while($i -lt 10){$i++} £> $i 10

BUT there is a difference

£> $i=11 £> do{$i++}while($i -lt 10) £> $i 12

£> $i=11 £> while($i -lt 10){$i++} £> $i

Notice the difference in the final result

A DO loop will ALWAYS execute at least once because the test is at the end.

A while loop has the test at the beginning so may not execute at all.

I’ve seen this cause people real difficulty at times so think about where you need the test to be, if the loop needs to execute at least once and what the final value of any incrementing variables should be


February 28, 2014  2:33 PM

Automatically create folder for home drive

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

In this post  http://richardspowershellblog.wordpress.com/2013/10/28/setting-ad-attributes-from-a-csv-file/

I showed how to modify the user’s home folder setting in Active Directory.

A comment was recently left asking about automatically creating the folder on the fileserver and creating the share that is associated with it.

This isn’t a simple exercise – you will need a script to:

You can create the folder using New-Item

New-Item -Path c:\test -Name anyolduser -Type Directory

You can share it

$max = [uint32]5

$type = [uint32]0

Invoke-CimMethod -ClassName Win32_Share -MethodName Create -Arguments @{Name=’anyolduser’; Path=’c:\test\anyolduser’; Type=$type; MaximumAllowed=$max; Description=’anyolduser – homedrive’}

And then you have to set share and NTFS permissions according to your organization’s policies


February 28, 2014  11:55 AM

File or Folder?

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

This is a common type of use for Get-ChildItem:

Get-ChildItem -Path c:\temp

However, you could get files or folders returned.  Very often you just want to see the files

In PowerShell 1.0 & 2.0 you had to do this:

Get-ChildItem -Path c:\temp   | where {-not $_.PSIsContainer}

If you want to see the subfolders only then you reverse the condition:

Get-ChildItem -Path c:\temp  | where {$_.PSIsContainer}

In PowerShell 3.0 Get-ChildItem got some new parameters –File and –Directory. They display files only or folders only respectively. The default action is still to display files and folders.

Get-ChildItem -Path c:\temp  -File  Get-ChildItem -Path c:\temp  -Directory

I still see a lot of people making more work for themselves because they don’t use or don’t know about the new parameters.


February 27, 2014  4:20 PM

I’m afraid you can’t do that anymore

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

In PowerShell 1.0 you could do this:

notepad

$proc = Get-WmiObject -Class Win32_Process -Filter “Name=’notepad.exe'”

$proc.Terminate()

To access the methods of the WMI class you had to get a variable representing the instance and call the method. This technique still works in PowerShell 4.0

When the CIM cmdlets were introduced in PowerShell 3.0 people assumed that they worked the same:

£> notepad

£> $proc = Get-CimInstance -ClassName Win32_Process -Filter “Name=’notepad.exe'”

£> $proc.Terminate()

Method invocation failed because [Microsoft.Management.Infrastructure.CimInstance] does not contain a method named

‘Terminate’.

At line:1 char:1

+ $proc.Terminate()

+ ~~~~~~~~~~~~~~~~~

+ CategoryInfo : InvalidOperation: (Terminate:String) [], RuntimeException

+ FullyQualifiedErrorId : MethodNotFound

 

The CIM cmdlets retrun inert objects – NO METHODS – so you can’t do that.

The correct way to use the CIM cmdlets is:

Get-CimInstance -ClassName Win32_Process -Filter “Name=’notepad.exe'” | Invoke-CimMethod -MethodName Terminate

 


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: