我正在向我的团队分发一个PowerShell脚本。该脚本从Vsphere客户端获取一个IP地址,建立一个mstsc连接,并将其记录在共享文件中。
他们一使用脚本就知道了机器的IP地址。在此之后,他们总是倾向于直接使用mstsc,而不是运行PowerShell脚本。 (因为他们正在使用mstsc,我不知道他们是否经常使用VM。)
他们主要告诉我,运行PowerShell并不简单。
我对他们的懒惰感到厌恶。
是否有一种方法可以通过双击.ps1文件来使PowerShell脚本工作?
我正在向我的团队分发一个PowerShell脚本。该脚本从Vsphere客户端获取一个IP地址,建立一个mstsc连接,并将其记录在共享文件中。
他们一使用脚本就知道了机器的IP地址。在此之后,他们总是倾向于直接使用mstsc,而不是运行PowerShell脚本。 (因为他们正在使用mstsc,我不知道他们是否经常使用VM。)
他们主要告诉我,运行PowerShell并不简单。
我对他们的懒惰感到厌恶。
是否有一种方法可以通过双击.ps1文件来使PowerShell脚本工作?
当前回答
您可以使用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.
其他回答
导航REGEDIT到 HKEY_LOCAL_MACHINE \ SOFTWARE \ \ Microsoft.PowerShellScript.1 \壳类 在右侧窗格中双击“(默认)” 删除现有的“Open”值(启动Notepad),输入“0”(为零,直接启动Powershell)。
如果希望再次使用“记事本”作为默认值,则恢复该值。
或者如果你想让所有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享受吧!
我在几年前写了这个(以管理员权限运行):
<#
.SYNOPSIS
Change the registry key in order that double-clicking on a file with .PS1 extension
start its execution with PowerShell.
.DESCRIPTION
This operation bring (partly) .PS1 files to the level of .VBS as far as execution
through Explorer.exe is concern.
This operation is not advised by Microsoft.
.NOTES
File Name : ModifyExplorer.ps1
Author : J.P. Blanc - jean-paul_blanc@silogix-fr.com
Prerequisite: PowerShell V2 on Vista and later versions.
Copyright 2010 - Jean Paul Blanc/Silogix
.LINK
Script posted on:
http://www.silogix.fr
.EXAMPLE
PS C:\silogix> Set-PowAsDefault -On
Call Powershell for .PS1 files.
Done!
.EXAMPLE
PS C:\silogix> Set-PowAsDefault
Tries to go back
Done!
#>
function Set-PowAsDefault
{
[CmdletBinding()]
Param
(
[Parameter(mandatory=$false, ValueFromPipeline=$false)]
[Alias("Active")]
[switch]
[bool]$On
)
begin
{
if ($On.IsPresent)
{
Write-Host "Call PowerShell for .PS1 files."
}
else
{
Write-Host "Try to go back."
}
}
Process
{
# Text Menu
[string]$TexteMenu = "Go inside PowerShell"
# Text of the program to create
[string] $TexteCommande = "%systemroot%\system32\WindowsPowerShell\v1.0\powershell.exe -Command ""&'%1'"""
# Key to create
[String] $clefAModifier = "HKLM:\SOFTWARE\Classes\Microsoft.PowerShellScript.1\Shell\Open\Command"
try
{
$oldCmdKey = $null
$oldCmdKey = Get-Item $clefAModifier -ErrorAction SilentlyContinue
$oldCmdValue = $oldCmdKey.getvalue("")
if ($oldCmdValue -ne $null)
{
if ($On.IsPresent)
{
$slxOldValue = $null
$slxOldValue = Get-ItemProperty $clefAModifier -Name "slxOldValue" -ErrorAction SilentlyContinue
if ($slxOldValue -eq $null)
{
New-ItemProperty $clefAModifier -Name "slxOldValue" -Value $oldCmdValue -PropertyType "String" | Out-Null
New-ItemProperty $clefAModifier -Name "(default)" -Value $TexteCommande -PropertyType "ExpandString" | Out-Null
Write-Host "Done !"
}
else
{
Write-Host "Already done!"
}
}
else
{
$slxOldValue = $null
$slxOldValue = Get-ItemProperty $clefAModifier -Name "slxOldValue" -ErrorAction SilentlyContinue
if ($slxOldValue -ne $null)
{
New-ItemProperty $clefAModifier -Name "(default)" -Value $slxOldValue."slxOldValue" -PropertyType "String" | Out-Null
Remove-ItemProperty $clefAModifier -Name "slxOldValue"
Write-Host "Done!"
}
else
{
Write-Host "No former value!"
}
}
}
}
catch
{
$_.exception.message
}
}
end {}
}
从http://www.howtogeek.com/204166/how-to-configure-windows-to-work-with-powershell-scripts-more-easily:
为HKEY_CLASSES_ROOT\Microsoft.PowerShellScript设置默认值。1\Shell到0
在我的子文件夹中放一个简单的.cmd文件和同名的.ps1文件,例如,一个名为“foobar”的脚本会有“foobar. exe”。Ps1”和“foobar.cmd”。所以要运行.ps1,我所要做的就是从资源管理器中单击.cmd文件或从命令提示符中运行.cmd。我使用相同的基本名称,因为.cmd文件将自动使用自己的名称查找.ps1。
::====================================================================
:: Powershell script launcher
::=====================================================================
:MAIN
@echo off
for /f "tokens=*" %%p in ("%~p0") do set SCRIPT_PATH=%%p
pushd "%SCRIPT_PATH%"
powershell.exe -sta -c "& {.\%~n0.ps1 %*}"
popd
set SCRIPT_PATH=
pause
pushd/popd允许您从命令提示符启动.cmd文件,而不必更改到脚本所在的特定目录。它将改变到脚本目录,然后当完成返回到原来的目录。
如果您希望命令窗口在脚本结束时消失,也可以取消暂停。
如果我的.ps1脚本有参数,我会使用. net Forms使用GUI提示来提示它们,但也要使脚本足够灵活,以便在我想要传递参数时接受参数。这样我就可以从资源管理器中双击它,而不必知道参数的详细信息,因为它会用列表框或其他形式询问我需要什么。