Read an Excel list thru a script in Powershell

Hello Folks

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?

Thank you in advance.

STYLUS3530

STYLUS3530,
Welcome to the forum. :wave:t3:

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.

Thanks in advance

How to format code in PowerShell.org 1 <---- Click :point_up_2:t4: :wink:

( !! Sometimes the preformatted text button hides behind the settings gear symbol. :wink: )

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.

Hello Olaf

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.

Greetings

STYLUS3530

Looks great now. :+1:t3:

Since it does not matter how you create it I’d recommend to simply refer to it as “CSV file”. :wink:

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? :wink:

Nothing easier than that …

$GivenElement = 'STYLUS3530'

$GivenElementList = @(
    'STYLUS3530'
    'STYLUS3830'
    'STYLUS3860'
    'STYLUS3231'
    'STULYS3530'
    'SLYTUS3530'
)

No you can check if the given element is in the given element list …

If ($GivenElement -in $GivenElementList){
    'Cool'
}
else {
    'Uncool'
}

… or if the given element list contains the given element …

if ($GivenElementList -contains $GivenElement) {
    'Cool'
}
else {
    'Uncool'
}

And of course you can turn this the other way around and check if the given element is not in the list with -notin or -notcontains

Is it that what you’re looking for?

@Olaf
Indeed, that is what I’m searching about.

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.

$value = ""

$naamArray = "Name1","Name2","Name3","Name4"
foreach ($naam in $letterArray)
{
  if ( $value -like $naam)
 {
    exit
 }
}

#Open Template
$MSExcel = New-Object -comobject Excel.Application 
$MSExcel.Visible = $True

If I for example change $value to Name2, the script is terminated

So now I have to try to fill $nameArray with the names from the CSV file, but I don’t know how I get these names in there.

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'
}
1 Like

@Olaf

Many thanks for the help.
At this moment I will use the solution without the CSV - File, but I will try to integrade it at a later moment.

Is there perhaps a good book that is recommended for a beginner in Powershell?

I don’t know if you want a book in Dutch, French, German or English.

For German beginners the books from Tobias Weltner are usually a great choice.

or

or

For English books I always hear the recommendation for the books from Don Jones and Jeff Hicks

But there are even a lot of online sources available:

1 Like

@Olaf

First of all, thank you very much for the various links to information regarding Powershell

My mother language is Dutch, but I also speak German, both speech and grammar.

It is long time ago but since I knew him I started with a book from Tobias Weltner …

“Scripting mit Windows PowerShell 2.0 - Der Einsteiger-Workshop”

… and I am convinced that his books are excellent written and well suited for beginners. But I’d recommend getting a current version. :wink:

1 Like