Run Powershell Script upon Network Connection

Do you handle any scripting with batch still? No?!

Do you need to make a scheduled task using schtasks.exe that runs a PS script in a hidden window every time a network connection is made? No?!

Perfect!

Here is a one line cmd batch script for adding exactly that!

schtasks /create /tn "Net-Connected" /tr "powershell -executionpolicy bypass -file '%SYSTEMDRIVE%\example.ps1'" /SC ONEVENT /EC Microsoft-Windows-NetworkProfile/Operational /MO "*[System[Provider[@Name='Microsoft-Windows-NetworkProfile'] and EventID=10000]]" /RU "System" /F

Windows 10 Autologon on Deployment

I normally deploy Windows version 1607 and just recently became acquainted with the post-1703 OOBE and Autologon settings regression.

The issue:
OOBE will wipe autologon settings, if they are implemented during the same boot as setupcomplete.cmd. This nullifies scripted/touchless Windows 10 deployment methods.

The solution:
Use setupcomplete.cmd and schtasks.exe to make a scheduled task that runs on the next start. Upon reboot, the scheduled task waits [an amount of time] before adding autologon settings, making a task to delete itself and reboot the computer, then running the task.

My improvements:
– Consolidate the solution implemented by others into one script/scheduledtask.
– Avoid unnecessarily long and inconsistent wait times.
– Designed to handle both windows 7 and windows 10 deployments.
– Use a secure method (as secure as possible with autologon)

@ECHO OFF
:: Run this script as an onstart Scheduled Task created by setupcomplete.cmd, if implemented in a Windows 10 deployment
:: Run this script directly from setupcomplete.cmd, if implemented in a Windows 7 deployment
:: Include autologon.exe in the same directory as this script

:: Account to use for autologon
SET ACCOUNT=dummyuser
SET PASSWORD=dummypassword

PUSHD %~dp0
:: Check if windows 10
FOR /F "tokens=3" %%A IN ('REG QUERY "HKLM\Software\Microsoft\Windows NT\CurrentVersion" /V "ReleaseId"') DO SET "RELEASEID=%%A"

SET Key="HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon"

:: Avoid OOBE autologon regression, post win10 1703
IF 1%RELEASEID% GEQ 11703 (
	REG ADD %KEY% /V AutoAdminLogon /T REG_SZ /D "1" /F
	:WAIT_FOR_CHANGE
	REG QUERY %KEY% /V AutoAdminLogon
	IF %ERRORLEVEL%==0 TIMEOUT /T 3 /NOBREAK>nul & GOTO WAIT_FOR_CHANGE
)

:: Activate autologon
REG DELETE %KEY% /V AutoLogonCount /F
REG DELETE %KEY% /V DefaultUserName /F
timeout /t 1
autologon -accepteula %ACCOUNT% . %PASSWORD%
timeout /t 1
REG ADD %KEY% /V AutoAdminLogon /T REG_SZ /D "1" /F

:: Cleanup scheduled task and restart, if win10
IF DEFINED RELEASEID ( START cmd /c TIMEOUT /T 1 & SCHTASKS /DELETE /TN Autologon /F & Shutdown /r /t 1 )

:: Delete itself
(goto) 2>nul & del "%~f0"

Installing Fonts – Batch Script

An easy way to install a folder of fonts from an (elevated) batch script. I’ve compiled this from a couple different sources, into a single subroutine. It avoids duplicate fonts and their warning messages which require user interaction.

:: Install Fonts
CALL :INSTALL_FONT "\\Server\Share\Fonts"

GOTO END

::SUBROUTINES
:INSTALL_FONT
set strNewFontsFolder=%1
(
	echo Set fso = CreateObject^("Scripting.FileSystemObject"^)
	echo Set stdout = fso.GetStandardStream^(1^)
	echo stdout.WriteLine "Installing fonts from: " ^& "%strNewFontsFolder:"=%"
	echo Set objShellApp = CreateObject^("Shell.Application"^)
	echo Set objFSO = CreateObject^("Scripting.FileSystemObject"^)
	echo Set objFolder = objShellApp.Namespace^(20^)
	echo If objFSO.FolderExists^("%strNewFontsFolder:"=%"^) = True Then
	echo 	For Each objFile In objFSO.GetFolder^("%strNewFontsFolder:"=%"^).Files
	echo 		If UCase^(Right^(LCase^(objFile.Name^), 4^)^) = ".TTF" Then
	echo 			If objFSO.FileExists^(objFolder.Self.Path ^& "\" ^& objFile.Name^) = False Then objFolder.CopyHere objFile.Path
	echo 		End If
	echo 		If UCase^(Right^(LCase^(objFile.Name^), 4^)^) = ".FON" Then
	echo 			If objFSO.FileExists^(objFolder.Self.Path ^& "\" ^& objFile.Name^) = False Then objFolder.CopyHere objFile.Path
	echo 		End If
	echo 	Next
	echo End If
) > "%TEMP%\tmp_%~n0.vbs"
cscript //nologo "%TEMP%\tmp_%~n0.vbs"
del "%TEMP%\tmp_%~n0.vbs"
EXIT /B

:END