[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

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

Posted on 2013-06-26
10
Medium Priority
?
757 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:Meir Rivkin
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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:Meir Rivkin
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 2000 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 learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

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

Previously, on our Nano Server Deployment series, we've created a new nano server image and deployed it on a physical server in part 2. Now we will go through configuration.
The Windows functions GetTickCount and timeGetTime retrieve the number of milliseconds since the system was started. However, the value is stored in a DWORD, which means that it wraps around to zero every 49.7 days. This article shows how to solve t…
In this video, we discuss why the need for additional vertical screen space has become more important in recent years, namely, due to the transition in the marketplace of 4x3 computer screens to 16x9 and 16x10 screens (so-called widescreen format). …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

656 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