Solved

Adding Powershell variables

Posted on 2012-04-09
4
495 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
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

Free Gift Card with Acronis Backup Purchase!

Backup any data in any location: local and remote systems, physical and virtual servers, private and public clouds, Macs and PCs, tablets and mobile devices, & more! For limited time only, buy any Acronis backup products and get a FREE Amazon/Best Buy gift card worth up to $200!

Join & Write a Comment

Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
New Windows 7 Installations take days for Windows-Updates to show up and install. This can easily be fixed. I have finally decided to write an article because this seems to get asked several times a day lately. This Article and the Links apply to…
This tutorial will give a short introduction and overview of Backup Exec 2012 and how to navigate and perform basic functions. Click on the Backup Exec button in the upper left corner. From here, are global settings for the application such as conne…
To efficiently enable the rotation of USB drives for backups, storage pools need to be created. This way no matter which USB drive is installed, the backups will successfully write without any administrative intervention. Multiple USB devices need t…

708 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

11 Experts available now in Live!

Get 1:1 Help Now