Execute command if user is found in OU by using -Searchbase

189 Views Asked by At

This is kind of specific to this Document Management System(DMS) application we use called Worksite / Filesite. Basically, we have 3 offices and 3 servers and if people login from a particular site's OU, no matter the physical presence, they should be presented with that site's connection after running the below command. I'm close to achieve the result, but it's not working for multiple reasons and in different situations.

This code runs on a Windows 10 machine

Start-Process 'C:\Program Files (x86)\Microsoft Office\Office16\OUTLOOK.EXE'
Start-Process 'C:\Program Files (x86)\Internet Explorer\iexplore.exe'
$Office1 = & 'C:\Program Files (x86)\Interwoven\WorkSite\addiman.exe OFFICE1-SRV'
$Office2 = & 'C:\Program Files (x86)\Interwoven\WorkSite\addiman.exe OFFICE2-SRV'
$Office3 = & 'C:\Program Files (x86)\Interwoven\WorkSite\addiman.exe OFFICE3-SRV'
$loggedinuser = Get-ADUser -Identity $env:UserName
if (Select-String -Pattern "Office1-User" -InputObject $loggedinuser) { $Office1 }
if (Select-String -Pattern "Office2-User" -InputObject $loggedinuser) { $Office2 }
if (Select-String -Pattern "Office3-User" -InputObject $loggedinuser) { $Office3 }

What happens is, the add-in for all 3 sites are added for Outlook, which I don't want. If I use the same code on remote terminal services machine, it executes just for $Office1 and stops even if the user is from either $Office2 or $Office3

My questions are :

  1. How can I use -Searchbase to execute the right command for the right office? "If the logged on user is $env:username, searchbase in OU and then execute $thisexe"
  2. My if statements are wrong, but I'm not getting an effective method to use the Switch function EDIT : Just to see how this is working, I ran the script without any conditions and it is simply executing each line sequentially and now, I'm even more confused.
1

There are 1 best solutions below

5
On

Maybe you can use DistinguishedName:

$loggedinuser = get-aduser -Identity $env:UserName -Properties DistinguishedName 
if ($loggedinuser.DistinguishedName  -like "*Office1-User*") {

$Office1 = & 'C:\Program Files (x86)\Interwoven\WorkSite\addiman.exe OFFICE1-SRV'


}
etc