PowerShell for Windows Admins


November 27, 2014  2:46 PM

The little changes that make a difference

Richard Siddaway Richard Siddaway Profile: Richard Siddaway
Powershell

Each version of PowerShell introduces a new headline feature – remoting, workflows, DSC, OneGet in version 2,3,4 and 5 respectively. While this can change the way we work there are also a host of little changes that come along that are often overlooked.

One example is a change to Get-ChildItem introduced in PowerShell 3.0.

Consider getting a directory listing:

Get-ChildItem -Path C:\Windows

This will give all subfolders and file in the given folder.

If you just wanted the files you had to do this:

Get-ChildItem -Path C:\Windows | where {$_.PSIsContainer}

If you want just the files you use:
Get-ChildItem -Path C:\Windows | where {-not $_.PSIsContainer}

or the slightly shorter but not as easy to read:
Get-ChildItem -Path C:\Windows | where {!$_.PSIsContainer}

The PSIsContainer property name is not intuitive and I rarely remember the name exactly and try ISPSContainer first or some other variant.

Two additional filtering parameters were added to Get-ChildItem

Get-ChildItem -Path C:\Windows –Directory

and
Get-ChildItem -Path C:\Windows -File

produce listings of folders and files respectively.

A small simple change that makes life easier.

There are a lot of small changes like this scattered through the later PowerShell versions – I’d recommend going through the release notes to track down the ones that will be useful to you.

November 26, 2014  1:53 PM

Persisting PowerShell Objects

Richard Siddaway Richard Siddaway Profile: Richard Siddaway
Powershell

I was asked recently about persisting PowerShell objects. The idea was to test a particular property on a semi-regular basis and save the object with the highest value for the property.  If the next test has a higher value it is saved and overwrites the existing object.

 

There are a number of ways to do this – if you are running the test very frequently then you could keep the object in memory as a variable. If you are testing fairly infrequently then you may want to persist the object to disk.  The CliXml cmdlets are good for this.

 

Start by creating a reference object:

Get-Process -Name powershell | Export-Clixml -Path proc1.xml

 

Then run some more tasks in PowerShell to increase the CPU usage. You can do this with any property that will change over time.  CPU is an easy example. You can then run your test. I used an instance of ISE to do the test so I didn’t alter the value by running the test.

 

$proc = Get-Process -Name powershell

Get-ChildItem -Path .\proc1.xml

$psp = Import-Clixml -Path .\proc1.xml

if ($proc.CPU -gt $psp.CPU){

$proc | Export-Clixml -Path proc1.xml

}

Get-ChildItem -Path .\proc1.xml

 

Get the current data and test the saved file. Import the saved file.  Test the CPU values and if the new value is higher save the object by overwriting the file.  Repeat as required.

 

The help files for Export-CliXml and Import-CliXml should be read.


November 25, 2014  2:17 PM

PowerShell Summit Europe 2015 – - topics to include

Richard Siddaway Richard Siddaway Profile: Richard Siddaway
Powershell

Are you planning on attending the PowerShell Summit Europe 2015.  If so what topics would you like to see covered. The Summit is deliberately pitched at a high level so you won’t find beginner level content.

If there’s a topic you’d like to be included in the agenda – leave a comment explaining what it is and why you think it would make a good Summit topic. No promises but we’ll see what we can do


November 24, 2014  2:05 PM

Call for Presentations for PowerShell Summit Europe 2015

Richard Siddaway Richard Siddaway Profile: Richard Siddaway
Powershell

The PowerShell Summit is the number one conference where PowerShell enthusiasts gather and learn from each other in fast-paced, knowledge packed presentations. PowerShell experts from all over the world including MVP’s, Guru’s, community leaders and PowerShell team members, will once again join together for a few days in Stockholm, Sweden to discuss and learn about maximizing PowerShell in the workplace. If you want to share your PowerShell expertise or story, then this is your official call to submit presentations for selection!

PowerShell Summit Europe 2015 will be held 14-16 September 2015 in Stockholm, Sweden.

Topic Areas – What we are looking for

We are looking for 45-minute presentations covering a wide aspect of PowerShell expertise. We have two main topic areas that may assist you in building an abstract.

PowerShell Internals – A deep look into the inside workings of PowerShell and practical solutions that are built from them. These presentations are typically more directed to the PowerShell development community that is building extensions and solutions relating to PowerShell.

PowerShell Features Deep Dive – These presentations are a deep look into configuring and working with PowerShell features and capabilities such as Remoting, Desired State Configuration and more. These presentations tend to be more IT Pro focused.

We are open to presentations across the entire ecosystem that has been built around PowerShell; so don’t hesitate to send an abstract for your particular area of expertise. This includes Microsoft platforms and products that have PowerShell-based management tools as well as 3rd parties such as VMware. New topics will be preferred over recycling of older topics – look to see what’s new in PowerShell 5.0 and use the questions on PowerShell.org to spot areas of confusion that could supply a good session for the Summit.

What kind of sessions get selected?

We’re looking for sessions that go beyond – often way beyond – “beginner.” If you want to see examples of the depth we’re looking for use the recordings on the PowerShell.org Youtube channel from the PowerShell Summit Europe 2014 as a guide. We look for an abstract that’s compelling and makes us salivate to see your session – so spend time writing a punchy abstract! We want sessions that offer real-world usability combined with “wow, nobody talks about THAT” awesomeness. If in doubt aim high. Remember, Summit sessions are recorded, so if you’ve previously presented a topic at a Summit, we’re less likely to choose it for another Summit. We want sessions that are challenging, and that ideally present things that simply aren’t explained or documented elsewhere. New modules, new techniques, and crazy approaches are all welcome. Discussion-format sessions are great, too, especially if you plan to turn them into a community deliverable (like a “best practices for writing DSC Resources” session that gets turned into a free e-guide later). Think community, deep dive, engaging, and amazing as keywords. We want attendees to finish each day with information leaking… just a little bit… out their eyeballs. Help us make it happen.

We do have some goals for speaker selection, too. We obviously have, and appreciate, the great involvement we get from the product team. We aim to have a certain number of sessions from well-known members of the community, simply because they’re well-known for a reason – they do a great job! But we also set aside slots for newcomers who’ve never presented before, or who’ve maybe only presented once or twice before – the audience will judge you on content not style. We want to create opportunities for more folks to become engaged and active in our community, and the Summit is a great way to do that.

We aren’t looking for soft-skills sessions, like “how to get a new user group running,” although contact us via email (summit@) if you’d like to do something like that as an extra evening thing after the main content wraps for the day.

Please note all sessions are to be delivered in English. Presenter will provide all equipment needed to deliver session(s), including a laptop or other computer. Presenter must be able to provide video by means of HDMI, DVI-D, or DisplayPort connectors – VGA is NOT supported. Presenter must be able to manually select an appropriate screen resolution for video output. Typically, 1024×768 or 1280×720 are preferred.

How to submit abstracts of presentations

Presentations will be 45-minutes in length and the submission should include the following:

Presentation Title

Presentation abstract – a description of the presentation and the topics covered. 250 words or less and suitable for marketing.

Go to http://eventmgr.azurewebsites.net/event/register/PSEU15/Europe%202015%20Member?preregister=1. This is the only valid URL for pre-registration. Provide your e-mail address, password, and full name. You’re creating a new account, even if you’ve attended past Summit events.

Do NOT attempt to register for the Summit as an attendee at this stage – we will be opening registration in late February 2015.

Click Abstracts

Click Submit Abstract

Provide a title and description; descriptions must be 50-250 words. Set the Status to “Ready to Review” when you are ready to send your session to us for consideration.

To return to the site at a later time, go to http://eventmgr.azurewebsites.net/event/login/PSEU15. Click Log In. You can then re-visit Abstracts.

Note that you must set your abstract status to Ready for Review or we won’t see it. If you leave it in Pending, it won’t be considered.

You can submit multiple presentations in the same topic area or for different ones. Be aware that even though the session length is 45 minutes we prefer to have at least 10 minutes set aside for questions. Summit presentations are intense and intimate often with plenty of audience interaction. You must expect questions and discussions. This is not a “lecture to the audience” event. Also because of the session length, generally co-presenters are unnecessary, but that is not a requirement.

Presentation submission deadline – When you should send it by

Start sending your presentation submissions immediately! The selection committee will start selecting presentations as soon as they arrive so you don’t want to miss out. The last day we will accept presentation submissions will be Sunday 11 January 2015. This is a hard deadline.

When you will know you’ve been selected

The selection committee will start reviewing submissions immediately and begin the selection process. You will be informed if one or more of your presentations have been selected and sent a contract on or before Sunday 18 January 2015. You will need to return the signed contract by Wednesday 28 January 2015 otherwise another speaker may be offered the opportunity.

Speakers, with accepted sessions, will be given free admission to the event, including attendance at all official Summit activities. However, AWPP membership is not included. Speakers may not bring guests to the day sessions or evening events. We have a limited budget, and the number of speakers selected will be partially governed by that budget. Speakers are responsible for their own travel expenses, including hotel, airfare, and ground transportation.

The final agenda will be announced and posted on PowerShell.Org on, or about, Monday 2 February 2015.

We look forward to your submissions and your help in making PowerShell Summit Europe 2015 the most valuable IT/Dev conference of the year building on and surpassing the Europe 2014 Summit!


November 23, 2014  7:30 AM

Creating NIC team without knowing the team members

Richard Siddaway Richard Siddaway Profile: Richard Siddaway
Powershell

I was asked how to create a NIC team only using the 1GB adapters without knowing how many 1gb NICs were on the server.

 

I think this should solve the problem

 

New-NetLbfoTeam -TeamMembers (Get-NetAdapter | where Speed -ge 1gb | select -ExpandProperty Name) -Name MyTeam

 

Use New-NetLbfoTeam to create the team. The team member names are generated by

 

Get-NetAdapter | where Speed -ge 1gb | select -ExpandProperty Name

 

By putting that statement in parentheses as the value for the –TeamMembers parameter the results are used as the value for the parameter.  Shouldn’t matter now how many NICs or what they are called.  You can modify the filter criteria as required.


November 17, 2014  2:55 PM

PowerShell books = Deal of the Day

Richard Siddaway Richard Siddaway Profile: Richard Siddaway
Books, Powershell

Manning’s Deal of the Day for 18 November 2014 includes PowerShell in Depth, second edition and PowerShell and WMI


November 14, 2014  4:51 AM

PowerShell is 8 years old this month

Richard Siddaway Richard Siddaway Profile: Richard Siddaway
Powershell

Strange as it may seem PowerShell 1.0 was release in November 2006. In that time we’ve had a lot of changes:

- PowerShell support added to all major Microsoft products – except Office!

- Third party adoption of PowerShell – I am now disappointed if a product doesn’t have PowerShell support rather than being surprised that it does

 

Version 2.0

- PowerShell remoting

- Background jobs

- More WMI cmdlets

- More cmdlets

 

Version 3.0

- CIM cmdlets

- CIM sessions

- CDXML

- PowerShell workflow

 

Version 4.0

- Desired State configuration

 

Version 5.0

- Oneget

- PowerShell get

 

The PowerShell community has grown phenomenally:

- powershell.org

- PowerShell Summits in North America & Europe

- User groups across the world

 

Really looking forward to the next 8 years


November 12, 2014  2:14 PM

PowerShell in Depth, second edition in print

Richard Siddaway Richard Siddaway Profile: Richard Siddaway
Books, Powershell

I received my copies of the print version of PowerShell in Depth, second edition today.  Amazon shows it being available in a couple of days

Enjoy


November 11, 2014  1:00 PM

Transcripts

Richard Siddaway Richard Siddaway Profile: Richard Siddaway
Powershell

The ability to create transcripts of your PowerShell activity is great for keeping track of what you’ve done. They are also very useful when testing as you can keep a record of your results.

There’s one problem though – transcripts don’t work in the ISE.  Try this in PowerShell 4.0 or earlier:

Start-Transcript -Path C:\Temp\tran1.txt
Get-Process
Stop-Transcript

You’ll get an error like this:

Start-Transcript : This host does not support transcription.
At line:1 char:1
+ Start-Transcript -Path C:\Temp\tran1.txt
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : NotImplemented: (:) [Start-Transcript], PSNotSup
portedException
+ FullyQualifiedErrorId : NotSupported,Microsoft.PowerShell.Commands.Start
TranscriptCommand

Your script will run then you’ll get another error:

Stop-Transcript : This host does not support transcription.
At line:3 char:1
+ Stop-Transcript
+ ~~~~~~~~~~~~~~~
+ CategoryInfo          : NotImplemented: (:) [Stop-Transcript], PSNotSupp
ortedException
+ FullyQualifiedErrorId : NotSupported,Microsoft.PowerShell.Commands.StopT
ranscriptCommand

 

Now try the same in PowerShell 5.0 and you’ll get this:

Transcript started, output file is C:\Temp\tran1.txt

 

Your script will run and you’ll see this at the end:

Transcript stopped, output file is C:\Temp\tran1.txt

 

Your transcript file will will be created and contain the desired output.

Another useful addition to your PowerShell tool kit – transcripts now work in ISE


November 9, 2014  11:50 AM

PowerShell classes – - static classes

Richard Siddaway Richard Siddaway Profile: Richard Siddaway
Powershell

A static class is one that you don’t need to create  an instance of the object to use – the [math] class provides many examples e.g.

£> [math]::pi
3.14159265358979

 

You can create a class with static methods using PowerShell classes:

class stest {

static [int] doubleup ([int] $in){

return $in * 2
}

}

 

Its used like this:

£> [stest]::doubleup(2)
4

 

Where could you use this – possibly to resolve look up values for instance when decoding WMI integer values or if you have a series of calculations you’ll be using in several select statements


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: