Articles

PowerShell articles, tutorials, and guides from community experts.

Richard Siddaway

Piping between functions

A question came up about piping between advanced functions. The input to the second function might be an array. To illustrate how this works imagine a function that gets disk information ““ or better still use this one.

`function

get-mydisk

{

[

CmdletBinding

(

)

]

param

(

[string]

$computername

=

“$env:COMPUTERNAME”

)

BEGIN

{

}

#begin

PROCESS

{

Get-WmiObject

-Class

Win32_LogicalDisk

-ComputerName

$computername

|

foreach

{

New-Object

-TypeName

PSObject

-Property

@{

Disk

=

$_

.

DeviceID

Free

=

$_

.

FreeSpace

Size

=

$_

.

Size

}

}

}

#process

END

Richard Siddaway

Starting virtual machines for WSUS

My test environment usually has a dozen or so machines at any one time. Some of these are short lived and used for a particular piece of testing ““ others are kept for years. I decided that I wanted to keep up to date on the patching of these virtual machines so installed WSUS on a Windows 2012 box.

One issue is that if a VM isn”™t started for 10 days WSUS starts complaining that it hasn"™t been contacted and if you run the WSUS clean up wizard the non-reporting servers may be removed. Checking the WSUS console for which machines haven"™t sync"™d recently is a chore.

Richard Siddaway

Account SIDs"“hopefully my last word

Ok the embarrassing moral of this story is that you shouldn’t answer questions in a hurry at the end of the evening. 5 minutes after shutting down I realised that there is a far, far simpler way to get the info. Win32_AccountSID is a WMI linking class. It links Win32_SystemAccount and Win32_SID classes.

Get-WmiObject -Class Win32_SystemAccount | select Caption, Domain, Name, SID, LocalAccount

gets you all you need

Richard Siddaway

Account SIDs revisited

I realised there is an easier way to get the data

`function

get-SID

{

param

(

[string]

$computername

=

$env:COMPUTERNAME

)

Get-WmiObject

-Class

Win32_AccountSID

-ComputerName

$computername

|

foreach

{

$exp

=

“[wmi]'”

$(

$_

.

Element

)

“'”

Invoke-Expression

-Command

$exp

|

select

Domain

,

Name

,

SID

,

LocalAccount

}

}

`Use the wmi type accelerator with the path from the Element and you can just select the data you want. As a bonus you can discover if the account is local or not

Richard Siddaway

Passing function names

A question asked about passing a function name into another function which then called the function. It sounds worse than it is. if you need to pass the name of a command and then call it try using invoke-expression

`function

ffour

{

Get-Random

}

function

fthree

{

Get-Date

}

function

ftwo

{

param

(

[string]

$fname

)

Invoke-Expression

$fname

}

“date”

ftwo

fthree

“random”

ftwo

ffour

`

Richard Siddaway

Account SIDs

A question on the forum asked about finding the accounts and SIDs on the local machine.

`function

get-SID

{

param

(

[string]

$computername

=

$env:COMPUTERNAME

)

Get-WmiObject

-Class

Win32_AccountSID

-ComputerName

$computername

|

foreach

{

$da

=

(

(

$_

.

Element

)

.

Split

(

“.”

)

[

1

]

)

.

Split

(

“,”

)

$sid

=

(

$_

.

Setting

-split

“=”

)

[

1

]

-replace

‘"’

,

''

$props

=

[ordered]

@{

Domain

=

(

$da

[

]

-split

“=”

)

[

1

]

-replace

‘"’

,

''

Account

=

(

$da

[

1

]

-split

“=”

)

[

1

]

-replace

‘"’

,

''

SID

=

$sid

}

New-Object

-TypeName

PSObject

-Property

$props

}

}

`Pass a computer name into the function ““ default is local machine.

Richard Siddaway

UK PowerShell group "“ 29 January 2013

`**When: Tuesday, Jan 29, 2013 7:30 PM (GMT)

Where: virtual

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

`Active Directory is one of the commonest automation targets for administrators. This session will covert the basics of automating your AD admin – scripts and the Microsoft cmdlets. The new features in PowerShell for Windows 2012 AD will also be covered

  Notes**`Richard Siddaway has invited you to attend an online meeting using Live Meeting.**[Join the meeting.](https://www.livemeeting.com/cc/usergroups/join?id=RCRWH3&role=attend&pw=5p7%24%7DS_%21h)****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. TroubleshootingUnable to join the meeting? Follow these steps:

Richard Siddaway

Updating Help on PowerShell v3

One of the new features in PowerShell v3 is the capability to update the help files. In fact you have to do this because PowerShell v3 doesn"™t ship with any help files. Since Windows 8 RTM"™d there have been a succession of new help files released.

I discovered one of my netbooks didn"™t have the latest version of the help files installed. So I needed to update them. This got me thinking that it would be better if the machine did this for me.