question

vijayakumarD-1665 avatar image
0 Votes"
vijayakumarD-1665 asked IanXue-MSFT commented

How to clear extra lines in output while running powershell sript and Output CSV file

Hi All,

I have script for service status check service is running or stopped but while run the script output results shows in script itself and same will come in CSV output reportfile . Please help on this issue it will be very helpfull.

Service Statdu Check Scripts

$Servers = Get-Content -Path "C:\Users\CPCWM\Desktop\Services Status Pre patch checking\Serverlist.txt"
$Services = Get-Content -Path "C:\Users\CPCWM\Desktop\Services Status Pre patch checking\Services.txt"
$Date = Get-Date -Format "dd-mm-yy hh:mm"
$Servreportpre = $null
$Servreportpre = @()
$x=0
foreach($Server in $Servers)
{ $x=$x+1
Write-Host "Service Status StartType for Server $x :$Server" -ForegroundColor Yellow
foreach($Service in $Services)
{
$Status = Get-WmiObject Win32_Service -ComputerName $Servers | ?{$.Name -eq $Service} | Select-Object -ExpandProperty "State"
$StartType = Get-WmiObject Win32_Service -ComputerName $Servers | ?{$
.Name -eq $Service} | Select-Object -ExpandProperty "StartMode"
if($Status -like "Running")
{
Write-Host "$Service,$Status,$StartType" -ForegroundColor Green
$Servreportpre += $Server | Select @{n='ServerName';e={$}}, @{n='ServiceName';e={"$Service"}}, @{n='Status';e={"$Status"}}, @{n='StartType';e={"$StartType"}}
}
elseif($Status -like "Stopped")
{
Write-Host "$Service,$Status,$StartType" -ForegroundColor Red
$Servreportpre += $Server | Select @{n='ServerName';e={$
}}, @{n='ServiceName';e={"$Service"}}, @{n='Status';e={"$Status"}}, @{n='StartType';e={"$StartType"}}
}
}
}
$Pdate = Get-Date -Format "dd-mm-yy hh:mm"
$Servreportpre | Export-Csv -Path "C:\Users\CPCWM\Desktop\Services Status Pre patch checking\Service Status Pre Check Report.csv" -NoTypeInformation

Output in CSV File

83912-![83892-output-in-csv-file.jpg83921-powershell-script-output-with-error.jpgimage.png][1]


windows-serverwindows-server-powershell
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi,
Is there any update? Have you got a chance to verify the below suggestions?
Please feel free to let us know if more assistance is needed. If the reply is helpful, please “Accept Answer” to help other community members find it more easily.

0 Votes 0 ·

1 Answer

IanXue-MSFT avatar image
0 Votes"
IanXue-MSFT answered IanXue-MSFT edited

Hi,

If no output is wanted you can simply remove the ”Write-Host“ lines.

 $Servers = Get-Content -Path "C:\Users\CPCWM\Desktop\Services Status Pre patch checking\Serverlist.txt"
 $Services = Get-Content -Path "C:\Users\CPCWM\Desktop\Services Status Pre patch checking\Services.txt"
 $Date = Get-Date -Format "dd-mm-yy hh:mm"
 $Servreportpre = $null
 $Servreportpre = @()
 $x=0 
 foreach($Server in $Servers)
 {
     $x=$x+1
     Write-Host "Service Status StartType for Server $x :$Server" -ForegroundColor Yellow
     foreach($Service in $Services)
     {
         $Status = Get-WmiObject Win32_Service -ComputerName $Servers | ?{$_.Name -eq $Service} | Select-Object -ExpandProperty "State"
         $StartType = Get-WmiObject Win32_Service -ComputerName $Servers | ?{$_.Name -eq $Service} | Select-Object -ExpandProperty "StartMode"
         if($Status -like "Running")
         {
             $Servreportpre += $Server | Select @{n='ServerName';e={$_}}, @{n='ServiceName';e={"$Service"}}, @{n='Status';e={"$Status"}}, @{n='StartType';e={"$StartType"}}
         }
         elseif($Status -like "Stopped")
         {
             $Servreportpre += $Server | Select @{n='ServerName';e={$_}}, @{n='ServiceName';e={"$Service"}}, @{n='Status';e={"$Status"}}, @{n='StartType';e={"$StartType"}}
         }
     }
  }
 $Pdate = Get-Date -Format "dd-mm-yy hh:mm"
 $Servreportpre | Export-Csv -Path "C:\Users\CPCWM\Desktop\Services Status Pre patch checking\Service Status Pre Check Report.csv" -NoTypeInformation

Best Regards,
Ian Xue
============================================
If the Answer is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.