I’ve tried to write a script in Powershell, I will briefly explain what the intention is: I retrieve the login name of a person (FirstnameName) from the AD, then I check whether the script can be run for this person (FirstnameName1), if the script is not allowed to run, I terminate the script. Otherwise the script continues automatically, as an example I started EXCEL.
Now, if I have to test for multiple people, I would have to add lines (below as an example “FirstnameName2”).
#Enviremont variabels
$AppData=(Get-Item env:appdata).value
$SigPath = '\Microsoft\Signatures'
$LocalSignaturePath = $AppData+$SigPath
$RemoteSignaturePathFull = $SigSource
#Getting info from AD
$UserName = $env:username
$Filter = "(&(objectCategory=User)(samAccountName=$UserName))"
$Searcher = New-Object System.DirectoryServices.DirectorySearcher
$Searcher.Filter = $Filter
$ADUserPath = $Searcher.FindOne()
$ADUser = $ADUserPath.GetDirectoryEntry()
$ADDisplayName = $ADUser.DisplayName
$ADEmailAddress = $ADUser.mail
$ADTitle = $ADUser.title
$ADDescription = $ADUser.description
$ADTelePhoneNumber = $ADUser.TelephoneNumber
$strFax = $ADUser.facsimileTelephoneNumber
$ADMobile = $ADUser.mobile
$ADStreetAddress = $ADUser.streetaddress
$ADCity = $ADUser.l
$ADCustomAttribute1 = $ADUser.extensionAttribute1
$ADModify = $ADUser.whenChanged
#Testing if it is FirstnameName1
$value = $UserName
#Testen op FirstnameName1
if ( $value -like 'FirstnameName1')
{
exit
}
#Testing if it is FirstnameName2
if ( $value -like 'FirstnameName2')
{
exit
}
#Open Template
$MSExcel = New-Object -comobject Excel.Application
$MSExcel.Visible = $True
Now I would like to place an CSV file in the folder (where the script is located) containing the exception names, I could change this file at any time depending on the needs, so the number of names are not known
Below is an example of this file:
Jan
Pete
Jeff
Sabrina
Wendy
Louis
Peter
The script should therefore read how many people are in the CSV file, then read all the names thru a loop and test whether any of the names appear in it. If not, it should then open Excell, otherwise the script must be endet
Does anyone know the best way to approach this in Powershell?
Please, before we proceed, go back, edit your question once again and fix the formatting of your code. This way it is hard to distinguish between code and prosa and the forum software often messes up some special characters in code.
When you post code, sample data, console output or error messages please format it as code using the preformatted text button ( </> ). Simply place your cursor on an empty line, click the button and paste your code.
( !! Sometimes the preformatted text button hides behind the settings gear symbol. )
As an aside: Please specify what file type EXACTLY you’re talking about. PowerShell can handle CSV files out of the box. For proper Excel files you will need additional modules or an installed Excel. But usually it is more complex and cumbersome to work with native Excel files. Therefor CSV files should be preferred.
Thanks for the welcoming. I’ve edit my posting and now the code is better visible.
As for the CSV file, I just want to create it in Excel and then choose the option “Save as CSV(MS-DOS)” when saving.
I have also adjusted this in my text.
Since it does not matter how you create it I’d recommend to simply refer to it as “CSV file”.
I’m still unsure what you’re actually trying to do? Do you want to impersonate other users and start Excel in their context? Is it really Excel what you want to start or is it just an example?
Whatfor do you need the infos from your AD?
Approach what exactly? Read a CSV file? Use the content of the CSV file for a particular purpose? What’s the end goal of your task?
For all users of our AD, a signature is created under their emails thru a script. However, some users now require a different signature.
The intention is that the script is terminated for these users (these are the users who are in the CSV file).
In order not to have to create lines in the script every time, it would be simpler to first read the names in the CSV file and then compare whether this is the current user, if YES the script should be stopped, if no, the signature should be created by the script.
If users need to be adjusted, I only must do this in the CSV file.
Ah … I see … so you basically want to check if a given element (for example a sAMAccountName) is contained in a given list of elements (for example a list of sAMAccountNames), right?
I have already adjusted the script a bit, just show the most important part here, I put the names in a $nameArray. The script is run and then Excel starts.
Google is your friend. For the vast majority of the cases you’re not the very first one with a given task. So it is recommended to search online for code snippets fitting your particular needs.
If you only have a bunch of names you don’t actually need a CSV file. A plain text file would serve this purpose as well. In this case Get-Content would read the file. If you want to use a CSV file you can use Import-Csv and use the header name for the dot notation.
If the header name is sAMAccountName it could look like this:
$ExceptionList = Import-Csv -Path 'C:\_Sample\ExceptionList.csv'
if($naam -in $ExceptionList.sAMAccountName){
'run very sophisticated and important code'
}