Gestionar tareas desde Powershell

Durante la pandemia decidí utilizar el tiempo muerto para aprender más Powershell, uno de los proyectos que usé para motivarme fue un script para hacer un deploy de Windows 10 desde limpio a usable. 
Nada que configuradores como Ansible no hagan ya mejor, pero necesitaba engrasar mis habilidades haciendo algo.

Una de las tareas es actualizar el software instalado previamente usando chocolatey a través de una tarea programada. El workflow viene a ser este:

  1. Actualiza todo
  2. Borra todos los iconos que se creen en el escritorio público
  3. Lanza la tarea todos los días a las 0AM o, cuando el equipo esté disponible tras esa hora. 
$taskAction = New-ScheduledTaskAction -Execute 'powershell.exe' -Argument 'chocolatey upgrade --yes all; Get-ChildItem C:\Users\Public\Desktop -filter '*lnk' | foreach-object { Remove-item -Path $_.Fullname  -Force -ErrorAction SilentlyContinue }'       
$taskTrigger = New-ScheduledTaskTrigger -Daily -At 0AM
$taskPrincipal = New-ScheduledTaskPrincipal -UserId "SYSTEM" -LogonType ServiceAccount
$taskSettings=New-ScheduledTaskSettingsSet -StartWhenAvailable
Register-ScheduledTask -TaskName "ChocolateyUpdate" -Action $taskAction -Trigger $taskTrigger -Description "Update chocolatey packages" -Principal $taskPrincipal -Settings $taskSettings  

Los logs asociados a la actualización son los de chocolatey, en una instalación por defecto, a fecha de escritura, están en C:\ProgramData\chocolatey\logs

 

·n·

Powershell – Using defined variables in a scriptblock

If you try use an already defined variable inside a scripblock, using the default call without any scope definition, the variable will not be expanded and it probably will not produce the expected outcome. 

From Powershell v3 onwards, the scopes include an «using», that allows to access defined variables in other scopes, in this way the variable will be expanded as expected inside the scriptblock.

$foo = 'Get-Childitem .'
Invoke-Command -Computername server -scriptblock { $using:foo }

 

·n·

Synology – Acceso lento

Desde la actualización de Windows a 20H2 el acceso a la ds1512+ se vuelve lento de repente durante una transferencia via SMB. Empieza bien y en algún punto de la transferencia se detiene. 

Si intentas navegar al dispositivo via SMB el Explorer se queda en no reponde, el acceso via navegador o NFS sigue funcionando como esperarías. 

¿Qué hacer para solucionar este problema y cual es el problema raiz?

Sigue leyendo Synology – Acceso lento