Copy ad user list to exchange - powershell

I have an ou with approximately 100 users in it. Our Exchange server has just been stood up, and we need to copy the AD user accounts to the exchange mailboxes. In essence, create a mailbox and email address for every ad user by copying their login.
I know this can be done through powershell. The command I have so far is:
Import-Module
$OUusers = Get-aduser -LDAPFilter '(name=*)' -SearchBase
{OU=myou,DC=MYDC,DC=COM}
foreach($username in $OUusers)
{
Enable-Mailbox - identity $username.samaccountname
}
But it errors out and says "missing argument" and points to the comma after my OU. What am I doing wrong? Server 2012R2, exch 2016.

Qualify the SearchBase OU DN with quotes, rather than with {}:
$OUusers = Get-aduser -LDAPFilter '(name=*)' -SearchBase 'OU=myou,DC=MYDC,DC=COM'

Related

Powershell deleting user in ADSI from outside LDAP domain

Our application allows the customer to authenticate to their own domain via Ldap but we keep a cached copy of those logons and accounts in "myserver" ADSI. Due to limitations with another part of our application I have a need to delete several thousand of those cached accounts from myserver ADSI
Keep in mind that this is NOT FOR MY DOMAIN but for the customer's domain. And no, I'm not trying to delete accounts in THEIR domain, just our cached copies in ADSI.
The following line of code does NOT throw an error but it also does NOT delete the acct (neither does piping it to "remove-aduser"
Get-ADObject -Server "myserver:3890" -SearchBase "CN=fqdn.customer.org,CN=Authentication Sources,O=Enterprise,CN=DifferentDirectory" -filter {name -eq "testuser"} | remove-adobject
Side note: I can query this tree of the default naming context just fine
Get-ADObject -Server "myserver:3890" -filter 'objectclass -like "*"' -SearchBase "CN=fqdn.customer.org,CN=Authentication Sources,O=Enterprise,CN=DifferentDirectory"
or I can use -ldapfilter switch to get pertinent info about a specific account.
It's a weird situation since I'm NOT dealing with accounts in my own domain. Many other variations on this theme throw errors referencing my own domain, partitions, etc. I've worked through all of those I think. The above examples SHOULD work in my opinion.
Final note: I CAN delete the user in the ADSIEDIT gui but as mentioned, they have given me a list of thousands of accts that need removing. There's gotta be a way?!
I figured it out (I’m feeding it a list of $users)
Get-ADObject -Server “myserver:3890” -SearchBase “CN=fqdn.customer.org,CN=Authentication Sources,O=Enterprise,CN=DifferentdirectoryDirectory” -Filter * | Where-Object {$_.name -eq “$user”} | Remove-ADObject -confirm:$false

How To search for user across all domains?

I am writing a Powershell script to get password expiry for specific set of users. These users belongs to different domains (across the world) in our org.
I use Get-Aduser to get the do this. The script works only for users where the script is run from. So if the script is run from let's say US, the Get-AdUser finds only users from US.
How can I search across all domains?
I tried this
Get-AdUser -Server <GlobalCatalog> [...]
This does have the same result without -Server
Have you tried using the following:
Get-ADUser -Filter {(yourFilterCondition)} -SearchBase "" -Server X.Y.Z.W:3268 -Properties desiredProperties | select-object...
Setting SearchBase to "" is the key thing here. And, also try to perform the query on 3268 port for reading values from all domains.

Remove full access permissions of all disabled users on shared mailboxes with exchange management shell

I’m looking for a powershell exchange script to remove Full access permissions of all disabled users on all shared mailboxes in a specific OU.
This is what I got so far
Remove-MailboxPermission -Identity Sharedmailbox -AccessRights Fullaccess -InheritanceType all -user DisabledUser -Confirm:$false | where {$_.UseraccountControl -like "*accountdisabled*"}
Its seems to work but I’m not sure about the last piece of het script if it will check for “accountdisabled”
Then I created a variable so it will check only one specific OU
$ou = Get-ADUser -SearchBase "OU=Functional Mailboxes,OU=Generalaccounts,DC=DOMAIN,DC=COM" -Filter * foreach ($user in $ou)
Remove-MailboxPermission -Identity "$ou" -AccessRights Fullaccess -InheritanceType all -Confirm:$false | where {$_.UseraccountControl -like "*accountdisabled*"}
The script is checking the right OU but I'm still looking for the last part where it will automatically remove full access permissions of the disabled users ONLY.
Can someone show me the way?
Instead of trying to screen for disabled users after removing the mailbox permissions (which is what your Remove-MailboxPermission ... | Where-Object ... appears to be intended to do - except that the way you wrote it, it's only checking for disabled state after removing the permissions), try selecting for the disabled accounts first, then passing only the disabled accounts to Remove-MailboxPermission:
Get-ADUser -SearchBase ... -filter {Enabled -eq $false} | Remove-Mailbox ...
(replacing ... with the appropriate SearchBase or parameters for Remove-Mailbox, using $_ for the identity of the ADUser whose mailbox permissions you're removing.)

how to remove all OU from AD via powershell

I am testing powershell with active directory,
have created list of OU in particular domain, but unable to remove all the OU from AD, Want to remove all OU except built in from AD
below is the script i am using, but it is giving access denied-
using administrator id
Get-ADOrganizationalUnit -Filter {Name -notlike "Domain Controllers"} -SearchBase (Get-ADDomain).Distinguishedname -SearchScope OneLevel | Remove-ADOrganizationalUnit -Recursive -Confirm:$false
OU's are protected by default, you have to remove that flag before deleting:
Set-ADObject -ProtectedFromAccidentalDeletion $false

Processing ForeignSecurityPrincipal

DomainA and DomainB trust each other. Some DomainB users are members of DomainA local domain groups. How can I get ForeignSecurityPrincipal in PowerShell and get list of its groups?
That was surprisingly simple:
Get-ADObject -Filter {ObjectClass -eq "foreignSecurityPrincipal"} -Properties msds-principalname,memberof
where "msds-principalname" is sAMAccountName, so I can search now through FSPs by sAMAccountName and get its groups.
You can get the list of foreign security principals in a domain by running Get-ADObject cmdlet with SearchBase set to CN=ForeignSecurityPrincipals,DC=domain,DC=com and LDAPFilter to something acceptable, like (|(objectCategory=user)(objectCategory=group)). Then, you can use this script to get its domain\username. Then you query that domain for DCs via Get-ADDomain and Get-ADDomainController, get the user object from there and run Get-ADPrincipalGroupMembership in your current domain against the retrieved user. An example (untested, as I have no env with many domains):
$ldf='(|(objectCategory=user)(objectCategory=group))'
$fspc=(get-addomain).ForeignSecurityPrincipalsContainer
$fsps = get-adobject -ldapfilter $ldf -searchbase $fspc
# got principals here
foreach ($fsp in $fsps) {
$fspsid=New-Object System.Security.Principal.SecurityIdentifier($fsp.cn)
($fspdomain, $fspsam) = ($securityPrincipalObject.Translate([System.Security.Principal.NTAccount]).value).Split("\")
# ^ this can throw exceptions if there's no remote user, take care
$fspdc=(get-addomaincontroller -domainname $fspdomain -discover)[0] # taking first one
$fspuser=get-aduser $fspsam -server $fspdc.hostname # use crossdomain DNS to resolve the DC
$fspgroups=get-adprincipalgroupmembership $fspuser # local query
$fspgroups # now do whatever you need with them and the $fspuser
}