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

Adding Powershell variables

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

A procedure for exporting installed hotfix details of remote computers using powershell
Windows 10 came with  a lot of built in applications, Some organisations leave them there, some will control them using GPO's. This Article is useful for those who do not want to have any applications in their image (example:me).
This tutorial will walk an individual through the steps necessary to enable the VMware\Hyper-V licensed feature of Backup Exec 2012. In addition, how to add a VMware server and configure a backup job. The first step is to acquire the necessary licen…
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…

828 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