Solved

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

Posted on 2013-06-26
10
735 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
Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

The following article is intended as a guide to using PowerShell as a more versatile and reliable form of application detection in SCCM.
Auditing domain password hashes is a commonly overlooked but critical requirement to ensuring secure passwords practices are followed. Methods exist to extract hashes directly for a live domain however this article describes a process to extract u…
The viewer will learn how to count occurrences of each item in an array.
Windows 8 came with a dramatically different user interface known as Metro. Notably missing from that interface was a Start button and Start Menu. Microsoft responded to negative user feedback of the Metro interface, bringing back the Start button a…

691 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