$SearchBase=“OU=UK,OU=Company,DC=Domain,DC=local” #Definition of OU containing groups I want to change
$searchScope=1 #Depends how deep you want to search : 0 – means Base,1 – means one level, 2 – means subtree
$CurrentDate = Get-Date
$CurrentDate = $CurrentDate.ToString(‘dd-MMM-yyyy_HH-mm-ss’)
$Dir=“C:\scripts"
$FileName=“Exchange”
$FileName=”$Dir$FileName-$CurrentDate.txt"
You didn’t close the } for the loop. Also, you’re referencing $1, not $i. You’d probably want $($i.samAccountName) if the goal is to insert the samAccountName. Otherwise, I’m not entirely sure what $1 is meant to refer to, as I don’t see the variable defined elsewhere in your code.
It looks like you’ve got some experience with batch files, and you’re running into some extremely common “gotchas” in PowerShell. Consider reviewing our “Gotchas” list (on the Resources menu), and consider working through “Learn PowerShell in a Month of Lunches.” There’s a certain amount of background information that would make this go a lot quicker for you.
$SearchBase="OU=UK,OU=OwenMumford,DC=woodstock,DC=local" #Definition of OU containing groups I want to change
$searchScope=1 #Depends how deep you want to search : 0 – means Base,1 – means one level, 2 – means subtree
$CurrentDate = Get-Date
$CurrentDate = $CurrentDate.ToString('dd-MMM-yyyy_HH-mm-ss')
$Dir="C:\scripts\"
$FileName="Exchange"
$FileName="$Dir$FileName-$CurrentDate.txt"
So, again: $i contains a user object. You need to reference $i.samAccountName. You should also explicitly use parameter names on Add-MailboxPermission and Add-ADPermission. Right now, you’re passing $i, not $i.samAccountName, and you’re passing it positionally, which is forcing the cmdlet to try and figure out what you mean. And it’s guessing wrong, which is why it isn’t working.