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

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

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

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

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


当前回答

在Windows 10中,你可能还想删除Windows资源管理器对文件扩展名关联的覆盖:

HKEY_CURRENT_USER软件\ Microsoft \ Windows \ CurrentVersion探索者FileExts \ \ UserChoice。ps1

除了HKEY_CLASSES_ROOT\Microsoft.PowerShellScript之外。1\Shell\open\命令的变化在其他答案中提到。

参见https://stackoverflow.com/a/2697804/1360907

其他回答

我同意设置系统设置可能有点多,但需要硬编码路径的快捷方式并不理想。bat文件实际上很好地解决了这个问题

RunMyPowershellScript.bat

 start powershell -command "& '.\MyPowershellScript.ps1' -MyArguments blah"

现在可以双击此批处理文件,可以轻松地为批处理文件创建快捷方式,并且可以将脚本部署到任何文件夹。

您可以使用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.

使用简单的PowerShell命令在注册表中设置此参数;

New-PSDrive -Name HKCR -PSProvider Registry -Root HKEY_CLASSES_ROOT
Set-ItemProperty -Path "HKCR:\Microsoft.PowerShellScript.1\Shell\open\command" -name '(Default)' -Value '"C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" -noLogo -ExecutionPolicy unrestricted -file "%1"'

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

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

您需要调整注册表。 首先,为HKEY_CLASSES_ROOT配置一个PSDrive,因为默认情况下没有设置。它的命令是:

New-PSDrive HKCR Registry HKEY_CLASSES_ROOT

现在您可以在HKEY_CLASSES_ROOT中浏览和编辑注册表项和值,就像在常规HKCU和HKLM PSDrives中一样。

配置双击直接启动PowerShell脚本:

Set-ItemProperty HKCR:\Microsoft.PowerShellScript.1\Shell '(Default)' 0

在PowerShell ISE中配置双击打开PowerShell脚本:

Set-ItemProperty HKCR:\Microsoft.PowerShellScript.1\Shell '(Default)' 'Edit'

要恢复默认值(设置双击打开记事本中的PowerShell脚本):

Set-ItemProperty HKCR:\Microsoft.PowerShellScript.1\Shell '(Default)' 'Open'