我正在向我的团队分发一个PowerShell脚本。该脚本从Vsphere客户端获取一个IP地址,建立一个mstsc连接,并将其记录在共享文件中。
他们一使用脚本就知道了机器的IP地址。在此之后,他们总是倾向于直接使用mstsc,而不是运行PowerShell脚本。 (因为他们正在使用mstsc,我不知道他们是否经常使用VM。)
他们主要告诉我,运行PowerShell并不简单。
我对他们的懒惰感到厌恶。
是否有一种方法可以通过双击.ps1文件来使PowerShell脚本工作?
我正在向我的团队分发一个PowerShell脚本。该脚本从Vsphere客户端获取一个IP地址,建立一个mstsc连接,并将其记录在共享文件中。
他们一使用脚本就知道了机器的IP地址。在此之后,他们总是倾向于直接使用mstsc,而不是运行PowerShell脚本。 (因为他们正在使用mstsc,我不知道他们是否经常使用VM。)
他们主要告诉我,运行PowerShell并不简单。
我对他们的懒惰感到厌恶。
是否有一种方法可以通过双击.ps1文件来使PowerShell脚本工作?
当前回答
这是我默认以admin身份运行的脚本:
Powershell.exe -Command "& {Start-Process PowerShell.exe -Verb RunAs -ArgumentList '-File """%1"""'}"
你需要将它作为默认值粘贴到regedit中:
HKEY_CLASSES_ROOT\Microsoft.PowerShellScript.1\Shell\Open\Command
或者这里有一个脚本,将为您做:
$hive = [Microsoft.Win32.RegistryKey]::OpenBaseKey('ClassesRoot', 'Default')
$key = $hive.CreateSubKey('Microsoft.PowerShellScript.1\Shell\Open\Command')
$key.SetValue($null, 'Powershell.exe -Command "& {Start-Process PowerShell.exe -Verb RunAs -ArgumentList ''-File """%1"""''}"')
其他回答
从http://www.howtogeek.com/204166/how-to-configure-windows-to-work-with-powershell-scripts-more-easily:
为HKEY_CLASSES_ROOT\Microsoft.PowerShellScript设置默认值。1\Shell到0
与UNIX shar (shell存档)具有相同精神的解决方案。
你可以把你的powershell脚本放在一个扩展名为.cmd的文件中(而不是.ps1),并把它放在开头:
@echo off
Rem Make powershell read this file, skip a number of lines, and execute it.
Rem This works around .ps1 bad file association as non executables.
PowerShell -Command "Get-Content '%~dpnx0' | Select-Object -Skip 5 | Out-String | Invoke-Expression"
goto :eof
# Start of PowerShell script here
创建一个快捷方式,像这样的“目标”:
powershell.exe -command "& 'C:\A path with spaces\MyScript.ps1' -MyArguments blah"
或者如果你想让所有PS1文件都像VBS文件那样工作,你可以像这样编辑注册表:
HKEY_CLASSES_ROOT\Microsoft.PowerShellScript.1\Shell\open\command
编辑默认值,如下所示…
"C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" -noLogo -ExecutionPolicy unrestricted -file "%1"
然后你可以像你想的那样双击所有的。ps1文件。以我的拙见,是能够跳出框框的。
我将称之为“Powershell去阉割黑客”。LOL享受吧!
使用简单的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"'