簡體   English   中英

為什么刪除 powershell.exe -executionpolicy unrestricted 突然起作用但以前沒有

[英]Why removing powershell.exe -executionpolicy unrestricted suddenly working but wasn't before

我有一個調用 python 腳本的批處理文件 run.bat(如果它是 ps1 而不是 py 腳本,則同樣的問題)

run.bat 的內容

powershell.exe -executionpolicy unrestricted
powershell python .\aTest.py

直到今天批處理文件沒有調用 python 腳本,這一直運行良好。 命令窗口顯示以下消息:“嘗試新的跨平台 PowerShell https://aka/ms/pscore6”

我從網上發現我可以使用 -nologon 禁止顯示此消息,但除了刪除該消息之外沒有其他幫助。 我刪除了以下行 powershell.exe -executionpolicy unrestricted 並且腳本有效。 從上次成功到今天,沒有用戶權限更改或對系統進行任何更改。

為什么會發生這種情況讓我感到困惑,最初添加了 -executionPolicy 因為沒有它,腳本就無法運行。 現在情況正好相反,我怎樣才能弄清楚為什么會發生這種情況? 是什么原因造成的? 如果用戶是本地管理員組,是否有額外的 PS 標志有什么區別?

系統是 Windows 10 並且有一個本地管理員用戶。

powershell.exe -executionpolicy unrestricted

  • 這將進入一個交互式PowerShell 會話,該會話要求用戶以交互方式提交exit以退出會話,並且只有這樣批處理文件才能繼續執行。

  • -executionpolicy unrestricted適用於該會話(進程)。

  • 由於既沒有使用-File也沒有使用-Command參數(后者可能是隱含的,僅通過傳遞命令),PowerShell 會發出一個“徽標”,即版權消息

     Windows PowerShell Copyright (C) Microsoft Corporation. All rights reserved.
    • 最新版本的 Windows PowerShell 會在此消息中附加一條消息,宣傳跨平台按需安裝后續版本PowerShell (Core) v6+ ,以便您看到以下內容:

       Windows PowerShell Copyright (C) Microsoft Corporation. All rights reserved. Try the new cross-platform PowerShell https://aka.ms/pscore6
    • 使用-NoLogo抑制此輸出; 但是,如上所述,如果您通過將腳本文件路徑 ( .ps1 ) 傳遞給-File ( -f ) 或通過(可能在位置上)傳遞 command( s) 到-Command ( -c )。 但是,如果將 -NoLogo 與 -File 與-NoExit結合使用-NoLogo -File確實需要它。

powershell python .\aTest.py

一般來說,執行 Python 腳本不需要涉及 PowerShell - 直接從批處理文件中調用python .\aTest.py應該可以。

只有當對 Python 腳本的調用依賴於通過 PowerShell 的配置文件(特別是通過當前用戶的$PROFILE文件) 執行的初始化時,才需要通過 PowerShell 進行調用。

  • 順便說一句:如果您想禁止加載任何配置文件,請使用-NoProfile CLI選項,這通常是正確的做法,以確保可預測的執行環境並避免不必要的處理。

如果確實需要通過 PowerShell 調用,則有效執行策略不適用於調用Python腳本 - 它僅適用於PowerShell腳本 ( *.ps1 ); 如果配置文件碰巧調用了 PowerShell 腳本,請使用以下命令:

powershell.exe -ExecutionPolicy Bypass -Command python .\aTest.py

注意: Bypass會繞過有關.ps1腳本執行的所有檢查,而Restricted在執行從 web下載的腳本之前仍會提示

注意:在powershell.exeWindows PowerShell CLI 中,明確使用-Command ( -c ) 參數名稱不是必需的; 但是, PowerShell (Core) 6+ CLI pwsh.exe現在確實需要它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM