我正在向我的团队分发一个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文件。

其他回答

我在几年前写了这个(以管理员权限运行):

<#
.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 {}
}

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

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

RunMyPowershellScript.bat

 start powershell -command "& '.\MyPowershellScript.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'

您可以将ps1文件的默认文件关联设置为powershell.exe,这将允许您通过双击它来执行powershell脚本。

在Windows 10中,

右键单击ps1文件 单击“打开” 点击选择另一个应用程序 在弹出窗口中,选择“更多应用程序” 滚动到底部,选择“在这台PC上寻找另一个应用程序”。 浏览并选择C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe。 列表项

这将改变文件关联,ps1文件将通过双击它们来执行。通过将notepad.exe设置为默认应用程序,您可以将其更改为默认行为。