PowerShell for Windows Admins


August 10, 2014  2:34 AM

Euorpean PowerShell Summit 2014 registration details

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

The European PowerShell Summit, organised by PowerShell.org, will be in Amsterdam September 29 – October 1 2014 at the Park Hotel. Details at http://powershell.org/wp/community-events/summit/

 

The Summit will feature 3 days of PowerShell sessions from PowerShell team members, PowerShell MVPs and other PowerShell experts. It’s the in-person gathering place for PowerShell enthusiasts and PowerShell users. It’s a place to make new connections, learn new techniques, and offer something to your peers and colleagues.  If you can’t get your PowerShell questions answered at the PowerShell Summit you’ll never get an answer.

The Summit agenda is available to view at: http://eventmgr.azurewebsites.net/event/agenda/PSEU14

 

Registration is now open via http://eventmgr.azurewebsites.net/event/home/PSEU14.

 

August 6, 2014  2:18 PM

Invoke-Item tips

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

Invoke-Item is another cmdlet that you don’t see used that often but there is one place where its invaluable – opening files. If you give Invoke-Item the path to a file

Invoke-Item -Path .\procs.txt

The file will be opened with the default application associated with that extension. In this case Notepad.

If you use a PowerShell script file

Invoke-Item .\t1.ps1

It will be opened in Notepad for you to examine.  CSV files automatically open in Excel and other Office files are opened in the correct application.

Using the alias ii for Invoke-item makes it even easier.


August 5, 2014  12:42 PM

ServerManagerTasks module – - Get-SMServerFeature

Richard Siddaway Richard Siddaway Profile: Richard Siddaway
CIM, WMI

Finding the Windows features installed on a remote machine can be an interesting task. You could use Get-WindowsFeature but that gives you a “graphical” display:

£> Get-WindowsFeature | where DisplayName -like ‘*DNS*’

Display Name         Name             Install State
————         —-             ————-
[X] DNS Server       DNS              Installed
[X] DNS Server Tools RSAT-DNS-Server  Installed

Or you could use Get-SMServerFeature from the ServerManagerTasks module:

£> Get-SMServerFeature -BatchSize 1000  | where DisplayName -like ‘*DNS*’ | sort Displayname | ft -a Displayname, State, Type, ConfigurationStatus

Displayname      State Type ConfigurationStatus
———–      —– —- ——————-
DNS Server           1    0                   3
DNS Server Tools     1    2                   3

Which is CIM based and uses a new class: root/microsoft/windows/servermanager/MSFT_ServerFeature

Following the tradition firmly established since the introduction of WMI the data from this class is represented by integer values AND just for giggles its not documented. To be fair most of the WMI classes aren’t documented.

<rant>

We need documentation for these classes

</rant>

In an effort to work out which are the important values I compared the outputs from

Get-SMServerFeature -BatchSize 1000  | where DisplayName -like ‘*PowerShell*’ | sort Displayname | ft -a Displayname, State, Type, ConfigurationStatus

AND

Get-WindowsFeature | where DisplayName -like ‘*PowerShell*’ | sort Displayname

The important property from  Get-SMServerFeature  seems to be State which takes a 0 or 1. O corresponds to Available in Get-WindowsFeature and 1 corresponds to Installed.

You can use the trick I show in PowerShell and WMI (www.manning.com/siddaway2)

$state = DATA {ConvertFrom-StringData -StringData @’
0 = Available
1 = Installed
‘@}

Get-SMServerFeature -BatchSize 1000  |
where DisplayName -like ‘*PowerShell*’ |
sort Displayname |
select DisplayName, UniqueName,
@{N=’State’; E={$state["$($_.State)"]}

$state = DATA {ConvertFrom-StringData -StringData @’
0 = Available
1 = Installed
‘@}

Create a hash table for the integer values and their meaning and use that hash table in a calculated field to get the meaning from the integer value.

The output will look like this:

DisplayName : Windows PowerShell Web Access
UniqueName  : WindowsPowerShellWebAccess
State       : Available

Its now easy to compare the data between different machines.  Get-SMServerFeature has a CimSession parameter for working with remote machines


August 4, 2014  2:09 PM

European Summit – - update 6

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

As pointed out on PowerShell.org – http://powershell.org/wp/2014/08/03/powershell-summit-europe-2014-status/ – we’re around the 50% of capacity for registrations at the moment.

We need at least 45 attendees to be able to afford to put on a European Summit in 2015. Its not that we don’t want to do a European Summit – its we can’t afford to unless we get the support needed to ensure the event is financially viable

There are about 6 weeks until registration closes in mid-September.

Now is the time to start pestering your boss about going. There’s a great speaker line up plus some excellent evening activities. Amsterdam is a wonderful city that’s well worth a visit.

If you want to hear top line PowerShell speakers, including the PowerShell team, at an annual PowerShell summit then we need your support and I’d urge you to attend.

Registration is via the link at http://powershell.org/wp/community-events/summit/

It would be a sad position if we can’t get enough attendees from across Europe to make this work


July 24, 2014  2:22 PM

History cmdlets

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

There are 4 cmdlets for working with the history in a PowerShell session:

Add-History – adds an entry to the history
Clear-History – clears the history
Get-History -enables you see the current history
Invoke-History – enables you to run a line in your history

Get-History is the one I use the most.  You can use the UP arrow key to cycle back to an old command or use Invoke-History.

I’d only use Clear-History if I had a long running session and I was changing what I was working on.

Add-History is the odd one. I’ve never used it but it could be useful if you have a command you run often and you want to push it into the history when you start a session.

Be aware of  a  variable that controls history:

$MaximumHistoryCount  defaults to  4096 and is the maximum number of commands saved in the command history.

You can increase this up to 32768 if you desire but unless you put the command in your profile your next session will drop back to the default.


July 23, 2014  2:10 PM

Its mandatory Jim but not as we know it

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

A uestion was asked on the forum about whether a parameter could be made mandatory sometimes and nor other – in particular if a parameter could be made mandatory if another parameter was used.

My initial thought was that it couldn’t but a little experimentation with parameter sets produced this:

function test {

[CmdletBinding()]
param (
[Parameter(Mandatory)]
[string]$Param1,

[Parameter(Mandatory)]
[string]$Param2,

[Parameter(ParameterSetName = 'Set1')]
[switch]$Param3,

[Parameter(ParameterSetName = 'Set1',Mandatory)]
[Parameter(ParameterSetName = 'Set2')]
[string]$Param4,

[Parameter(ParameterSetName = 'Set2')]
[switch]$param5
)

“parameters accepted”

}

The function doesn’t do anything – its just to demonstrate the parameters

So this works:

£> test -Param1 x -Param2 y
parameters accepted

These work

£> test -Param1 x -Param2 y -param5
parameters accepted

£> test -Param1 x -Param2 y -param5 -Param4 z
parameters accepted

So you see that param4 when used in parameter set set2 is not mandatory

However,

£> test -Param1 x -Param2 y -param3
cmdlet test at command pipeline position 1
Supply values for the following parameters:
Param4: a
parameters accepted

So when you use parameter 3 you’re in parameter set set1 and param4 becomes mandatory.

Bit messy with the parameter decorators but it gets the job done


July 22, 2014  3:09 PM

msmvps.com is back

Richard Siddaway Richard Siddaway Profile: Richard Siddaway
Security

My blog on msmvps.com is back at http://blogs.msmvps.com/richardsiddaway/


July 22, 2014  6:08 AM

July 22 2014–deal of the day

Richard Siddaway Richard Siddaway Profile: Richard Siddaway
Books

Half off my book PowerShell and WMI. Use code dotd072214au at www.manning.com/siddaway2/

Also

Half off PowerShell in Depth, Second Edition. Use code dotd072214au at www.manning.com/jones6/

 


July 21, 2014  1:45 PM

Bad practice – - creating objects

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

Another in my occassional series on bad practices I’ve seen and recommend you avoid.  This time I want to look at creating objects. I recently saw some code that looked a bit like this:

$os = Get-CimInstance -ClassName Win32_OperatingSystem
$comp = Get-CimInstance -ClassName Win32_ComputerSystem
$bios =  Get-CimInstance -ClassName Win32_Bios

$obj = New-Object -TypeName PSObject
$obj | Add-Member -MemberType NoteProperty -Name OperatingSystem -Value $os.Caption
$obj | Add-Member -MemberType NoteProperty -Name Manufacturer -Value $comp.Manufacturer
$obj | Add-Member -MemberType NoteProperty -Name Model -Value $comp.Model
$obj | Add-Member -MemberType NoteProperty -Name Bootup -Value $os.LastBootUpTime
$obj | Add-Member -MemberType NoteProperty -Name BiosType -Value $bios.BIOSVersion
$obj

This involves a lot more typing and effort than is required.

A much, much  simpler way is available:

$os = Get-CimInstance -ClassName Win32_OperatingSystem
$comp = Get-CimInstance -ClassName Win32_ComputerSystem
$bios =  Get-CimInstance -ClassName Win32_Bios

$obj = New-Object -TypeName PSObject -Property @{
OperatingSystem = $os.Caption
Manufacturer = $comp.Manufacturer
Model = $comp.Model
Bootup = $os.LastBootUpTime
BiosType = $bios.BIOSVersion
}
$obj

The Property parameter takes a hash table of property names and values and populates the object. Much less typing and more obvious, to my mind.

Some people complain that use the Property parameter means that you lose the order of the properties.

Who cares – its an object. Access the properties as you need them.

If for some reason you need to be able to dictate the order of the properties then use an ordered hash table:

$os = Get-CimInstance -ClassName Win32_OperatingSystem
$comp = Get-CimInstance -ClassName Win32_ComputerSystem
$bios =  Get-CimInstance -ClassName Win32_Bios

$props = [ordered]@{
OperatingSystem = $os.Caption
Manufacturer = $comp.Manufacturer
Model = $comp.Model
Bootup = $os.LastBootUpTime
BiosType = $bios.BIOSVersion
}

$obj = New-Object -TypeName PSObject -Property $props
$obj

But however you use the hash table use it in preference to Add-Member for these scenarios.

Does Add-Member have a place. Yes. Use when you want to add one or two properties to an existing object.


July 20, 2014  1:45 PM

Docker and DSC for Linux

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

Docker is a way to “virtualise” applications on Linux machines. With DSC for Linux you can manage Docker instances

http://blogs.technet.com/b/privatecloud/archive/2014/07/17/configuring-docker-on-azure-with-powershell-dsc.aspx


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: