Hi Guys,
Im have a script to extract the needed data from Printers and save to a file in one column (Version1). What I wanted to do is format the output so that it saves in csv format as a table with headers (Version2).
I think it is because were I have $o | export-csv $Outputfile -noType placed but could be wrong.
Can anyone help with my problem please?
**************************************************************
Version 1 below: working
**************************************************************
$Inputfile = Read-Host "Read in?"
$Printservers = get-content $Inputfile
$Outputfile = Read-Host "Save to?"
ForEach ($Printserver in $Printservers)
{ $Printers = Get-WmiObject Win32_Printer -ComputerName $Printserver
ForEach ($Printer in $Printers)
{
if ($Printer.Name -notlike "Microsoft XPS*")
{ $Printer.DriverName >> $Outputfile
If ($Printer.PortName -notlike "*\*")
{ $Ports = Get-WmiObject Win32_TcpIpPrinterPort -Filter "name = '$($Printer.Portname)'" -ComputerName $Printserver
ForEach ($Port in $Ports)
{ $Port.Name >> $Outputfile
$Port.HostAddress >> $Outputfile
}
}
$Printer.ShareName >> $Outputfile
$Printer.Location >> $Outputfile
$Printer.Name >> $Outputfile
}
}
}
**************************************************************
Version 2 below: not working
**************************************************************
$Inputfile = Read-Host "Read in?"
$Printservers = get-content $Inputfile
$Outputfile = Read-Host "Save to?
$o = @()
ForEach ($Printserver in $Printservers)
{ $Printers = Get-WmiObject Win32_Printer -ComputerName $Printserver
ForEach ($Printer in $Printers)
{
if ($Printer.Name -notlike "Microsoft XPS*")
{ $o = new-object PSObject
$o | add-member NoteProperty DriverName $Printer.DriverName
If ($Printer.PortName -notlike "*\*")
{ $Ports = Get-WmiObject Win32_TcpIpPrinterPort -Filter "name = '$($Printer.Portname)'" -ComputerName $Printserver
ForEach ($Port in $Ports)
{ $o | add-member NoteProperty PortName $Port.Name
$o | add-member NoteProperty IP $Port.HostAddress
}
}
$o | add-member NoteProperty ShareName $Printer.ShareName
$o | add-member NoteProperty Location $Printer.Location
$o | add-member NoteProperty Name $Printer.Name
}
}
} $o | export-csv $Outputfile -noType
Im have a script to extract the needed data from Printers and save to a file in one column (Version1). What I wanted to do is format the output so that it saves in csv format as a table with headers (Version2).
I think it is because were I have $o | export-csv $Outputfile -noType placed but could be wrong.
Can anyone help with my problem please?
**************************************************************
Version 1 below: working
**************************************************************
$Inputfile = Read-Host "Read in?"
$Printservers = get-content $Inputfile
$Outputfile = Read-Host "Save to?"
ForEach ($Printserver in $Printservers)
{ $Printers = Get-WmiObject Win32_Printer -ComputerName $Printserver
ForEach ($Printer in $Printers)
{
if ($Printer.Name -notlike "Microsoft XPS*")
{ $Printer.DriverName >> $Outputfile
If ($Printer.PortName -notlike "*\*")
{ $Ports = Get-WmiObject Win32_TcpIpPrinterPort -Filter "name = '$($Printer.Portname)'" -ComputerName $Printserver
ForEach ($Port in $Ports)
{ $Port.Name >> $Outputfile
$Port.HostAddress >> $Outputfile
}
}
$Printer.ShareName >> $Outputfile
$Printer.Location >> $Outputfile
$Printer.Name >> $Outputfile
}
}
}
**************************************************************
Version 2 below: not working
**************************************************************
$Inputfile = Read-Host "Read in?"
$Printservers = get-content $Inputfile
$Outputfile = Read-Host "Save to?
$o = @()
ForEach ($Printserver in $Printservers)
{ $Printers = Get-WmiObject Win32_Printer -ComputerName $Printserver
ForEach ($Printer in $Printers)
{
if ($Printer.Name -notlike "Microsoft XPS*")
{ $o = new-object PSObject
$o | add-member NoteProperty DriverName $Printer.DriverName
If ($Printer.PortName -notlike "*\*")
{ $Ports = Get-WmiObject Win32_TcpIpPrinterPort -Filter "name = '$($Printer.Portname)'" -ComputerName $Printserver
ForEach ($Port in $Ports)
{ $o | add-member NoteProperty PortName $Port.Name
$o | add-member NoteProperty IP $Port.HostAddress
}
}
$o | add-member NoteProperty ShareName $Printer.ShareName
$o | add-member NoteProperty Location $Printer.Location
$o | add-member NoteProperty Name $Printer.Name
}
}
} $o | export-csv $Outputfile -noType