简体   繁体   中英

Add registry backup to new key using Powershell and SendTo

This is most likely an issue with the way I'm writing the .bat file (spoiler: I'm very much new to those) since the Powershell script itself works.

Background
I have a lot of customer cases that requires me to import a registry file and look in it. I am trying to automate this process by making a script that will allow me to right-click the .reg file and use the SendTo to send it to a script that then adds all the contents to a specific registry key. Basically it will:

  • Look at the path of the .reg file that I'm trying to send to the Script
  • Find the beginning of the case number in the path
  • Create a registry key with the same name
  • Import the contents of the .reg file to that key

Work done so far
Here is the script that I've done so far:

param(
    [Parameter(mandatory=$true)][string]$FileName
    )

$RegistryParent = "HKLM:\Software\Cases"

$RegistryFile = Get-ChildItem -Path $FileName
$FullName = $RegistryFile.FullName

if(($RegistryFile.Extension -eq '.reg') -and ($FullName -like '*CaseNumber-*')){
    $IndexOfCaseStart = ($FullName).IndexOf('CaseNumber-')
    $IndexOfCaseEnd = ($FullName).IndexOf('\\',$IndexOfCaseStart)
    $CaseNumber = ($FullName).Substring($IndexOfCaseStart,$IndexOfCaseEnd-$IndexOfCaseStart)
    $ImportTo = "HKLM\Software\Cases\$CaseNumber"

    if(!(Test-Path $ImportTo)){
    New-Item -Path $RegistryParent -Name $CaseNumber
    reg.exe restore $ImportTo ($FullName)
    }
}

Running this script only works perfectly and correctly imports any .reg file I throw at it. As I understand it, you can't have a Powershell script in the shell:SendTo folder, so I'm using a .bat file in there instead to call the Powershell script. The .bat file looks like this:

@echo off
    powershell.exe -Command "'\\SERVER\pathToPowershellScript\AddToRegistry.ps1 -FileName %1'"
    powershell.exe -noexit

Issue

This completes without any failures, however no registry key is created in the registry. Here is what I get if add echo powershell.exe -Command "'\\SERVER\\pathToPowershellScript\\AddToRegistry.ps1 -FileName %1'" to the .bat file:

'\\SERVER\pathToRegistryKey'
CMD.EXE was started with the above path as the current directory.
UNC paths are not supported.  Defaulting to Windows directory.
'powershell.exe -Command "'\\SERVER\pathToPowershellScript\AddToRegistry.ps1 -FileName \\SERVER\pathToRegistryKey\registrykey.reg'"
\\SERVER\pathToPowershellScript\AddToRegistry.ps1 -FileName \\SERVER\pathToRegistryKey\registrykey.reg
Windows PowerShell
Copyright (C) 2016 Microsoft Corporation. All rights reserved.

My guess is that Powershell starts but doesn't run the script, or that I'm using the wrong parameters in the .bat file to call the script. From my point of view, it looks like it starts Powershell, and then it just picks the Scriptpath.ps1 -FileName Registrypath.reg and echos it, but I'm not used to reading .bat file outputs so I'm very likely wrong here.

I'll be happy to provide more information. If anyone has anything that can help me it would be greatly appreciated!

EDIT:
Removing the ' quotation marks throws an error because Powershell interprets it as a separate command (because it is within the My Documents folder):

@echo off
    powershell.exe -Command "\\SERVER\pathToPowershellScript\AddToRegistry.ps1 -FileName %1"
    powershell.exe -noexit

This gives:

\\SERVER\\Users\\UserName\\My : The term '\\SERVER\\Users\\UserName\\My' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the...

Adding a period to the beginning of the script does not take it either. I'm guessing that I need to wrap the location to the Powershell script with ' quotes, but I will need to escape them within the .bat file.

EDIT2:
Added \\SERVER\\ to all the paths to reflect that I'm using the full paths and not relative ones.

EDIT3:
Here are some tries with the results.

@echo off
    powershell.exe -Command "\\SERVER\pathToPowershellScript\AddToRegistry.ps1" -FileName "%1" -Noprofile

    powershell.exe -noexit

The above gives the same error as in the EDIT 2. I tried using -File instead of -Command but it still won't run.

@echo off
    powershell.exe -File"\\SERVER\pathToPowershellScript\AddToRegistry.ps1" -FileName "%1" -Noprofile
    powershell.exe -noexit

This gives the following:

\\SERVER\\Users\\UserName\\pathToPowershellScript\\AddToRegistry.ps1: A parameter cannot be found that matches parameter name 'Noprofile'.

EDIT 4:
I did find one that actually runs the script even if it doesn't complete the script, but now it actually starts at least. It seems that it doesn't have enough permissions. If I remove the -NoProfile from the above version of the .bat file I instead get the following error:

New-Item : Requested registry access is not allowed. At \\SERVER\\Users\\UserName\\pathToPowershellScript\\AddToRegistry.ps1:19 char:5

 + New-Item -Path $RegistryParent -Name $CaseNumber + CategoryInfo : PermissionDenied: (HKEY_LOCAL_MACH...\\Cases:String) [New-Item], SecurityException + FullyQualifiedErrorId : System.Security.SecurityException,Microsoft.PowerShell.Commands.NewItemCommand

I've tried adding the -ExecutionPolicy Bypass to the parameter that should call the script but it still says I don't have registry access...

Let's Explore what your current script does

@echo off
REM echo the string following command
powershell.exe -Command "'pathToPowershellScript\AddToRegistry.ps1 -FileName %1'"
REM opens an interactive powershell windows
powershell.exe -noexit

Here is my attempt to fix the issue

@echo off
REM Execute AddToRegistry and pass %1
powershell.exe -Command ".\pathToPowershellScript\AddToRegistry.ps1" -FileName "%1"
REM opens an interactive powershell windows
powershell.exe -noexit

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM