Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
319 views
in Technique[技术] by (71.8m points)

windows - Unable to execute PowerShell Script using 'Run with Powershell' option

Hi I wrote a simple powershell script to:

  1. Create a IE shortcut to a site
  2. Disable Mixed Code Security Verification for Java control panel
  3. Add a few sites as trusted sites

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.

enter image description here

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

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

Thank you, Asif

Here is the full script for reference:

& powershell.exe -executionpolicy bypass -file C:UsersAZahirDesktopps2.ps1

$Shell = New-Object -ComObject ("WScript.Shell")
$ShortCut = $Shell.CreateShortcut($env:USERPROFILE + "DesktopJacada.lnk")
$ShortCut.TargetPath = "C:Program Files (x86)Internet Exploreriexplore.exe"
$ShortCut.Arguments = "http://facebook.com"
$ShortCut.WorkingDirectory = "C:Program Files (x86)Internet Explorer";
$ShortCut.WindowStyle = 1;
$ShortCut.IconLocation = "C:Program Files (x86)Internet Exploreriexplore.exe"
$ShortCut.Save()

Add-Content -Path "$env:USERPROFILEAppDataLocalLowSunJavaDeploymentdeployment.properties" -Value ('deployment.security.mixcode=DISABLE')


Set-Location "HKCU:SoftwareMicrosoftWindowsCurrentVersionInternet Settings"
Set-Location ZoneMapDomains
New-Item bpoazusargdb01d
Set-Location bpoazusargdb01d
New-ItemProperty . -Name http -Value 2 -Type DWORD

Set-Location "HKCU:SoftwareMicrosoftWindowsCurrentVersionInternet Settings"
Set-Location ZoneMapDomains
New-Item "172.30.1.3"
Set-Location "172.30.1.3"
New-ItemProperty . -Name http -Value 2 -Type DWORD

Set-Location "HKCU:SoftwareMicrosoftWindowsCurrentVersionInternet Settings"
Set-Location ZoneMapDomains
New-Item "172.30.1.49"
Set-Location "172.30.1.49"
New-ItemProperty . -Name http -Value 2 -Type DWORD

Set-Location "HKCU:SoftwareMicrosoftWindowsCurrentVersionInternet Settings"
Set-Location ZoneMapDomains
New-Item "172.30.1.89"
Set-Location "172.30.1.89"
New-ItemProperty . -Name http -Value 2 -Type DWORD
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

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

Breakdown:

# 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 ...

This is our call to , 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.


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%System32WindowsPowerShellv1.0powershell.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
::#>

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...