日志中的自定义日期字符串,无需修改 cmdlet 的退出

问题描述 投票:0回答:1

这是一个获取一系列电脑上安装的所有打印机的脚本,我无法在不修改命令退出的情况下获取日志中的日期。 我想将日期放在退出命令的顶部/底部。

$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

          }
    }

这就是我试图在里面获取日期的内容,但格式变得非常混乱。

$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
powershell date system-administration
1个回答
0
投票

你让我多想了一点@jdweng ty对我有用。

$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
© www.soinside.com 2019 - 2024. All rights reserved.