Need Help modifying a powershell script to check values from excel file

Hello,
I'm looking for a powershell script that would run on a login script, export the current username logged on, check if that username is listed in an excel\csv file on a share.
If it doesn't exists, then the script needs to run a command and add the name in the sheet.
If it does exists then do nothing and exit.

Unfortunately it has to be in Powershell and i'm not to keen on it, any advise please?
johnnyjonathanAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
QlemoConnect With a Mentor Batchelor, Developer and EE Topic AdvisorCommented:
Batch file:
set usrfile=\\server\share\scripts\Users.txt
find /i "%UserName%" <%usrfile% >nul || (
  REM Do Something 
  echo %UserName%>> %usrfile%
)

Open in new window

0
 
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
It's an Excel file (XLS*) or CSV we are talking about? And which PowerShell release?
0
 
johnnyjonathanAuthor Commented:
csv or xls could be anything, doesn't matter to me.
And how can i check which powershell?
0
NEW Internet Security Report Now Available!

WatchGuard’s Threat Lab is a group of dedicated threat researchers committed to helping you stay ahead of the bad guys by providing in-depth analysis of the top security threats to your network.  Check out this quarters report on the threats that shook the industry in Q4 2017.

 
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
Check with:   powershell "$PsVersionTable"  from outside of PowerShell, or in PowerShell itself just type the var  $PsVersionTable.
If PSVersion is not 4.0, and you are on W7/W2008R2 or above, I strongly advice to upgrade to 4.0.

Automating Excel is slower and more complicated than parsing and writing a CSV file. PS 4.0 also allows to append to a CSV (prior you had to rewrite the CSV completely), hence my question for the version.
0
 
johnnyjonathanAuthor Commented:
Thanks for the explanation.
In that case, yes let's go with CSV.

and yes, the version is 4.
0
 
QlemoConnect With a Mentor Batchelor, Developer and EE Topic AdvisorCommented:
I'm assuming we not even need a CSV, but a simple text file will do.
$usrfile = '\\server\share\scripts\Users.txt'
if ( (get-content $usrfile) -notcontains $env:UserName )
{
  # Do something
  echo $env:UserName >> $usrfile
}

Open in new window

0
 
johnnyjonathanAuthor Commented:
Anyway to get the same script in vbs?
0
 
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
This question has an clear, enforced restriction to be about PowerShell:
Unfortunately it has to be in Powershell
If anything, I would use a cmd.exe batch. VBS requires some coding overhead, without gaining anything.
0
 
johnnyjonathanAuthor Commented:
batch is also viable, i simply can't relay on powershell since the environment  is too old
0
 
johnnyjonathanAuthor Commented:
Thanks! great help and great solution!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.