Powershell - Looking for contains string in array

Viewed 35

Hope all is well.

Looking to get some feedback and one help with this.

I'm working on task to get a list all ACTIVE printers connected to network on bunch of file/print server.

I need to append the all the output from the each server to CSV file. I want to see how I can add blank line in between the each server output.

What I would like the CSV file to look like,

FilePrinterServer PrinterName IPV4Address PrinterDriverName PrinterComment

Server 1 Printer1 10.10.10.11

Server 1 Printer2 10.10.10.12

Blank Space

Server 2 Printer1 10.20.10.12

Server 2 Printer1 10.20.10.13

Not sure if it this is even possible but just want to get an idea, how to out the nicely to differentiate the result quickly between servers for the management team.

Here is my invoke command I'm running

Invoke-Command -ComputerName (get-content Serverlist.txt) -ScriptBlock $ActivePrinterResult -Credential $Credientals 

This is the script block I created for Invoke.

#Script Block Variable.
$ActivePrinterResult = {

#Get all the Printers installed on the computer
$printers = Get-Printer | Select-Object portName,DriverName,Comment

#Get the final Output from Foreach statement 

$FinalResultOut = 
Foreach ( $printer in $printers )
    {
#Testing to see if the printer is active or note
If (Test-Connection $printer.portname -Count 1 -Quiet -ErrorAction SilentlyContinue) 
{
#Need get the IP address of the printer for the output
$IPaddressPrinter =  Test-Connection $printer.portname -Count 1

#Create custom properties for output. Need to have ComputerName,PrinterName,IP and DriverName and Comments

 [pscustomobject]@{
        FilePrinterServer= $env:computername
        PrinterName = $printer.Portname
        IPV4Address = $IPaddressPrinter.IPV4Address
        PrinterDriverName = $printer.DriverName
        PrinterComment = $printer.Comment
        
                   }

         }
 Else {
 #Export Printer not responding to CSV File

Write-output $printer.Portname 'on'  $env:computername 'not responding' | Out-        File -FilePath '\\Testing01\TestFolder$\NotRespondingPrinter.csv' -Append
    }

   
    }
#All the Active Printer and Output to CSV File

$FinalResultOut |  Export-Csv -Path '\\Testing01\TestFolder$\Outfile.CSV' -Append -NoTypeInformation
1 Answers

You could group results by server name, and then output a blank line after each group.

$FinalResultOut | Group FilePrinterServer | ForEach-Object{
    $_.Group 
    [PSCustomObject]@{FilePrinterServer=''}
} | Export-Csv -Path '\\Testing01\TestFolder$\Outfile.CSV' -Append -NoTypeInformation
Related