@ECHO OFF TITLE DIGITAL DATA - %~n0 ECHO - ECHO Windows Command Line Script ECHO CMD Version %CMDEXTVERSION% active! ECHO - ECHO Digital Data ECHO Ludwig-Rinn-Strasse 16 ECHO 35452 Heuchelheim ECHO Tel.: 0641 / 202360 ECHO E-Mail: info@didalog.de ECHO - ECHO Version Number: 1.0.0.0 ECHO Version Date: 03.06.2018 pushd "%~dp0" setlocal REM -------------------------------------------------------------- REM ----------------------- set variables ------------------------ REM -------------------------------------------------------------- SET ScriptName=%~n0 REM Automatic defined Variable, do not change! REM Write WITHOUT double Quotes("")! SET ScriptPath=%~dp0 REM Automatic defined Variable, do not change! REM Write WITHOUT double Quotes("")! SET SearchPath=%cd%\ REM Automatic defined Variable, maybe change, if you want to search in designated folder - dont forget the "\" in the end! REM Write WITHOUT double Quotes("")! SET SearchFileFilter="*-Example.cmd,*-Example.vbs,*-Example.ps1,*-Example.exe" REM Set File extension *.exe, *.ps1, *.vbs or *.cmd. Or set whole Filename like test.ps1. Wildcard is also possible: test*.ps1. Also a comma seperated list is possible like *.ps1, *.vbs. REM Write WITHOUT double Quotes(""), for a single value! Write WITH double Quotes(""), for comma seperated values. SET Counter=0 REM Predefined Variable, do not change! REM Write WITHOUT double Quotes("")! SET PowerShellCMD=powershell.exe -ExecutionPolicy Bypass -WindowStyle Hidden -File REM Predefined Variable, do not change! REM Write WITHOUT double Quotes("")! SET PowerShellRuntimeScript="%ScriptPath%\%ScriptName%_PowerShellRuntimeScript.ps1" REM Automatic defined Variable, do not change! REM Write WITH double Quotes("")! SET PowerShellWaitForExit=True REM Set to "True" if found EXE or Scripts Files should be processed one after another. Otherwise set to "False". REM Write WITHOUT double Quotes("")! SET RunAsUserName= REM Set an Username, if you want the EXE or Scripts Files run in an other context, like: "Administrator". But maybe: "domain\Administrator". REM Write WITHOUT double Quotes("")! SET RunAsUserPassword= REM Set Password if you defined the RunAsUserName Variable. REM Write WITHOUT double Quotes("")! REM -------------------------------------------------------------- GOTO mainpart REM -------------------------------------------------------------- REM ------------------------- subroutines ------------------------ REM -------------------------------------------------------------- :CreatePowerShellRuntimeScript REM GENERIC STARTING PART SET Item=%1 ECHO - ECHO Creating PowerShellRuntimeScript for: %Item% ECHO #Automatic generated PowerShellRuntimeScript>%PowerShellRuntimeScript% ECHO Clear-Host>%PowerShellRuntimeScript% ECHO $UserName=^"%RunASUserName%^">>%PowerShellRuntimeScript% ECHO $UserPassword=^"%RunAsUserPassword%^">>%PowerShellRuntimeScript% ECHO $Item=(Get-Item %Item%)>>%PowerShellRuntimeScript% ECHO $Credentials = New-Object System.Management.Automation.PSCredential -ArgumentList @($UserName,(ConvertTo-SecureString -String $UserPassword -AsPlainText -Force))>>%PowerShellRuntimeScript% ECHO.>>%PowerShellRuntimeScript% REM BLOCK FOR CMD FILES IF NOT x%Item:.cmd=%==x%Item% ( ECHO #Runtime File is exclusive build for %SearchFileFilter%>>%PowerShellRuntimeScript% ECHO $Process = New-Object System.Diagnostics.Process>>%PowerShellRuntimeScript% ECHO $Process.StartInfo.Filename = ^"cmd.exe^">>%PowerShellRuntimeScript% IF [%RunASUserName%] EQU [] ( ECHO #Start Process in current context.>>%PowerShellRuntimeScript% ECHO.>>%PowerShellRuntimeScript% ECHO $Process.StartInfo.UseShellExecute = $True>>%PowerShellRuntimeScript% ) ELSE ( ECHO #Start Process in RunAs context.>>%PowerShellRuntimeScript% ECHO $Process.StartInfo.UserName = $Credentials.UserName>>%PowerShellRuntimeScript% ECHO $Process.StartInfo.Password = $Credentials.Password>>%PowerShellRuntimeScript% ECHO $Process.StartInfo.UseShellExecute = $False>>%PowerShellRuntimeScript% ECHO $Process.StartInfo.LoadUserProfile = $True>>%PowerShellRuntimeScript% ) ECHO $Process.StartInfo.Arguments = "/c ""$($Item.FullName)""">>%PowerShellRuntimeScript% ECHO #$Process.StartInfo.RedirectStandardOutput = $False>>%PowerShellRuntimeScript% ECHO #$Process.StartInfo.RedirectStandardError = $True>>%PowerShellRuntimeScript% ECHO $Process.Start^(^)>>%PowerShellRuntimeScript% ) REM BLOCK FOR PS1 FILES IF NOT x%Item:.ps1=%==x%Item% ( ECHO #Runtime File is exclusive build for %SearchFileFilter%>>%PowerShellRuntimeScript% ECHO $Process = New-Object System.Diagnostics.Process>>%PowerShellRuntimeScript% ECHO $Process.StartInfo.Filename = ^"powershell.exe^">>%PowerShellRuntimeScript% IF [%RunASUserName%] EQU [] ( ECHO #Start Process in current context.>>%PowerShellRuntimeScript% ECHO.>>%PowerShellRuntimeScript% ECHO $Process.StartInfo.UseShellExecute = $True>>%PowerShellRuntimeScript% ) ELSE ( ECHO #Start Process in RunAs context.>>%PowerShellRuntimeScript% ECHO $Process.StartInfo.UserName = $Credentials.UserName>>%PowerShellRuntimeScript% ECHO $Process.StartInfo.Password = $Credentials.Password>>%PowerShellRuntimeScript% ECHO $Process.StartInfo.UseShellExecute = $False>>%PowerShellRuntimeScript% ECHO $Process.StartInfo.LoadUserProfile = $True>>%PowerShellRuntimeScript% ) ECHO $Process.StartInfo.Arguments = "-ExecutionPolicy Bypass -file ""$($Item.FullName)""">>%PowerShellRuntimeScript% ECHO #$Process.StartInfo.RedirectStandardOutput = $False>>%PowerShellRuntimeScript% ECHO #$Process.StartInfo.RedirectStandardError = $True>>%PowerShellRuntimeScript% ECHO $Process.Start^(^)>>%PowerShellRuntimeScript% ) REM BLOCK FOR VBS FILES IF NOT x%Item:.vbs=%==x%Item% ( ECHO #Runtime File is exclusive build for %SearchFileFilter%>>%PowerShellRuntimeScript% ECHO $Process = New-Object System.Diagnostics.Process>>%PowerShellRuntimeScript% ECHO $Process.StartInfo.Filename = ^"wscript.exe^">>%PowerShellRuntimeScript% IF [%RunASUserName%] EQU [] ( ECHO #Start Process in current context.>>%PowerShellRuntimeScript% ECHO.>>%PowerShellRuntimeScript% ECHO $Process.StartInfo.UseShellExecute = $True>>%PowerShellRuntimeScript% ) ELSE ( ECHO #Start Process in RunAs context.>>%PowerShellRuntimeScript% ECHO $Process.StartInfo.UserName = $Credentials.UserName>>%PowerShellRuntimeScript% ECHO $Process.StartInfo.Password = $Credentials.Password>>%PowerShellRuntimeScript% ECHO $Process.StartInfo.UseShellExecute = $False>>%PowerShellRuntimeScript% ECHO $Process.StartInfo.LoadUserProfile = $True>>%PowerShellRuntimeScript% ) ECHO $Process.StartInfo.Arguments = ^"^"^"^"^"^"^"^"^"^"^"^$^(^$Item.FullName^)^"^"^"^"^"^"^"^"^"^"^">>%PowerShellRuntimeScript% ECHO #$Process.StartInfo.RedirectStandardOutput = $False>>%PowerShellRuntimeScript% ECHO #$Process.StartInfo.RedirectStandardError = $True>>%PowerShellRuntimeScript% ECHO $Process.Start^(^)>>%PowerShellRuntimeScript% ) REM BLOCK FOR EXE FILES IF NOT x%Item:.exe=%==x%Item% ( ECHO #Runtime File is exclusive build for %SearchFileFilter%>>%PowerShellRuntimeScript% ECHO $Process = New-Object System.Diagnostics.Process>>%PowerShellRuntimeScript% ECHO $Process.StartInfo.Filename = ^"^$^(^$Item.FullName^)^">>%PowerShellRuntimeScript% IF [%RunASUserName%] EQU [] ( ECHO #Start Process in current context.>>%PowerShellRuntimeScript% ECHO.>>%PowerShellRuntimeScript% ECHO $Process.StartInfo.UseShellExecute = $True>>%PowerShellRuntimeScript% ) ELSE ( ECHO #Start Process in RunAs context.>>%PowerShellRuntimeScript% ECHO $Process.StartInfo.UserName = $Credentials.UserName>>%PowerShellRuntimeScript% ECHO $Process.StartInfo.Password = $Credentials.Password>>%PowerShellRuntimeScript% ECHO $Process.StartInfo.UseShellExecute = $False>>%PowerShellRuntimeScript% ECHO $Process.StartInfo.LoadUserProfile = $True>>%PowerShellRuntimeScript% ) ECHO #$Process.StartInfo.RedirectStandardOutput = $False>>%PowerShellRuntimeScript% ECHO #$Process.StartInfo.RedirectStandardError = $True>>%PowerShellRuntimeScript% ECHO $Process.Start^(^)>>%PowerShellRuntimeScript% ) REM GENERIC FINISHING PART IF /i %PowerShellWaitForExit% EQU True ( ECHO $Process.WaitForExit^(^)>>%PowerShellRuntimeScript% ) ELSE ( ECHO #$Process.WaitForExit^(^)>>%PowerShellRuntimeScript% ) ECHO #[string] $Out = $Process.StandardOutput.ReadToEnd();>>%PowerShellRuntimeScript% ECHO.>>%PowerShellRuntimeScript% ECHO Remove-Variable -Name UserName -Force -ErrorAction SilentlyContinue>>%PowerShellRuntimeScript% ECHO Remove-Variable -Name UserPassword -Force -ErrorAction SilentlyContinue>>%PowerShellRuntimeScript% ECHO Remove-Variable -Name Item -Force -ErrorAction SilentlyContinue>>%PowerShellRuntimeScript% ECHO Remove-Variable -Name Credentials -Force -ErrorAction SilentlyContinue>>%PowerShellRuntimeScript% ECHO Remove-Variable -Name Process -Force -ErrorAction SilentlyContinue>>%PowerShellRuntimeScript% ECHO. ECHO Starting RuntimeFile: %PowerShellRuntimeScript% %PowerShellCMD% %PowerShellRuntimeScript% DEL %PowerShellRuntimeScript% GOTO :eof REM -------------------------------------------------------------- REM -------------------------------------------------------------- REM ------------------------- main part -------------------------- REM -------------------------------------------------------------- :mainpart cls ECHO - ECHO Program Startup: %ScriptName% on %computername%, ECHO from Account: %userdomain%\%username%. ECHO On %date% at %time:~0,8% o'clock. REM Count Files and show Counter FOR /F "tokens=*" %%f in ('dir /S /b "%SearchPath%\%SearchFileFilter%"') do (set /a Counter+=1) ECHO - ECHO Found %Counter% File(s) with File extension %SearchFileFilter% in Directory: ECHO %SearchPath% REM If Counter greater then 0 call subroutine for every found file IF %Counter% GTR 0 ( FOR /F "tokens=*" %%f in ('dir /S /b "%SearchPath%\%SearchFileFilter%"') DO (Call :CreatePowerShellRuntimeScript "%%f") GOTO PlanedExit ) ELSE ( ECHO - ECHO Found no Files. Exiting... GOTO UnplanedExit ) REM -------------------------------------------------------------- REM -------------------------------------------------------------- REM ----------------------- finishing part ----------------------- REM -------------------------------------------------------------- :UnplanedExit endlocal ECHO - ECHO This Window will close in: timeout /T 10 rem pause exit :PlanedExit endlocal rem pause exit REM --------------------------------------------------------------