Why are my PowerShell scripts not running?

前端 未结 9 1099
感情败类
感情败类 2020-12-07 08:33

I wrote a simple batch file as a PowerShell script, and I am getting errors when they run.

It\'s in a scripts directory in my path. This is the error I get:

9条回答
  •  北海茫月
    2020-12-07 08:42

    I was able to bypass this error by invoking PowerShell like this:

    powershell -executionpolicy bypass -File .\MYSCRIPT.ps1
    

    That is, I added the -executionpolicy bypass to the way I invoked the script.

    This worked on Windows 7 Service Pack 1. I am new to PowerShell, so there could be caveats to doing that that I am not aware of.

    [Edit 2017-06-26] I have continued to use this technique on other systems including Windows 10 and Windows 2012 R2 without issue.

    Here is what I am using now. This keeps me from accidentally running the script by clicking on it. When I run it in the scheduler I add one argument: "scheduler" and that bypasses the prompt.

    This also pauses the window at the end so I can see the output of PowerShell.

    if NOT "%1" == "scheduler" (
       @echo looks like you started the script by clicking on it.
       @echo press space to continue or control C to exit.
       pause
    )
    
    C:
    cd \Scripts
    
    powershell -executionpolicy bypass -File .\rundps.ps1
    
    set psexitcode=%errorlevel%
    
    if NOT "%1" == "scheduler" (
       @echo Powershell finished.  Press space to exit.
       pause
    )
    
    exit /b %psexitcode%
    

提交回复
热议问题