Solved

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

Posted on 2013-06-26
10
717 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
  • 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
 
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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Utilizing an array to gracefully append to a list of EmailAddresses
Citrix XenApp, Internet Explorer 11 set to Enterprise Mode and using central hosted sites.xml file.
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…
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). …

757 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now