在不先运行 VB 脚本的情况下运行 Powershell 脚本的问题

德雷克

我正在寻找一种解决方案,将快捷方式或程序固定到带有 PS 的 win 10 中的任务。在 Windows 10 中使用 PS找到了Pin 程序到任务栏VB 脚本有效,

If WScript.Arguments.Count < 1 Then WScript.Quit
'----------------------------------------------------------------------
Set objFSO = CreateObject("Scripting.FileSystemObject")
objFile    = WScript.Arguments.Item(0)
sKey1      = "HKCU\Software\Classes\*\shell\{:}\\"
sKey2      = Replace(sKey1, "\\", "\ExplorerCommandHandler")
'----------------------------------------------------------------------
With WScript.CreateObject("WScript.Shell")
    KeyValue = .RegRead("HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer" & _
    "\CommandStore\shell\Windows.taskbarpin\ExplorerCommandHandler")

   .RegWrite sKey2, KeyValue, "REG_SZ"

    With WScript.CreateObject("Shell.Application")
        With .Namespace(objFSO.GetParentFolderName(objFile))
            With .ParseName(objFSO.GetFileName(objFile))
                .InvokeVerb("{:}")
            End With
        End With
    End With

    .Run("Reg.exe delete """ & Replace(sKey1, "\\", "") & """ /F"), 0, True
End With
'----------------------------------------------------------------------

我可以从 PS 调用 VB 脚本,但一个乐于助人的人将脚本转换为 PS

Param($Target)

$KeyPath1  = "HKCU:\SOFTWARE\Classes"
$KeyPath2  = "*"
$KeyPath3  = "shell"
$KeyPath4  = "{:}"
$ValueName = "ExplorerCommandHandler"
$ValueData =
    (Get-ItemProperty `
        ("HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\" + `
            "CommandStore\shell\Windows.taskbarpin")
    ).ExplorerCommandHandler

$Key2 = (Get-Item $KeyPath1).OpenSubKey($KeyPath2, $true)
$Key3 = $Key2.CreateSubKey($KeyPath3, $true)
$Key4 = $Key3.CreateSubKey($KeyPath4, $true)
$Key4.SetValue($ValueName, $ValueData)

$Shell = New-Object -ComObject "Shell.Application"
$Folder = $Shell.Namespace((Get-Item $Target).DirectoryName)
$Item = $Folder.ParseName((Get-Item $Target).Name)
$Item.InvokeVerb("{:}")

$Key3.DeleteSubKey($KeyPath4)
if ($Key3.SubKeyCount -eq 0 -and $Key3.ValueCount -eq 0) {
    $Key2.DeleteSubKey($KeyPath3)
}

但是,除非 VB 脚本至少运行过一次,否则此 PS 脚本不会运行。有没有办法让 PS 脚本工作而不必运行 VB 脚本?

我在尝试运行 PS 脚本而不运行 VB 脚本时遇到的错误:

You cannot call a method on a null-valued expression.
At \\server\Utilities\TaskbarPin.ps1:41 char:5
+     $Key3 = $Key2.CreateSubKey($KeyPath3, $true)
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

You cannot call a method on a null-valued expression.
At \\server\Utilities\TaskbarPin.ps1:42 char:5
+     $Key4 = $Key3.CreateSubKey($KeyPath4, $true)
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

You cannot call a method on a null-valued expression.
At \\server\Utilities\TaskbarPin.ps1:43 char:5
+     $Key4.SetValue($KeyValue, $ValueData)
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

You cannot call a method on a null-valued expression.
At \\server\Utilities\TaskbarPin.ps1:50 char:5
+     $Key3.DeleteSubKey($KeyPath4)
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

使用 VB 脚本一次完成任务后,我没有收到错误消息。

变得

你不应该受到这种方式的影响。代码按设计工作,但您必须完全调用 exe 路径。

我只是将它转换为一个函数,它在没有其他依赖项的情况下成功。

Function Add-AppToTaskbar
{
    [cmdletbinding()]

    Param
    (
        [string]$Target
    )

    $KeyPath1  = "HKCU:\SOFTWARE\Classes"
    $KeyPath2  = "*"
    $KeyPath3  = "shell"
    $KeyPath4  = "{:}"
    $ValueName = "ExplorerCommandHandler"
    $ValueData =
        (Get-ItemProperty `
            ("HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\" + `
                "CommandStore\shell\Windows.taskbarpin")
        ).ExplorerCommandHandler

    $Key2 = (Get-Item $KeyPath1).OpenSubKey($KeyPath2, $true)
    $Key3 = $Key2.CreateSubKey($KeyPath3, $true)
    $Key4 = $Key3.CreateSubKey($KeyPath4, $true)
    $Key4.SetValue($ValueName, $ValueData)

    $Shell = New-Object -ComObject "Shell.Application"
    $Folder = $Shell.Namespace((Get-Item $Target).DirectoryName)
    $Item = $Folder.ParseName((Get-Item $Target).Name)
    $Item.InvokeVerb("{:}")

    $Key3.DeleteSubKey($KeyPath4)
    if ($Key3.SubKeyCount -eq 0 -and $Key3.ValueCount -eq 0) 
    {$Key2.DeleteSubKey($KeyPath3)}
}

Add-AppToTaskbar -Target 'C:\Program Files (x86)\Google\Chrome\Application\chrome.exe'

顺便说一句,这些固定的东西存在于你系统的两个地方:

这里:

$env:USERPROFILE\AppData\Roaming\Microsoft\Internet Explorer\Quick Launch\User Pinned\TaskBar

注册表:

HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Taskband

两者都是必需的。

根据 OP 的评论更新

我只是在本地和远程运行了这个,两者都成功了。请参阅下面的结果。我使用的本地主机 - WS2012R2 设置为工作站角色 我的实验室中没有任何 W10 系统。较早的测试是在本地 W10 主机上进行的。

在控制台主机、ISE 和 VSCode 中执行。

PS C:\Windows\system32> $env:COMPUTERNAME
LabWS01

# PS Version
PS C:\Windows\system32> $PSVersionTable

Name                           Value
----                           -----
PSVersion                      4.0
WSManStackVersion              3.0
SerializationVersion           1.1.0.1
CLRVersion                     4.0.30319.42000
BuildVersion                   6.3.9600.18968
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0}
PSRemotingProtocolVersion      2.2


# the current user profile pinned location filtered for notepad*

PS C:\Windows\system32> Get-ChildItem -Path "$env:USERPROFILE\AppData\Roaming\Microsoft\Internet Explorer\Quick Launch\User Pinned\TaskBar\Notepad*"

# Tested path to remote share

PS C:\Windows\system32> Test-path -Path '\\Server\ShareName\Add-AppToTaskbar.ps1'
True

# Ran the script from that remote share

PS C:\Windows\system32> \\Server\ShareName\Add-AppToTaskbar.ps1 'c:\windows\notepad.exe'

或者这样……

Start-process -FilePath Powershell -ArgumentList '\\Server\ShareName\Add-AppToTaskbar.ps1 -Target C:\Windows\notepad.exe'

# Review pinned item location, filtered for notepad*

PS C:\Windows\system32> Get-ChildItem -Path "$env:USERPROFILE\AppData\Roaming\Microsoft\Internet Explorer\Quick Launch\User Pinned\TaskBar\Notepad*"


    Directory: C:\Users\Labuser001\AppData\Roaming\Microsoft\Internet Explorer\Quick Launch\User Pinned\TaskBar


Mode                LastWriteTime     Length Name
----                -------------     ------ ----
-a---          8/9/2018   8:48 PM        791 Notepad.lnk

快捷方式显示固定到任务栏。

所以,这听起来很环保。现在,如果此问题继续存在,您可以使用 GPO 固定应用程序。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

在路径中有空格的情况下从Excel运行Powershell脚本

来自分类Dev

如何在没有终端窗口的情况下运行PowerShell脚本?

来自分类Dev

如何在没有SDK的情况下运行PowerShell脚本文件?

来自分类Dev

在不继承变量和作用域的情况下从另一个运行一个PowerShell脚本

来自分类Dev

在VB.NET中运行Powershell脚本时出错

来自分类Dev

在不输入amm的情况下运行Ammonite scala脚本

来自分类Dev

在没有crontab的情况下运行php脚本

来自分类Dev

在没有安装库的情况下运行python脚本

来自分类Dev

如何在不终止的情况下运行bash脚本

来自分类Dev

在名称前没有./的情况下运行脚本

来自分类Dev

PHP:在不访问网页的情况下运行PHP脚本?

来自分类Dev

在不运行构建脚本的情况下进行构建

来自分类Dev

在不使用crontab的情况下运行php脚本

来自分类Dev

在不声明解释器的情况下运行python脚本

来自分类Dev

在不输入amm的情况下运行Ammonite scala脚本

来自分类Dev

如何在不终止的情况下运行bash脚本

来自分类Dev

PowerShell脚本上下文菜单项可在不退出的情况下以管理员身份运行脚本

来自分类Dev

在IPython中使用魔术“ ed”而不先运行脚本

来自分类Dev

Powershell问题:尝试为非管理用户运行安装脚本

来自分类Dev

在没有网页等待运行的情况下运行PHP脚本

来自分类Dev

在没有网页等待运行的情况下运行PHP脚本

来自分类Dev

如何在不运行初始化/配置脚本的情况下运行bash?

来自分类Dev

VB脚本问题

来自分类Dev

如何在不显示Windows控制台的情况下运行Python脚本

来自分类Dev

docker在没有主机卷的情况下运行本地脚本

来自分类Dev

部署Google应用脚本以在未经用户授权的情况下运行

来自分类Dev

如何在没有DOM的情况下将JavaScript作为node.js脚本运行?

来自分类Dev

如何在启用Mac App沙盒的情况下运行Shell脚本?

来自分类Dev

Makefile.am在源文件更改的情况下运行脚本

Related 相关文章

  1. 1

    在路径中有空格的情况下从Excel运行Powershell脚本

  2. 2

    如何在没有终端窗口的情况下运行PowerShell脚本?

  3. 3

    如何在没有SDK的情况下运行PowerShell脚本文件?

  4. 4

    在不继承变量和作用域的情况下从另一个运行一个PowerShell脚本

  5. 5

    在VB.NET中运行Powershell脚本时出错

  6. 6

    在不输入amm的情况下运行Ammonite scala脚本

  7. 7

    在没有crontab的情况下运行php脚本

  8. 8

    在没有安装库的情况下运行python脚本

  9. 9

    如何在不终止的情况下运行bash脚本

  10. 10

    在名称前没有./的情况下运行脚本

  11. 11

    PHP:在不访问网页的情况下运行PHP脚本?

  12. 12

    在不运行构建脚本的情况下进行构建

  13. 13

    在不使用crontab的情况下运行php脚本

  14. 14

    在不声明解释器的情况下运行python脚本

  15. 15

    在不输入amm的情况下运行Ammonite scala脚本

  16. 16

    如何在不终止的情况下运行bash脚本

  17. 17

    PowerShell脚本上下文菜单项可在不退出的情况下以管理员身份运行脚本

  18. 18

    在IPython中使用魔术“ ed”而不先运行脚本

  19. 19

    Powershell问题:尝试为非管理用户运行安装脚本

  20. 20

    在没有网页等待运行的情况下运行PHP脚本

  21. 21

    在没有网页等待运行的情况下运行PHP脚本

  22. 22

    如何在不运行初始化/配置脚本的情况下运行bash?

  23. 23

    VB脚本问题

  24. 24

    如何在不显示Windows控制台的情况下运行Python脚本

  25. 25

    docker在没有主机卷的情况下运行本地脚本

  26. 26

    部署Google应用脚本以在未经用户授权的情况下运行

  27. 27

    如何在没有DOM的情况下将JavaScript作为node.js脚本运行?

  28. 28

    如何在启用Mac App沙盒的情况下运行Shell脚本?

  29. 29

    Makefile.am在源文件更改的情况下运行脚本

热门标签

归档