Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 935
  • Last Modified:

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

0
kam_uk
Asked:
kam_uk
  • 6
  • 5
1 Solution
 
Chris DentPowerShell DeveloperCommented:
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
0
 
kam_ukAuthor Commented:
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?
0
 
LearnctxEngineerCommented:
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

0
Evaluating UTMs? Here's what you need to know!

Evaluating a UTM appliance and vendor can prove to be an overwhelming exercise.  How can you make sure that you're getting the security that your organization needs without breaking the bank? Check out our UTM Buyer's Guide for more information on what you should be looking for!

 
Chris DentPowerShell DeveloperCommented:

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
0
 
kam_ukAuthor Commented:
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"
0
 
kam_ukAuthor Commented:
Anyone?
0
 
Chris DentPowerShell DeveloperCommented:
Sorry, I must have missed your reply.

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

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

Chris
0
 
kam_ukAuthor Commented:
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"
0
 
Chris DentPowerShell DeveloperCommented:
Identity is the mailbox you're changing, so you probably want this:
get-content c:\users.txt | ForEach-Object {Get-mailbox $_} | add-mailboxpermission -accessrights:"FullAccess" -User "APP1"

Open in new window

Or this:
get-content c:\users.txt | ForEach-Object { 
  Get-mailbox $_ | Add-MailboxPermission -accessrights:"FullAccess" -User "APP1"
}

Open in new window

Chris
0
 
kam_ukAuthor Commented:
Perfect, thanks!
0

Featured Post

Become an Android App Developer

Ready to kick start your career in 2018? Learn how to build an Android app in January’s Course of the Month and open the door to new opportunities.

  • 6
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now