?
Solved

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

Posted on 2013-06-26
10
Medium Priority
?
765 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: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
A Cyber Security RX to Protect Your Organization

Join us on December 13th for a webinar to learn how medical providers can defend against malware with a cyber security "Rx" that supports a healthy technology adoption plan for every healthcare organization.

 
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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
In this post we will be converting StringData saved within a text file into a hash table. This can be further used in a PowerShell script for replacing settings that are dynamic in nature from environment to environment.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses
Course of the Month13 days, 14 hours left to enroll

807 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