我正在向我的团队分发一个PowerShell脚本。该脚本从Vsphere客户端获取一个IP地址,建立一个mstsc连接,并将其记录在共享文件中。

他们一使用脚本就知道了机器的IP地址。在此之后,他们总是倾向于直接使用mstsc,而不是运行PowerShell脚本。 (因为他们正在使用mstsc,我不知道他们是否经常使用VM。)

他们主要告诉我,运行PowerShell并不简单。

我对他们的懒惰感到厌恶。

是否有一种方法可以通过双击.ps1文件来使PowerShell脚本工作?


当前回答

你可能不想这样做,但一个简单的方法就是创建一个.BAT文件,然后把你的命令放进去:

powershell ./generate-strings-table-en.ps1
powershell ./generate-conjoined-tables-it.ps1

然后双击BAT文件。

其他回答

这是基于KoZm0kNoT的答案。我修改了它,让它能跨驱动器工作。

@echo off
pushd "%~d0"
pushd "%~dp0"
powershell.exe -sta -c "& {.\%~n0.ps1 %*}"
popd
popd

如果用户的cwd在不同的驱动器上,这两个pushd/popd是必要的。如果没有外部设置,带有脚本的驱动器上的cwd将会丢失。

您可以使用Windows的“SendTo”功能,使运行PS1脚本更容易。使用此方法,您可以右键单击 一个PS1脚本并执行。这并没有完全回答OP问题,但已经很接近了。希望这对其他人有用。顺便说一句. .这对 各种各样的其他任务。

Locate / Search for Powershell.exe Right click on Powershell.exe and choose Open File Location Right click on Powershell.exe and choose Create Shortcut. Temporarily save some place like your desktop You might want to open as Admin by default. Select Shortcut > Properties > Advanced > Open As Admin Open the Sendto folder. Start > Run > Shell:Sendto Move the Powershell.exe shortcut to the Sendto folder You should now be able to right click on a PS1 script. Right Click on a PS1 file, Select the SendTo context option > Select the Powershell shortcut Your PS1 script should execute.

我在Windows 10和powershell 5.1上使用了这个方法:

右键单击.ps1文件 打开…… 选择另一个应用程序 将powershell.exe的位置复制到地址栏(默认情况下不会显示windows文件夹),即C:\Windows\System32\WindowsPowerShell\v1.0 选择powershell.exe 选择“总是使用此应用程序打开。ps1文件” 单击OK

创建一个快捷方式,像这样的“目标”:

powershell.exe -command "& 'C:\A path with spaces\MyScript.ps1' -MyArguments blah"

我使用了这个(只需要运行一次);还要确保你有执行的权利:

从PowerShell提升权限:

Set-ExecutionPolicy=RemoteSigned

然后从一个bat文件:

-----------------------------------------

 ftype Microsoft.PowerShellScript.1="C:\WINDOWS\system32\windowspowershell\v1.0\powershell.exe" -noexit ^&'%%1'

 assoc .ps1=Microsoft.PowerShellScript.1

-----------------------------------------
auto exit: remove -noexit 

瞧;双击*。Ps1会执行它。