1

So I've got this pretty basic powershell script to backup files to our network drive:

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 
}

I'm trying to get it to work in a right click menu, making it show up is no problem, and it executes, but I can't figure out how to successfully feed it the $backupSource parameter.

I'm working out of HKEY_CLASSES_ROOT\Directory\shell\NASBackup\command with my default key. I've tried every combination of "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" -File "C:\Windows\System32\Backup.ps1" "%1" I can think of. Can someone please help me out with the syntax here?

2 Answers 2

1

The script as provided doesn't work with command line arguments. i.e. calling the script as is doesn't work.

Try adding the call to the function passing the command line arguments.

At the end of your script add:

Backup $args[0]
Sign up to request clarification or add additional context in comments.

Comments

0

I do it like this.

First I start cmd.exe from inside the registry. CMD window will appear briefly while it sends the powershell command.

Second I call powershell with the hidden switch so the rest of it runs invisibly.

Below is a working .reg file.

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"
;

Here is a powershell snip to set the same bits in the registry.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.