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

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

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

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

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


当前回答

如果你熟悉高级Windows管理,那么你可以使用这个ADM包(说明包括在那个页面上),并允许通过这个模板和本地GPO双击后运行PowerShell脚本。在此之后,您可以简单地将与.ps1文件类型相关联的默认程序更改为PowerShell .exe(使用搜索,它是相当隐藏的),并且您已经准备好双击运行PowerShell脚本。

否则,我建议坚持使用其他建议,因为使用这些管理工具可能会搞乱整个系统。

我认为默认设置太严格了。如果有人设法在你的计算机上放置一些恶意代码,那么他/她也能够绕过这个限制(将其包装成.cmd文件或.exe,或带有快捷方式的技巧),而它最终所实现的只是阻止你以简单的方式运行你所编写的脚本。

其他回答

您需要调整注册表。 首先,为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文件都像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.exe -command "& 'C:\A path with spaces\MyScript.ps1' -MyArguments blah"

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

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