Custom date string inside a log without modifying the exit of the cmdlet

56 Views Asked by At

Heres a script that gets all printers installed on a range of pcs , i have trouble getting the date inside the log without modifying the exit of the command. I would like to put the date on top/down of the exit command. enter image description here

$fecha = (Get-Date).ToString(' dd.MM.yyyy "y en esta hora" HH:mm:ss')

$equipos = "D:\SCRIPTSM\despFi\cont-pla.txt"
$listaEquipos = Get-Content $equipos

foreach ($equipo in $listaEquipos){
    Try {
        if (test-connection -ComputerName $equipo -Count 1 -Quiet){
           
        Get-Printer -ComputerName "$equipo" -ErrorAction Stop | Where-Object {$_.Name | Select-String -NotMatch "Fax","Microsoft","OneNote","PDF"} >> D:\SCRIPTSM\ALV\PRUEBA.log

    }else{
        Write-Output "El equipo $equipo está apagado el día $fecha " >> D:\SCRIPTSM\ALV\fallos.log
        }
    }catch{

     $Error[0].Exception.Message
     Write-Output "$equipo se necesita reiniciar para sacar las impresoras instaladas $fecha" >> D:\SCRIPTSM\ALV\PRUEBA.log

          }
    }

And heres what i tried to get the date inside but the format gets messed up so bad.

enter image description here

$fecha = (Get-Date).ToString(' dd.MM.yyyy "y en esta hora" HH:mm:ss')

$equipos = "D:\SCRIPTSM\despFi\cont-pla.txt"
$listaEquipos = Get-Content $equipos

foreach ($equipo in $listaEquipos){
    Try {
        if (test-connection -ComputerName $equipo -Count 1 -Quiet){
           
        $Impre = Get-Printer -ComputerName "$equipo" -ErrorAction Stop | Where-Object {$_.Name | Select-String -NotMatch "Fax","Microsoft","OneNote","PDF"}
    
     Write-Output "$Impre Se realizó este día $fecha  " >> D:\SCRIPTSM\ALV\PRUEBA.log
#Same code as on top continuing from here down
1

There are 1 best solutions below

0
AALV8 On BEST ANSWER

U made me think a little more @jdweng ty works for me. enter image description here

$fecha = (Get-Date).ToString(' dd.MM.yyyy "y en esta hora" HH:mm:ss')

$equipos = "D:\SCRIPTSM\despFi\cont-pla.txt"
$listaEquipos = Get-Content $equipos

foreach ($equipo in $listaEquipos){
    Try {
        if (test-connection -ComputerName $equipo -Count 1 -Quiet){
           
        $Impre = Get-Printer -ComputerName "$equipo" -ErrorAction Stop | Where-Object {$_.Name | Select-String -NotMatch "Fax","Microsoft","OneNote","PDF"}
    
     Write-Output $Impre $fecha >> D:\SCRIPTSM\ALV\PRUEBA.log
#Same code as on top continuing from here down