Batch File - PowerShell -ExecutionPolicy ByPass -File

Page 1 of 2 12 LastLast

  1. Posts : 18,044
    Win 10 Pro 64-bit v1909 - Build 18363 Custom ISO Install
       #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 am NOT really up with VBS.

    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.
      My Computer


  2. Posts : 3,453
       #2

    Code:
     C:\Users\SYSTEM~1\AppData\Local\Temp\*.vbs
    cannot be an Object in VBS - why not do everything in PS?

    I'm not clued up on CMD (isn't that what Moses used on his tablets when he came down from the mountain?)
      My Computer


  3. Posts : 18,044
    Win 10 Pro 64-bit v1909 - Build 18363 Custom ISO Install
    Thread Starter
       #3

    Superfly said:
    Code:
     C:\Users\SYSTEM~1\AppData\Local\Temp\*.vbs

    cannot be an Object in VBS - why not do everything in PS?

    I'm not clued up on CMD (isn't that what Moses used on his tablets when he came from the mountain?)

    I want to do this via Batch Script!
      My Computer


  4. Posts : 7,606
    Windows 10 Home 20H2
       #4

    @Paul Black, I was the one who advised you to use VBScript last year in this post: Adapting .REG Files.

    Now I know that VBScript is totally unnecessary. You can simply use the following command in a batch script:

    Call PowerShell -ExecutionPolicy ByPass -file "%%i"
      My Computer


  5. Posts : 18,044
    Win 10 Pro 64-bit v1909 - Build 18363 Custom ISO Install
    Thread Starter
       #5

    Matthew Wai said:
    Now I know that VBScript is totally unnecessary. You can simply use the following command in a batch script:

    Call PowerShell -ExecutionPolicy ByPass -file "%%i"

    Thanks Matthew.

    Did you get it to run, because my screen just flashes?
      My Computer


  6. Posts : 7,606
    Windows 10 Home 20H2
       #6

    Paul Black said:
    I have a Folder with .PS1 files that I want to iterate through and run one after the other.
    One line of command will do the job.
    Code:
    For %%i in ("C:\System-Admin_Implementation\*.ps1") Do (Call PowerShell -ExecutionPolicy ByPass -file "%%i")

    No VBScript is needed.

    Paul Black said:
    Did you get it to run, because my screen just flashes?
    Yes, I did.
      My Computer


  7. Posts : 3,453
       #7

    Still dunno how the *.vbs fits in - looks they are being deleted then attempted to access the non-existent stuff .

    If using WShell.Run merely to run execute silently try the PS "&" command

    eg:

    Code:
     & (Join-Path $env:SystemRoot -ChildPath "\Path\File.vbs") | Out-Null
      My Computer


  8. Posts : 7,606
    Windows 10 Home 20H2
       #8

    Superfly said:
    Still dunno how the *.vbs fits in
    *.vbs is wrong.
    Last year, in the following post, I used ✱.vbs, which worked.

    Adapting .REG Files.

    Note that .vbs and *.vbs are two different things.
      My Computer


  9. Posts : 18,044
    Win 10 Pro 64-bit v1909 - Build 18363 Custom ISO Install
    Thread Starter
       #9

    Matthew Wai said:
    One line of command will do the job.
    Code:
    For %%i in ("C:\System-Admin_Implementation\*.ps1") Do (Call PowerShell -ExecutionPolicy ByPass -file "%%i")

    Thanks Matthew.

    I did get it work and was just about to delete my above post when you posted. I had left a line of code in that needed to be taken out from when I was testing.

    Last edited by Paul Black; 05 Apr 2021 at 10:33.
      My Computer


  10. Posts : 3,453
       #10

    Matthew Wai said:
    *.vbs is wrong.
    Last year, in the following post, I used ✱.vbs, which worked.

    Adapting .REG Files.

    Note that .vbs and *.vbs are two different things.
    OIC.. like a Capital Asterix - never seen it before - what's the difference?
      My Computer


 

  Related Discussions
Our Sites
Site Links
About Us
Windows 10 Forums is an independent web site and has not been authorized, sponsored, or otherwise approved by Microsoft Corporation. "Windows 10" and related materials are trademarks of Microsoft Corp.

© Designer Media Ltd
All times are GMT -5. The time now is 00:52.
Find Us




Windows 10 Forums