Solved

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

Posted on 2014-12-08
10
162 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 68

Expert Comment

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

Author Comment

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

Expert Comment

by:Qlemo
Comment Utility
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
 

Author Comment

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

and yes, the version is 4.
0
 
LVL 68

Assisted Solution

by:Qlemo
Qlemo earned 500 total points
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:johnnyjonathan
Comment Utility
Anyway to get the same script in vbs?
0
 
LVL 68

Expert Comment

by:Qlemo
Comment Utility
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
Comment Utility
batch is also viable, i simply can't relay on powershell since the environment  is too old
0
 
LVL 68

Accepted Solution

by:
Qlemo earned 500 total points
Comment Utility
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
Comment Utility
Thanks! great help and great solution!
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

I thought I'd write this up for anyone who has a request to create an anonymous whistle-blower-type submission form created using SharePoint 2010 (this would probably work the same for 2013). It's not 100% fool-proof but it's as close as you can get…
Create and license users in Office 365 in bulk based on a CSV file. A step-by-step guide with PowerShell script examples.
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 this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

763 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now