Clicky

I was hoping someone could explain or point me in the right direction for getting more insight on what is going on in this script/command.  Specifically the line where $owner variable is declared.  What is this construct '{0}\{1}' and what is the -f parameter doing?  I can see that this somehow is creating the variable from the first element {0} adding the backslash, then adding the second element {1}  , however I've read through several powershell books, and numerous posts about arrays, variables, & hash tables, but can't seem to find some deeper explanation of doing something like this.  Anyhow, here's the whole script.  I found this trying to find a one-liner that would return the process owner of all running process:

Get-WmiObject Win32_Process |
ForEach-Object {
$ownerraw = $_.GetOwner();
$owner = '{0}\{1}' -f $ownerraw.domain, $ownerraw.user; $_ |
Add-Member NoteProperty Owner $owner -PassThru } |
Select-Object Name, Owner

asked 12/15/2011 04:51

gedcgene's gravatar image

gedcgene ♦♦


4 Answers:
Basically the {0} and {1} are placeholders simply put.  You can do something like this:

"Domain:{0} User:{1}" -f $owner.domain, $owner.user

Does that clear it up for you?

What you're holding places for is the variables your listing at the end.  If you have {2} in there, you're going to have to give it a 3rd variable to display since it's zero based.

HTH,

Dale Harris
link

answered

DaleHarris's gravatar image

DaleHarris

As DaleHarris pointed out, the -f, when placed between a quoted string on the left and a set of objects on the right, will format the objects.  There are a variety of ways you can manipulate basic strings with the .NET format specifiers.  Here is a set of examples:

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

You can have as few as 1 or as many as you like in whatever order you like.  For instance, you could put "your name" like this:
1:
2:
"{0} {1}" -f "your","name"
"{8}{6}{4}{2}{0}" -f "y","o","u","r"," ","n","a","m","e"

Just remember, it's a zero-based array.  So, your first object, on the right hand side, will be associated with the {0}th place holder.
link

answered 2011-12-15 at 12:54:40

wls3's gravatar image

wls3

Thanks, in regards to the syntax, what is the last $_ pipe variable doing in the line:

$owner = '{0}\{1}' -f $ownerraw.domain, $ownerraw.user; $_

Is that going back to the array returned by the Get-WmiObject Win32_Process in order to pipe each element to the Add-Member statement?
link

answered 2011-12-15 at 13:48:23

gedcgene's gravatar image

gedcgene

There are a few things going on here:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
# This starts the iteration process
ForEach-Object { 

# This stores the current pipelined object in the $ownerraw variable
$ownerraw = $_.GetOwner(); 

# This essentially reformats the data in the $ownerraw variable
$owner = '{0}{1}' -f $ownerraw.domain, $ownerraw.user; 

# This takes the current object and adds the new $owner object (after it has been modified) to the current object
$_ | Add-Member NoteProperty Owner $owner -PassThru 
} 

# Now that the manipulation on all objects is complete (and the owner objects has been added as a NoteProperty this selects the Name and (newly added) Owner properties from the collection returned by the iteration
| Select-Object Name, Owner


The misleading part is that the pipeline was at the end of a line.  Maybe my rewriting has help organize it a little more clearly.
link

answered 2011-12-15 at 14:35:49

wls3's gravatar image

wls3

Your answer
[hide preview]

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Tags:

×25

Asked: 12/15/2011 04:51

Seen: 186 times

Last updated: 12/15/2011 07:10

Categories