[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Adding Powershell variables

Posted on 2012-04-09
4
Medium Priority
?
504 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 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 6

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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

My attempt to use PowerShell and other great resources found online to simplify the deployment of Office 365 ProPlus client components to any workstation that needs it, regardless of existing Office components that may be needing attention.
A walk-through example of how to obtain and apply new DID phone numbers to your cloud PBX enabled users that are configured in Office 365. Whether you have 1, 10 or 100+ users in your tenant, it's quite easy to get them phone-enabled and making/rece…
This tutorial will walk an individual through configuring a drive on a Windows Server 2008 to perform shadow copies in order to quickly recover deleted files and folders. Click on Start and then select Computer to view the available drives on the se…
Screencast - Getting to Know the Pipeline

873 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