Sam*_*abu 119 powershell powershell-2.0
我正在向我的团队分发Powershell脚本.该脚本是从Vsphere客户端获取IP地址,进行mstsc连接并将其记录在共享文件中.
他们使用脚本的那一刻他们就知道了机器的IP地址.之后,他们总是倾向于直接使用mstsc而不是运行powershell脚本.(因为他们正在使用mstsc,我无法知道他们是否经常使用VM)
主要是他们告诉我运行PowerShell并不是直截了当的.
我因他们的懒惰而生病.
有没有办法通过双击.ps1文件使PowerShell脚本工作?
Dav*_*ant 117
创建一个类似于"目标"的快捷方式:
powershell.exe -command "& 'C:\A path with spaces\MyScript.ps1' -MyArguments blah"
Run Code Online (Sandbox Code Playgroud)
小智 70
或者,如果您希望所有PS1文件都像VBS文件一样工作,您可以像这样编辑注册表:
HKEY_CLASSES_ROOT\Microsoft.PowerShellScript.1\Shell\open\command
Run Code Online (Sandbox Code Playgroud)
编辑默认值是这样的......
"C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" -noLogo -ExecutionPolicy unrestricted -file "%1"
Run Code Online (Sandbox Code Playgroud)
然后,您可以像您希望的那样双击所有.PS1文件.以我的拙见,能够开箱即用.
我打算称之为"Powershell De-castration Hack".大声笑享受!
Jef*_*cks 19
请注意,PowerShell的安全功能之一是用户无法通过双击启动脚本.如果修改此设置,请务必小心.另一种方法是打包脚本.像PrimalScript这样的编辑可以做到这一点.用户仍然需要安装PowerShell,但他们可以双击exe.听起来你的团队需要一点教育.
JPB*_*anc 16
我几年前写过这个(用管理员权限运行):
<#
.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 {}
}
Run Code Online (Sandbox Code Playgroud)
Joh*_*ulé 16
与 UNIX shar(shell 存档)具有相同精神的解决方案。
.cmd您可以将 powershell 脚本放入扩展名为(而不是)的文件中.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
Run Code Online (Sandbox Code Playgroud)
小智 13
我同意设置系统设置可能有点多,但需要硬编码路径的快捷方式并不理想.bat文件实际上很好地解决了这个问题
RunMyPowershellScript.bat
start powershell -command "& '.\MyPowershellScript.ps1' -MyArguments blah"
Run Code Online (Sandbox Code Playgroud)
现在可以双击此批处理文件,可以轻松地为批处理文件创建快捷方式,并且可以将脚本部署到任何文件夹.
Fra*_*cis 13
我尝试了这个问题的最上面的答案,但遇到了错误消息。然后我在这里找到了答案:
对我来说效果很好的是使用这个解决方案:
powershell -ExecutionPolicy Bypass -File script.ps1
Run Code Online (Sandbox Code Playgroud)
您可以将其粘贴到 .bat 文件中并双击它。
Ale*_*ger 12
你需要调整注册表.首先,为HKEY_CLASSES_ROOT配置一个PSDrive,因为默认情况下没有设置它.对此的命令是:
New-PSDrive HKCR Registry HKEY_CLASSES_ROOT
Run Code Online (Sandbox Code Playgroud)
现在,您可以在HKEY_CLASSES_ROOT中导航和编辑注册表项和值,就像在常规HKCU和HKLM PSDrives中一样.
要配置双击以直接启动PowerShell脚本:
Set-ItemProperty HKCR:\Microsoft.PowerShellScript.1\Shell '(Default)' 0
Run Code Online (Sandbox Code Playgroud)
要配置双击以在PowerShell ISE中打开PowerShell脚本,请执行以下操作:
Set-ItemProperty HKCR:\Microsoft.PowerShellScript.1\Shell '(Default)' 'Edit'
Run Code Online (Sandbox Code Playgroud)
要恢复默认值(设置双击以在记事本中打开PowerShell脚本):
Set-ItemProperty HKCR:\Microsoft.PowerShellScript.1\Shell '(Default)' 'Open'
Run Code Online (Sandbox Code Playgroud)
viz*_*zmi 12
这适用于Windows 10和PowerShell 5.1:
简单的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"'
Run Code Online (Sandbox Code Playgroud)
你可以设置的默认文件关联ps1的文件是powershell.exe,这将允许您通过双击它执行PowerShell脚本。
在 Windows 10 中,
ps1文件Open withChoose another appMore appsLook for another app on this PC。C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe。这将更改文件关联,ps1文件将通过双击它们来执行。您可以通过设置notepad.exe为默认应用程序将其更改回其默认行为。
小智 6
如果您希望再次使用记事本作为默认值,请还原该值.
将一个简单的 .cmd 文件与我的 .ps1 文件同名放在我的子文件夹中,因此,例如,名为“foobar”的脚本将具有“foobar.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
Run Code Online (Sandbox Code Playgroud)
pushd/popd 允许您从命令提示符启动 .cmd 文件,而无需更改到脚本所在的特定目录。它将更改为脚本目录,然后完成后返回原始目录。
如果您希望在脚本完成时命令窗口消失,您也可以取消暂停。
如果我的 .ps1 脚本有参数,我会使用 .NET Forms 使用 GUI 提示来提示它们,但如果我想传递参数,也会使脚本足够灵活以接受参数。这样我就可以从资源管理器中双击它而不必知道参数的详细信息,因为它会询问我需要什么,包括列表框或其他形式。
| 归档时间: |
|
| 查看次数: |
125728 次 |
| 最近记录: |