Skip to content

Instantly share code, notes, and snippets.

@raulunzue
Created December 14, 2020 08:34
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save raulunzue/320ddd62522cf1761cd948895a488b60 to your computer and use it in GitHub Desktop.
#########################
## RAUL UNZUE - EL BLOG DE NEGU
## IMPRESORAS MAPEADAS
#########################
## LOG
$Logfile = "\\nas\elblogdenegu\IMPRESORAS\impresoras.log"
#Start-Transcript -Path $Logfile -Append
# FECHA Y HORA
$date = Get-Date -Format "dd-MM-yyyy"
$hora = Get-Date -Format "HH:mm:ss"
# VALIDAMOS QUE EXISTE YA EL FICHERO
if ((Get-Content \\nas\elblogdenegu\IMPRESORAS\impresoras-$env:USERNAME.csv) -ne $null){
# PUNTO DE CONTROL
Write-Output "###############################################################" | out-file -Filepath $Logfile -append
Write-Output "#####$date - $hora - INVENTARIO YA CREADO PARA - $env:USERNAME" | out-file -Filepath $Logfile -append
Write-Output "###############################################################" | out-file -Filepath $Logfile -append
}
else{
# OBTENER IMPRESORA PREDETERMINADA
cscript.exe C:\Windows\System32\Printing_Admin_Scripts\es-ES\prnmngr.vbs -g > \\nas\elblogdenegu\IMPRESORAS\impresora-predeterminada-$env:USERNAME.txt
# USAMOS LA LINEA 3
(Get-Content \\nas\elblogdenegu\IMPRESORAS\impresora-predeterminada-$env:USERNAME.txt)[3] | ForEach-Object { $_ -replace "La impresora predeterminada es ","" } | Set-Content \\nas\elblogdenegu\IMPRESORAS\impresora-predeterminada-$env:USERNAME.txt
$predeterminada=(Get-Content \\nas\elblogdenegu\IMPRESORAS\impresora-predeterminada-$env:USERNAME.txt)
# PUNTO DE CONTROL
Write-Output "#############################################################" | out-file -Filepath $Logfile -append
Write-Output "#####$date - $hora - IMPRESORA PREDETERMINADA - $env:USERNAME" | out-file -Filepath $Logfile -append
Write-Output "#############################################################" | out-file -Filepath $Logfile -append
Write-Output $predeterminada | out-file -Filepath $Logfile -append
# OBTENER IMPRESORAS
$impresoras = Get-Printer
$impresoras.Name > \\nas\elblogdenegu\IMPRESORAS\impresoras-$env:USERNAME.txt
$printers= Get-Content \\nas\elblogdenegu\IMPRESORAS\impresoras-$env:USERNAME.txt
# PUNTO DE CONTROL
Write-Output "#############################################################" | out-file -Filepath $Logfile -append
Write-Output "#####$date - $hora - IMPRESORAS CONFIGURADAS - $env:USERNAME" | out-file -Filepath $Logfile -append
Write-Output "#############################################################" | out-file -Filepath $Logfile -append
Write-Output $printers | out-file -Filepath $Logfile -append
# EXPORTAMOS A EXCEL
# EXPORTAR IMPRESORAS USUARIO
$Output = foreach ($p in $printers) {
New-Object -TypeName PSObject -Property @{
Usuario = $env:USERNAME
Impresora = $p
} | Select-Object Usuario,Impresora
}
$Output | Export-Csv -NoTypeInformation -Path \\nas\elblogdenegu\IMPRESORAS\impresoras-$env:USERNAME.csv -Append
# EXPORTAR IMPRESORA PREDETERMINADA
$Output2 = New-Object -TypeName PSObject -Property @{
Usuario = $env:USERNAME
ImpresoraPredeterminada = $predeterminada
} | Select-Object Usuario,ImpresoraPredeterminada
$Output2 | Export-Csv -NoTypeInformation -Path \\nas\elblogdenegu\IMPRESORAS\impresora-predeterminada-$env:USERNAME.csv
# BORRAR TXT
Remove-Item -Path \\nas\elblogdenegu\IMPRESORAS\impresoras-$env:USERNAME.txt
Remove-Item -Path \\nas\elblogdenegu\IMPRESORAS\impresora-predeterminada-$env:USERNAME.txt
}
#Stop-Transcript
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment