[英]Unable to execute PowerShell Script using 'Run with Powershell' option
嗨,我編寫了一個簡單的Powershell腳本來:
當我手動復制並將其粘貼到powershell中時,腳本運行良好。
但是,當我將其另存為.ps1文件並“使用Powershell運行”時,它似乎沒有執行(未進行更改)。
我嘗試將執行策略更改為“繞過”,但仍然無法執行。
關於如何通過使用“使用Powershell運行”來獲取.ps1腳本的任何想法?
這樣一來,我的用戶就可以簡單地運行此腳本,而不必復制並粘貼到powershell中。
謝謝阿西夫
這是完整的腳本供參考:
& powershell.exe -executionpolicy bypass -file C:\Users\AZahir\Desktop\ps2.ps1
$Shell = New-Object -ComObject ("WScript.Shell")
$ShortCut = $Shell.CreateShortcut($env:USERPROFILE + "\Desktop\Jacada.lnk")
$ShortCut.TargetPath = "C:\Program Files (x86)\Internet Explorer\iexplore.exe"
$ShortCut.Arguments = "http://facebook.com"
$ShortCut.WorkingDirectory = "C:\Program Files (x86)\Internet Explorer";
$ShortCut.WindowStyle = 1;
$ShortCut.IconLocation = "C:\Program Files (x86)\Internet Explorer\iexplore.exe"
$ShortCut.Save()
Add-Content -Path "$env:USERPROFILE\AppData\LocalLow\Sun\Java\Deployment\deployment.properties" -Value ('deployment.security.mixcode=DISABLE')
Set-Location "HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings"
Set-Location ZoneMap\Domains
New-Item bpoazusargdb01d
Set-Location bpoazusargdb01d
New-ItemProperty . -Name http -Value 2 -Type DWORD
Set-Location "HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings"
Set-Location ZoneMap\Domains
New-Item "172.30.1.3"
Set-Location "172.30.1.3"
New-ItemProperty . -Name http -Value 2 -Type DWORD
Set-Location "HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings"
Set-Location ZoneMap\Domains
New-Item "172.30.1.49"
Set-Location "172.30.1.49"
New-ItemProperty . -Name http -Value 2 -Type DWORD
Set-Location "HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings"
Set-Location ZoneMap\Domains
New-Item "172.30.1.89"
Set-Location "172.30.1.89"
New-ItemProperty . -Name http -Value 2 -Type DWORD
這樣一來,我的用戶就可以簡單地運行此腳本,而不必復制並粘貼到powershell中。
並排使用bat文件。
包括
PowerShell.exe -executionpolicy bypass -file "%~dp0ps2.ps1"
從您的powershel腳本中刪除以下內容。
& powershell.exe -executionpolicy bypass -file C:\Users\AZahir\Desktop\ps2.ps1
當用戶雙擊bat文件時,他們將運行ps2.ps1。
運行時我沒有看到錯誤。 它創建快捷方式和reg鍵。 以防萬一,如果您嘗試第二次運行它,它將產生錯誤,指出reg鍵存在。
使用"""
代替"
更多詳細信息"""
也是明智的做法如何在Powershell中使用$ ScriptDir和$ ScriptDir傳遞msi ArgumentList?
否則,如果用戶將該腳本放在具有空格的路徑(例如c:\\new folder\\
以下保留我的原始答案,但此后我發現了一種無需文件副本的更有效方法:
# 2>NUL & @powershell -nop -ep bypass "(gc '%~f0')-join[Environment]::NewLine|iex" && @EXIT /B 0
這將作為Powershell腳本的第一行包括在內,另存為.cmd
文件。
分解:
# 2>NUL &
這處理了文件的批處理部分,因此我們可以獲得點擊執行功能。 由於#
不是文件名或命令,因此它會引發錯誤,我們會使用2>NUL
忽略該錯誤,並使用&
跳至下一個命令。
@powershell ...
這是我們對powershell的調用,它獲取文件的內容( gc
: Get-Content
)並執行它們( iex
: Invoke-Expression
)。 我們使用@
因此命令不會回顯到cli。
&& EXIT /B 0
如果未引發任何錯誤,這將正常退出腳本。
如果您的唯一目標是為用戶提供一個可單擊快捷方式的鏈接以運行Powershell腳本,則可以通過將腳本內容粘貼到此標頭下(保存為myscript.cmd
或任何您想要的名稱)來實現此目的:
::<#
@ECHO OFF
REM https://stackoverflow.com/questions/3759456/create-a-executable-exe-file-from-powershell-script#answer-4629494
REM https://blogs.msdn.microsoft.com/zainala/2008/08/05/using-0-inside-the-batch-file-to-get-the-file-info/
SET "pwsh=%SYSTEMROOT%\System32\WindowsPowerShell\v1.0\powershell.exe"
SET "args=-NoProfile -NoLogo -ExecutionPolicy Bypass -Command"
SET "cmd="@(Get-Content -Path '%~f0') -replace '^^::'^|Set-Content -Path '%~dpn0.ps1';. '%~dpn0.ps1' %*""
%pwsh% %args% %cmd%
DEL "%~dpn0.ps1" /Q /F
EXIT
::#>
簡而言之,它將批處理部分替換為塊注釋后,它處理執行策略並將其自身保存為Powershell腳本。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.