Solved

Create powershell script to read in csv, match wmi data and then output new csv.

Posted on 2013-06-26
10
731 Views
Last Modified: 2013-06-26
I have a script that reads in a csv and reads the first column and runs a get-wmiobject win32_printer for Name, Portname, and local value. I need it to make a csv from this information.

It needs to match the computer name to each printer on it. It can repeat the computer name if needed to make sure they match up.

Below is what I have that doesn't do all the things I need.



$OU = "C:\Powershell\OU.csv"
 
IMPORT-CSV $OU | foreach {

$Comp = $_.Name
if ($_.Name -like '*FW-*'){




#$strComputer = "$Comp"



$printers  = @(Get-WmiObject Win32_Printer -computer $Comp | select Name,PortName,Local)


foreach ($print in $printers) {

     
     $1print += $print
     
     
   
     }
0
Comment
Question by:Gjdalton
[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
  • 4
  • 2
10 Comments
 
LVL 42

Expert Comment

by:sedgwick
ID: 39278137
cls
$OU = "C:\Powershell\OU.csv"

$data = @()
IMPORT-CSV $OU | foreach { 
	$Comp = $_.Name
	if ($_.Name -like '*FW-*'){
			$printers  = @(Get-WmiObject Win32_Printer -computer $Comp | select Name,PortName,Local)
			foreach ($print in $printers) {
				$data += $print			
		}
	}
}

$data | Export-Csv -NoTypeInformation c:\temp\result.csv

Open in new window

0
 
LVL 40

Expert Comment

by:Subsun
ID: 39278200
Try..
Import-Csv "C:\Powershell\OU.csv" | ? {$_.Name -like '*FW-*'} | % { 
	Get-WmiObject Win32_Printer -computer $_.Name | select Name,PortName,Local
} | Export-Csv c:\Report.csv -NTI

Open in new window

0
 

Author Comment

by:Gjdalton
ID: 39278580
The only thing I need to have added is the input matched up with printers in the output. So the computer names show up on the output. Like I said they can be repeated in the column if need be.

Thank you for the effort put in so far!
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 40

Expert Comment

by:Subsun
ID: 39278646
Which attribute you want to match with input computer name? SystemName or Name?

Based on the attribute you need to filter it.. For example.. Following code filter by Name (Printer Name) property..
Import-Csv "C:\Powershell\OU.csv" | ? {$_.Name -like '*FW-*'} | % { 
    $Comp = $_.Name
	Get-WmiObject Win32_Printer -computer $Comp | ? {$_.Name -eq $Comp} | select Name,PortName,Local
} | Export-Csv c:\Report.csv -NTI

Open in new window

0
 

Author Comment

by:Gjdalton
ID: 39278823
The computer name that is pulled in from the OU.csv.
 So I need the computer name pulled in from the ou.csv and the wmi query run on it. Then I need that computer name to show up in the output csv next to the printers and ports that are on it.

That's why i said repeat if needed.

So right now I have columns :
Name	PortName	Local 

Open in new window

from yours.

I need it to be:
Computer Name Name	PortName	Local

Open in new window


Thank you!
0
 
LVL 42

Expert Comment

by:sedgwick
ID: 39278879
Import-Csv "C:\Powershell\OU.csv" | ? {$_.Name -like '*FW-*'} | % { 
    $Comp = $_.Name
	$data = Get-WmiObject Win32_Printer -computer $Comp | ? {$_.Name -eq $Comp} | 
select Name,PortName,Local
$object = New-Object –TypeNamePSObject
$object | Add-Member –MemberTypeNoteProperty –Name Computer –Value $Comp 
$object | Add-Member –MemberTypeNoteProperty –Name Name –Value $data.Name
$object | Add-Member –MemberTypeNoteProperty –Name Port –Value $data.PortName
$object | Add-Member –MemberTypeNoteProperty –Name Local -Value $data.Local
$object 
} | Export-Csv c:\Report.csv -NTI

Open in new window

0
 

Author Comment

by:Gjdalton
ID: 39278970
Changing that a little gets me the Computer column with the data and creates the other columns but they are blank.

Thanks!
0
 
LVL 40

Accepted Solution

by:
Subsun earned 500 total points
ID: 39279039
So you just want to add the computer name to the output..
Try..
Import-Csv "C:\Powershell\OU.csv" | ? {$_.Name -like '*FW-*'} | % { 
    $Comp = $_.Name
	Get-WmiObject Win32_Printer -computer $Comp | select @{N="CompName";E={$Comp}},Name,PortName,Local
} | Export-Csv c:\Report.csv -NTI

Open in new window

0
 

Author Closing Comment

by:Gjdalton
ID: 39279075
Thank you for your time!
0
 
LVL 40

Expert Comment

by:Subsun
ID: 39279105
You are welcome!... I'm glad I could help..
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

A brief introduction to what I consider to be the best editor for PowerShell.
A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

696 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