【问题标题】:Powershell Right Click Context MenusPowershell 右键单击​​上下文菜单
【发布时间】:2015-08-12 15:34:27
【问题描述】:

所以我有这个非常基本的 powershell 脚本来将文件备份到我们的网络驱动器:

Function Backup {
param ($backupSource)

#Define backup location
$backupTarget = '\\192.168.0.247\Public'

#Make sure we're targeting a folder
If (!(Test-Path $backupSource -pathtype container)) {
    [System.Windows.Forms.MessageBox]::Show("Target must be a folder" , "Error", 0)
    Exit
    }
#Make sure we have access to the backup location
DO {
    $test = Test-Path $backupTarget
    If (!$test) {
        $loop = [System.Windows.Forms.MessageBox]::Show("Is the WiFi on? I can't reach the public drive. Maybe try again in a second." , "Internet Connection Unavailable" , 5)
            If ($loop -eq 'Cancel') {
            Exit
            }
        }
    } WHILE (!$test)

Copy-Item $backupSource $backupTarget -recurse 
}

我试图让它在右键菜单中工作,让它显示没问题,它会执行,但我不知道如何成功地为其提供 $backupSource 参数。

我正在使用我的默认密钥处理HKEY_CLASSES_ROOT\Directory\shell\NASBackup\command。我已经尝试了我能想到的"C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" -File "C:\Windows\System32\Backup.ps1" "%1" 的所有组合。有人可以帮我解决这里的语法吗?

【问题讨论】:

    标签: windows powershell registry contextmenu


    【解决方案1】:

    提供的脚本不适用于命令行参数。即按原样调用脚本不起作用。

    尝试将调用添加到传递命令行参数的函数。

    在脚本末尾添加:

    Backup $args[0]
    

    【讨论】:

      【解决方案2】:

      我就是这样做的。

      首先我从注册表中启动 cmd.exe。 CMD 窗口在发送 powershell 命令时会短暂出现。

      其次,我使用隐藏开关调用 powershell,以便它的其余部分不可见地运行。

      下面是一个有效的 .reg 文件。

      Windows Registry Editor Version 5.00
      
      [HKEY_CLASSES_ROOT\Directory\shell\NASBackup\command]
      @="cmd /c start /b /wait powershell.exe -nologo -WindowStyle Hidden -file C:\\Windows\\System32\\Backup.ps1"
      ;
      

      这是一个用于在注册表中设置相同位的 powershell 片段。

      If  ( -Not ( Test-Path "Registry::HKEY_CLASSES_ROOT\Directory\shell\NASBackup\command")){New-Item -Path "Registry::HKEY_CLASSES_ROOT\Directory\shell\NASBackup\command" -ItemType RegistryKey -Force}
      Set-ItemProperty -path "Registry::HKEY_CLASSES_ROOT\Directory\shell\NASBackup\command" -Name "(Default)" -Type "String" -Value "cmd /c start /b /wait powershell.exe -nologo -WindowStyle Hidden -file C:\Windows\System32\Backup.ps1"
      #
      

      【讨论】:

        猜你喜欢
        • 2016-02-04
        • 1970-01-01
        • 1970-01-01
        • 2010-12-15
        • 2017-04-22
        • 1970-01-01
        • 1970-01-01
        • 2011-09-30
        相关资源
        最近更新 更多