Solved

Adding Powershell variables

Posted on 2012-04-09
4
501 Views
Last Modified: 2012-04-10
Hi,

I'm putting together a Powershell script to collect event log data from servers. It works fine when I use -ComputerName . or I actually input the name of the server.

However, I have a CSV with the name of each server and I'd like to have it go through each server name in the CSV individually and add the result to a text file I have it create so I can look at the event log info.

When I use $ComputerName = import-CSV C\Temp\ServerEventLog.csv it adds the entire list of servers to the variable. I need to have it take one name, collect the event logs and then go to the next server name in the list, so that the variable $ComputerName changes as it finishes collecting the eventlog and moving to the next server in the list?

Is this possible? Seems like it should be but I'm at a loss. I've attached as a text file the very simple script I'm working on.

Thanks,
Jake
0
Comment
Question by:JakeBanzai
[X]
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
4 Comments
 
LVL 27

Expert Comment

by:KenMcF
ID: 37823922
You can do this

Foreach ($ComputerName  in (import-CSV C:\Temp\ServerEventLog.csv)){


YOUR CODE HERE

}
0
 
LVL 6

Accepted Solution

by:
slidingfox earned 500 total points
ID: 37824792
Does your CSV only contain computernames? or does it contain other information aswell?

If the file only contains computernames, then you could put them in a text file with one hostname per line. Then use

$computername = Get-Content C:\computers.txt

The problems with using CSVs is that it creates an object rather than a list of computernames.

If you have to use a CSV, i'd make sure the top row of your CSV had a heading (such as computername), then I'd use

$computername = Import-CSV C:\computers.csv | Select -Expand ComputerName

This will expand out the computername column, and your variable will only contain a list of computer names and nothing else.

once you've got your variable into your script, you'll have to loop through each item in the variable with something like this:

Foreach ($computer in $computername) {

      <do something clever>

}
0
 
LVL 2

Expert Comment

by:un0ri
ID: 37827152
As slidingfox has said you need to either ensure your CSV already has headers in, or additionally you can create the headers on the import (have headers makes it much more readable for if you need to revisit it).

If your CSV doesn't already have headers you can do something like this to add them

$sHeader = "Computername","Computerdescription","someotherfield","heresanotherone"
$Computername = Import-Csv C:\Temp\ServerEventLog.csv -header $sHeader | Select -expand computername

Then carry on with the foreach loop above
0
 

Author Comment

by:JakeBanzai
ID: 37827164
I will try all of these - however, slidingfox  is correct, I should just be using a txt file as the only information in the file is the list of servers and nothing else. I do have a header on the csv but the txt option will work better.

I will try these solutions as soon as I get to work. Thanks for all the input!
0

Featured Post

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

Question has a verified solution.

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

A brief introduction to what I consider to be the best editor for PowerShell.
I was prompted to write this article after the recent World-Wide Ransomware outbreak. For years now, System Administrators around the world have used the excuse of "Waiting a Bit" before applying Security Patch Updates. This type of reasoning to me …
This tutorial will walk an individual through locating and launching the BEUtility application to properly change the service account username and\or password in situation where it may be necessary or where the password has been inadvertently change…
This tutorial will show how to configure a new Backup Exec 2012 server and move an existing database to that server with the use of the BEUtility. Install Backup Exec 2012 on the new server and apply all of the latest hotfixes and service packs. The…

726 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