Powershell右键单击上下文菜单

时间:2015-08-12 15:34:27

标签: windows powershell registry contextmenu

所以我有了这个非常基本的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"我能想到的每一种组合。有人可以帮我解决这里的语法吗?

2 个答案:

答案 0 :(得分:1)

提供的脚本不能使用命令行参数。即调用脚本不起作用。

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

在您的脚本结束时添加:

Backup $args[0]

答案 1 :(得分:0)

我是这样做的。

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

其次,我使用隐藏的开关调用powershell,以便其余部分无形地运行。

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

Map

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

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"
;
相关问题