Powershell Script: Disable OWA access using a list of users

Hello,

I'm pretty new at Powershell and have been tasked with forming a script that feeds a list of users and disable OWA access for those users
this is what i'm thinking. will this do the trick and what do you experts propose?

$Users = Import-CSV c:\temp\users.csv
 
# For each user set the mailbox for user and disable OWA
foreach ($usr in $Users) {
$sAMAccountName = $usr.samaccountname
Write-host -NoNewLine "Modifying $sAMAccountName / Mailbox Identity Properties...."
Set-CASMailbox $SamAccountName -OWAEnabled $False
Write-host "Done!"
}
LVL 16
Raymond PengSystems EngineerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Anthony CarterCommented:
I don't think you send SamAccountName to Set-CASMailbox.  If you want to do that you need:

Set-CASMailbox -SamAccountName $SamAccountName

Otherwise just send:

Set-CASMailbox user@domain.com

You can simplify everything by doing the following (assuming you have a header line in your CSV and samaccountname is one of them):

Import-CSV c:\temp\users.csv | Foreach-Object{
  Write-host -NoNewLine "Modifying $_.samaccountname / Mailbox Identity Properties...."
  Set-CASMailbox -SamAccountName $_.samaccountname
}

If you don't, but know your columns content and your SAMAccountName is in column 3:

Import-CSV c:\temp\users.csv -Header VAR1,VAR2,VAR3,VAR4 | Foreach-Object{
  Write-host -NoNewLine "Modifying $_.VAR3 / Mailbox Identity Properties...."
  Set-CASMailbox -SamAccountName $_.VAR3
}
0
Anthony CarterCommented:
If you want to test prior to actually doing the change:

Import-CSV c:\temp\users.csv | Foreach-Object{
  Write-host -NoNewLine "Modifying $_.samaccountname / Mailbox Identity Properties...."
 }

Which will print you out all the samaccountnames from the CSV
0
Anthony CarterCommented:
Sorry, typo, the script would be:

Import-CSV c:\temp\users.csv | Foreach-Object{
  Write-host -NoNewLine "Modifying $_.samaccountname / Mailbox Identity Properties...."
  Set-CASMailbox -SamAccountName $_.samaccountname -OWAEnabled $false
}

I Forgot to add the -OWAEnabled $false on the Set-CASMailbox line.

or

Import-CSV c:\temp\users.csv -Header VAR1,VAR2,VAR3,VAR4 | Foreach-Object{
  Write-host -NoNewLine "Modifying $_.VAR3 / Mailbox Identity Properties...."
  Set-CASMailbox -SamAccountName $_.VAR3 -OWAEnabled $false
}
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Marwan OsmanCommented:
try that, and if it is not working inform me and I will send a script that I use.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.