Author Posts

May 26, 2017 at 10:50 am

A lot of my powershell work consists of creating things in bulk using PowerShell and csv files. Is it possible to pass a switch parameter in the csv file?

Example: Resource Mailboxes

-Shared
-Room
-Equipment

foreach ($_ in (Import-csv C:\exchangetools\sharedmailbox.csv)) {
New-Mailbox -Name $_.Name -Alias $_.Alias -OrganizationalUnit $_.OrgUnit -FirstName $_.FirstName -LastName $_.LastName -Database $_.Database -Room
}

It would be great to be able to populate a csv file to create multiple resource mailboxes of mixed types (shared, room, equipment) and just run one simple script and pass the switch parameter via the file.

Yes I could create a script with a switch to read that value and then run the right script for that item, but I am all about the least amount of duplication that is possible.

May 26, 2017 at 12:13 pm

Why you do not show code you use ?

the shortest way is splatting. something like that

$csv = 'Name, Email, Type
room1, room1@corp.com, Shared
equip1, eq1@corp.com, Equipment' | ConvertFrom-Csv -Delimiter ','

$csv | Foreacn-Object {
  $params = @{
    name=$_.Name
    email=$_.email
  }
  $params.$type = $true
  new-mailbox @params
}

May 26, 2017 at 1:18 pm

I added the script. If you notice the identifier -room it's a switch parameter, not a value for a parameter and you don't show in your answer how the script is seeing that in the csv file to process the new-mailbox command properly.

May 26, 2017 at 6:38 pm

Sorry, before line 10 in my example should be $type = $_.Type

But I thought that you can find the error yourself 🙂

About switches: take into account than switches can be used as -switch and as -Switch:$true (or $false)

and finally, if you can't see how parameters goes into new-mailbox, you should `Get-Help about_Splatting`
https://blogs.technet.microsoft.com/heyscriptingguy/2010/10/18/use-splatting-to-simplify-your-powershell-scripts/

p.s. example it just example and may not match real new-mailbox parameters, but it correctly show the conception