New
#1
Batch File - PowerShell -ExecutionPolicy ByPass -File
I know this is probably simple to correct but I am having a problem.
I have a Folder with .PS1 files that I want to iterate through and run one after the other. The thing is I think I have complicated the permissions side of things. Here is the Script . . .
Code:@echo off color 17 & if NOT "%1"=="MAX" start /MAX cmd /c %0 MAX & Exit/b setlocal EnableDelayedExpansion set "params=%*" cd /d "%~dp0" && ( if exist "%Temp%\getadmin.vbs" del "%Temp%\getadmin.vbs") && fsutil dirty query %systemdrive% 1>nul 2>nul || ( echo Set UAC = CreateObject^("Shell.Application"^) : UAC.ShellExecute "cmd.exe", "/k cd ""%~sdp0"" && %~s0 %params%", "", "runas", 1 >> "%Temp%\getadmin.vbs" && "%Temp%\getadmin.vbs" && Exit /b) %windir%\system32\reg.exe query "HKU\S-1-5-19" 1>nul 2>nul || ( echo. & echo ERROR: This Batch file MUST be run in an ELEVATED cmd prompt [ Administrator ] & echo. & echo Right-click the Batch file and click ^<Run as Administrator^>. & echo. & echo ^>Press ANY key to EXIT . . . & pause >nul & Exit ) set "Folder=C:\System-Admin_Implementation" set Count=0 echo. if exist "%Temp%\*.vbs" (del "%Temp%\*.vbs") echo set X=CreateObject("WScript.Shell") >> "%Temp%\*.vbs" for %%j in ("%Folder%\*.ps1") do set /a Count += 1 echo. echo Processing . . . & echo. echo ==============================================================================& echo. for %%i in ("%Folder%\*.ps1") do (echo Processing File: %%i & echo X.Run ^"PowerShell -ExecutionPolicy ByPass -File ^" ^& chr^(34^) ^& %%i ^& chr^(34^), 0, True >> "%Temp%\*.vbs") & echo. echo ==============================================================================& echo. echo Processing COMPLETE. echo Processed %Count% PS1 files [ Check the Results for ERRORS ]. & echo. & del "%Temp%\*.vbs" & echo. echo ^>Press ANY Key to Exit and Restart Explorer . . . & pause > nul TaskKill /f /im explorer.exe & start explorer.exe
I amNOT
really up withVBS
.
The results show . . .
Code:The filename, directory name, or volume label syntax is incorrect. Processing . . . ============================================================================== Processing File: C:\System-Admin_Implementation\Change_This.ps1 The filename, directory name, or volume label syntax is incorrect. ============================================================================== Processing COMPLETE. Processed 1 PS1 files [ Check the Results for ERRORS ]. Could Not Find C:\Users\SYSTEM~1\AppData\Local\Temp\*.vbs >Press ANY Key to Exit and Restart Explorer . . .
I even tried these . . .
Code:PowerShell -Command "& {Set-ExecutionPolicy bypass}" -NoExit for %%f in ("%Folder%\*.ps1") do PowerShell -Command "& {%%f}" -NoExit
Code:set "Folder=C:\System-Admin_Implementation" PowerShell -NoProfile -ExecutionPolicy Bypass -Command "& {Start-Process PowerShell -ArgumentList '-NoProfile -ExecutionPolicy Bypass -File ""%Folder%\*.ps1""' -Verb RunAs}";
Thanks in advance.
Last edited by Paul Black; 05 Apr 2021 at 09:32.