Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Adding Powershell variables

Posted on 2012-04-09
4
Medium Priority
?
503 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 2000 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:Daryl Bamforth
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

Q2 2017 - Latest Malware & Internet Attacks

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 our latest Quarterly Internet Security Report!

Question has a verified solution.

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

For anyone that has accidentally used newSID with Server 2008 R2 (like I did) and hasn't been able to get the server running again because you were unlucky (as I was) and had no backups - I was able to get things working by doing a Registry Hive rec…
Resolving an irritating Remote Desktop connection that stops your saved credentials from being used.
This tutorial will give a an overview on how to deploy remote agents in Backup Exec 2012 to new servers. Click on the Backup Exec button in the upper left corner. From here, are global settings for the application such as connecting to a remote Back…
This tutorial will walk an individual through setting the global and backup job media overwrite and protection periods in Backup Exec 2012. Log onto the Backup Exec Central Administration Server. Examine the services. If all or most of them are stop…

636 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