Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

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

Posted on 2014-12-08
10
165 Views
Last Modified: 2015-01-17
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?
0
Comment
Question by:johnnyjonathan
  • 5
  • 5
10 Comments
 
LVL 69

Expert Comment

by:Qlemo
ID: 40486505
It's an Excel file (XLS*) or CSV we are talking about? And which PowerShell release?
0
 

Author Comment

by:johnnyjonathan
ID: 40489034
csv or xls could be anything, doesn't matter to me.
And how can i check which powershell?
0
 
LVL 69

Expert Comment

by:Qlemo
ID: 40489232
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
Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 

Author Comment

by:johnnyjonathan
ID: 40497066
Thanks for the explanation.
In that case, yes let's go with CSV.

and yes, the version is 4.
0
 
LVL 69

Assisted Solution

by:Qlemo
Qlemo earned 500 total points
ID: 40498082
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
 

Author Comment

by:johnnyjonathan
ID: 40544859
Anyway to get the same script in vbs?
0
 
LVL 69

Expert Comment

by:Qlemo
ID: 40544975
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
 

Author Comment

by:johnnyjonathan
ID: 40555073
batch is also viable, i simply can't relay on powershell since the environment  is too old
0
 
LVL 69

Accepted Solution

by:
Qlemo earned 500 total points
ID: 40555135
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
 

Author Closing Comment

by:johnnyjonathan
ID: 40555216
Thanks! great help and great solution!
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is a PowerShell web interface I use to manage some task as a network administrator. Clicking an action button on the left frame will display a form in the middle frame to input some data in textboxes, process this data in PowerShell and display…
The following article is intended as a guide to using PowerShell as a more versatile and reliable form of application detection in SCCM.
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question