无法使用“使用Powershell运行”选项执行PowerShell脚本 [英] Unable to execute PowerShell Script using 'Run with Powershell' option

查看:448
本文介绍了无法使用“使用Powershell运行”选项执行PowerShell脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个简单的Powershell脚本来

Hi I wrote a simple powershell script to:


  1. 创建指向站点的IE快捷方式

  2. 禁用Java控制面板的混合代码安全验证

  3. 添加一些站点作为可信站点

当我手动复制并将其粘贴到powershell中时,脚本运行良好。

但是,当我将其另存为.ps1文件并与Powershell一起运行时,它不会似乎没有执行(未进行更改)。

The script runs fine when I manually copy and paste it into powershell.
However, when I save it as a .ps1 file and 'Run with Powershell' - it doesn't seemingly execute (changes aren't made).

我尝试将执行策略更改为绕过,但仍然无法执行。

I tried changing execution policy to Bypass but it still does not execute.

是否有任何关于如何使用使用Powershell运行来执行.ps1脚本的想法?

Any thoughts on how I can get the .ps1 script to execute by using 'Run with Powershell'?

这是我的用户只需运行该脚本,而不必复制并粘贴到powershell中。

This is so my users can simply run this script without having to copy and paste into powershell.

谢谢,
Asif

Thank you, Asif

以下是完整的脚本供参考:

Here is the full script for reference:

& 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


推荐答案

原始答案在下面,但我已经找到了一种没有文件副本的更有效方法:

Leaving my original answer below but I've since found a more effective way without the file copy:

# 2>NUL & @powershell -nop -ep bypass "(gc '%~f0')-join[Environment]::NewLine|iex" && @EXIT /B 0

这将作为Powershell脚本的第一行包括在内,另存为 .cmd 文件。

This is to be included as your first line of the powershell script, saved as a .cmd file.

崩溃:

# 2>NUL &

这处理了文件的批处理部分,因此我们可以获取单击执​​行功能。由于不是文件名或命令,它会引发一个错误,而我们使用 2> NUL 忽略该错误,并跳至使用& 的下一个命令。

This handles the batch part of our file so we can get that click-execution functionality. Since # isn't a filename or command, it throws an error we ignore with 2>NUL and skip to the next command with &.

@powershell ...

这是我们对,抓取文件的内容( gc 获取内容)并执行它们( iex Invoke-Expression )。我们使用 @ ,因此命令不会回显到cli。

This is our call to powershell, grabbing the contents of the file (gc: Get-Content) and executing them (iex: Invoke-Expression). We use @ so the command isn't echoed to the cli.

&& EXIT /B 0

如果未引发任何错误,这将正常退出脚本。

This will exit the script gracefully if no errors were thrown.

如果您的唯一目标是为用户提供一个可单击快捷方式的链接以运行您的Powershell脚本,则可以通过粘贴您的代码来实现此标头下的脚本内容(另存为 myscript.cmd 或任何您想命名的内容):

If your only goal is to have a shortcut-clickable link for users to run your powershell script, you can accomplish that with this by pasting your script contents under this header (saved as myscript.cmd or whatever you want to name it):

::<#
@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脚本作为一个整体评论。

Simply put, it handles the execution policy and saves itself as a powershell script after replacing the batch-parts as a block comment.

这篇关于无法使用“使用Powershell运行”选项执行PowerShell脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆