Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to import csv.. 1

Status
Not open for further replies.

DarkOne72

Technical User
Jun 14, 2002
210
US
Hey guys...

I was wondering if someone could help me with a script I have. What I am trying to do is import a csv file that has 2 columns (Server and Process), each server has different process that I want to check. So I want the script to import the file and read each line, connect to that server and check and see if the processes are running for that particular server then continue with the next one (which will be a different server and different processes).With this i want to capture the percentage of each process and how much mem it is taking (like you see in task manager) I can't seem to get it to work, can someone help me?

Code:
[COLOR=blue]
$path = "C:\CMonitor.csv"
$csv = Import-csv -path $path

$ProcBefore=Get-Process -computername $line.Server -Name $line.Processes

$usage="{0:P2}" -f (1-(($p.workingset)/($z[0].workingset)))
# Take a snapshot of running processes
Write-Host "Sleeping..."
sleep -Seconds 10

foreach ($p in $ProcBefore) {

$procAfter | where {$_.id -eq $p.id} -OutVariable z | Out-Null
 
if (($z[0].workingset)-($p.workingset) -gt 0 ) {
  $usage="{0:P2}" -f (1-(($p.workingset)/($z[0].workingset)))

# defined a variable for later use with write-host
  $color="RED"
}
Else 
{
 # there was no difference so set default variables
  $usage="0 %"
  $color="WHITE"
}
foreach($line in $csv)
{
$ProcBefore #Get-Process -computername $line.Server -Name $line.Processes
}
# write the results to the screen
Write-Host -ForegroundColor $color ($p.id) ($p.name)
Write-Host -ForegroundColor $color BEFORE: ($p.workingset) AFTER: ($z[0].workingset)
Write-Host -ForegroundColor $color Usage: $usage
}
[/color]

Thanks in advance..
 
IMHO, when you have a CSV file like this

processes.csv
Code:
SERVER;PROCESS
server1;proc1;
server2;proc2;
server3;proc3;
then do something like this to process it line by line:

processes.ps1
Code:
function process_line ($line) {
  $srv = $line.SERVER
  $prc = $line.PROCESS
  echo "Look at the process '$srv' running on server '$prc' .."
  echo $my_output_line
}

# -- main --
$csv_path = "processes.csv"
$csv = Import-csv -path $csv_path -delimiter ';'

foreach($line in $csv){
  process_line($line)
}

which gives the result:
Code:
c:\Work>powershell -ExecutionPolicy RemoteSigned .\processes.ps1
Look at the process 'server1' running on server 'proc1' ..
Look at the process 'server2' running on server 'proc2' ..
Look at the process 'server3' running on server 'proc3' ..
 
Correct:
Code:
..
  echo "Look at the process '$prc' running on server '$srv' .."
..
...
Code:
c:\Work>powershell -ExecutionPolicy RemoteSigned .\processes.ps1
Look at the process 'proc1' running on server 'server1' ..
Look at the process 'proc2' running on server 'server2' ..
Look at the process 'proc3' running on server 'server3' ..
 
Thank you for the reply...I have got it to work like you have in the above but how would I get the cpu, memory usage in percent based on the script I have above? I cant get that to work while adding what you have.
Sorry I am new to PS
 
Hi DarkOne72,
...but how would I get the cpu, memory usage in percent based on the script I have above? I cant get that to work while adding what you have.
Sorry I am new to PS
Sorry, but I'm new to PS too. Try to google for it, or maybe somebody from this forum helps you.
 
I think, that you should use the commandlet get-process:

Now I have following example:
processes.csv
Code:
SERVER;PROCESS
miklos7;explorer
1_mikl1071_01;nlnotes
processes.ps1
Code:
function process_line ($line) {
  $srv = $line.SERVER
  $prc = $line.PROCESS
  # get-process from line 
  $my_proc=get-process -Name $prc -ComputerName $srv
  # extract values
  $my_PID = $my_proc.ID
  $my_CPU = $my_proc.CPU
  if ($my_CPU -eq $nul) {$my_CPU = "'? (not a value)'"}
  # print 
  echo "at server '$srv' the process '$prc' with PID = $my_PID"
  echo "    has CPU usage of $my_CPU" 
}

# -- main --
$csv_path = "processes.csv"
$csv = Import-csv -path $csv_path -delimiter ';'

foreach($line in $csv){
  process_line($line)
}
when I run my script I get this output
Code:
U:\PowerShell>powershell -ExecutionPolicy RemoteSigned .\processes.ps1
at server 'miklos7' the process 'explorer' with PID = 1544
    has CPU usage of 9.5004609
at server '1_mikl1071_01' the process 'nlnotes' with PID = 2752
    has CPU usage of '? (not a value)'
First computer in the CSV is my local PC, from which I run my script. The second is a remote computer, where I'm admin.
I wonder why get-process didn't show CPU from the remote computer...
 
I finally got the code working, just keep in mind you need to have a csv as describe by mikrom above and set it to the path you have it located in the code.

Code:
[COLOR=blue]
#Must be on a server to run that has access to all
#servers in your csv listand the more you change 
#the sleep seconds the more usage you may get.

$csv_path = "D:\CMonitor.csv"
$csv = Import-csv -path $csv_path -delimiter ';'
$color="GREEN"

foreach($line in $csv) {

	$srv = $line.SERVER 
	$prc = $line.PROCESSES 
	echo "Looking at the Server '$srv' running process '$prc'" 
	
	
    $procBefore=Get-Process -computername $line.Server -Name $line.Processes

    Write-Host "Sleeping..."
    sleep -Seconds 20

    $procafter=Get-Process -computername $line.Server -Name $line.Processes

	if(($procafter.workingset)-($procBefore.workingset) -gt 0){
		$usage="{0:P2}" -f (1-(($procBefore.workingset)/($procafter.workingset)))
		$usageColor="RED"
	} else {
		$usage="0 %"
		$usageColor="GREEN"
	}

    # write the results to the screen

    Write-Host -ForegroundColor $color ($procBefore.id) ($procBefore.name)
    Write-Host -ForegroundColor $color BEFORE: ($procBefore.workingset) AFTER: ($procafter.workingset)
    Write-Host -ForegroundColor $usageColor Usage: $usage
}

[/color]

I want to thank mikrom for all of his assistance and hopefully this code finds some use for people trying to do what this.

Thanks..
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top