Link to home
Start Free TrialLog in
Avatar of kam_uk
kam_uk

asked on

Powershell piping

Sometimes I'd like to get a list of users, mailboxes, objects etc from a text file and perform an action on them

Could someone confirm the below is the best way to do this? In my example, I'm performing an action on a mailbox.

How about:

Get-Content c:\mailboxes.txt | Get-Mailbox | set-mailbox -prohibitsendquota XX

Avatar of Chris Dent
Chris Dent
Flag of United Kingdom of Great Britain and Northern Ireland image

It depends what you have in your text file. But test it, try this first:

Get-Content c:\mailboxes.txt | Get-Mailbox

If that returns too much, try explicit looping (the above has an implicit loop):

Get-Content c:\mailboxes.txt | ForEach-Object { Get-Mailbox $_ }

In either case, you should find you can tack the last command on the end. It adds another implicit loop, but it will happily accept pipeline input from Get-Mailbox (which is what falls out of both examples).

$_ is a special variable in PowerShell, it's the pipeline variable, it represents the "current" value in our loop, the value it taken from the input pipeline.

Chris
Avatar of kam_uk
kam_uk

ASKER

Thanks for explaining..so what is the difference between these three:

1. Get-Content c:\mailboxes.txt | Get-Mailbox | set-mailbox -prohibitsendquota XX

2. Get-Content c:\mailboxes.txt | ForEach-Object { Get-Mailbox $_ } set-mailbox -prohibitsendquota XX

3. Get-Content c:\mailboxes.txt | ForEach-Object { Get-Mailbox $_ } | set-mailbox -prohibitsendquota XX

Which would be better to use performance wise, if I had say a couple of hundred entries in my txt file?
If you want to measure the performance of a command(s), use measure-command.
measure-command { Get-Content c:\mailboxes.txt | Get-Mailbox | set-mailbox -prohibitsendquota XX }

Open in new window


A couple of hundred is nothing, not really.

1 and 3 are effectively identical (in terms of what they actually do), the only difference is that 1 should perform slightly better as it handles the looping rather than us.

2 won't work :) Bad syntax.

Chris
Avatar of kam_uk

ASKER

Thanks Chris.

Interestingly, I am also trying to give a mailbox named "APP1" Full Access to a bunch of mailboxes. I tried a similar command to what you showed me but that failed?

get-content c:\users.txt | get-mailbox | add-mailboxpermission -accessrights:"FullAccess" -identity "APP1"

(the below also fails)

get-content c:\users.txt | ForEach-Object {Get-mailbox $_} | add-mailboxpermission -accessrights:"FullAccess" -identity "APP1"
Avatar of kam_uk

ASKER

Anyone?
Sorry, I must have missed your reply.

Presumably it's throwing a nasty red error? If so, what does it say?

Chris
Avatar of kam_uk

ASKER

Sorry, missed your reply back! On holiday at the moment, but will check it out when I get back! Thanks!
No worries, enjoy the rest of your holiday :)

Chris
Avatar of kam_uk

ASKER

The error I'm getting for both of them is:

Add-MailboxPermission : The input object cannot be bound to any parameters for the command either because the command does not take pipeline input or the input and its propertiesdo not match any of the parameters that take pipeline input.At line:1 char:83+ get-content c:\users.txt | ForEach-Object {Get-mailbox $_} | add-mailboxpermission  <<<< -accessrights:"FullAccess" -identity "APP1"
ASKER CERTIFIED SOLUTION
Avatar of Chris Dent
Chris Dent
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of kam_uk

ASKER

Perfect, thanks!