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
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
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?
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 }
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
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"
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"
(the below also fails)
get-content c:\users.txt | ForEach-Object {Get-mailbox $_} | add-mailboxpermission -accessrights:"FullAccess"
ASKER
Anyone?
Sorry, I must have missed your reply.
Presumably it's throwing a nasty red error? If so, what does it say?
Chris
Presumably it's throwing a nasty red error? If so, what does it say?
Chris
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
Chris
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"
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"
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Perfect, thanks!
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