簡體   English   中英

使用 Powershell 和 SendTo 將注冊表備份添加到新密鑰

[英]Add registry backup to new key using Powershell and SendTo

這很可能是我編寫 .bat 文件的方式的問題(劇透:我對這些文件很陌生),因為 Powershell 腳本本身可以工作。

背景
我有很多客戶案例需要我導入注冊表文件並查看它。 我試圖通過制作一個腳本來自動化這個過程,該腳本允許我右鍵單擊 .reg 文件並使用 SendTo 將其發送到一個腳本,然后將所有內容添加到特定的注冊表項。 基本上它會:

  • 查看我嘗試發送到腳本的 .reg 文件的路徑
  • 在路徑中查找案例編號的開頭
  • 創建一個同名的注冊表項
  • 將 .reg 文件的內容導入該密鑰

迄今為止完成的工作
這是我到目前為止所做的腳本:

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)
    }
}

運行此腳本只能完美且正確地導入我扔給它的任何 .reg 文件。 據我了解,shell:SendTo 文件夾中不能有 Powershell 腳本,因此我在其中使用 .bat 文件來調用 Powershell 腳本。 .bat 文件如下所示:

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

問題

這在沒有任何故障的情況下完成,但是在注冊表中沒有創建注冊表項。 如果將 echo powershell.exe -Command "'\\SERVER\\pathToPowershellScript\\AddToRegistry.ps1 -FileName %1'" 添加到 .bat 文件,我會得到以下結果:

'\\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.

我的猜測是 Powershell 啟動但不運行腳本,或者我在 .bat 文件中使用了錯誤的參數來調用腳本。 從我的角度來看,它看起來像是啟動了 Powershell,然后它只是選擇 Scriptpath.ps1 -FileName Registrypath.reg 並回顯它,但我不習慣讀取 .bat 文件輸出,所以我很可能是錯的這里。

我很樂意提供更多信息。 如果有人有任何可以幫助我的東西,將不勝感激!

編輯:
刪除 ' 引號會引發錯誤,因為 Powershell 將其解釋為單獨的命令(因為它位於我的文檔文件夾中):

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

這給出:

\\SERVER\\Users\\UserName\\My :術語“\\SERVER\\Users\\UserName\\My”不被識別為 cmdlet、函數、腳本文件或可運行程序的名稱。 檢查...

在腳本的開頭添加句點也不接受。 我猜我需要用 ' 引號將位置包裝到 Powershell 腳本中,但我需要在 .bat 文件中對它們進行轉義。

編輯2:
將 \\SERVER\\ 添加到所有路徑以反映我使用的是完整路徑而不是相對路徑。

編輯3:
以下是結果的一些嘗試。

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

    powershell.exe -noexit

上面給出了與 EDIT 2 相同的錯誤。我嘗試使用 -File 而不是 -Command 但它仍然無法運行。

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

這給出了以下內容:

\\SERVER\\Users\\UserName\\pathToPowershellScript\\AddToRegistry.ps1:找不到與參數名稱“Noprofile”匹配的參數。

編輯 4:
我確實找到了一個實際上運行腳本的程序,即使它沒有完成腳本,但現在它至少實際上啟動了。 好像權限不夠。 如果我從 .bat 文件的上述版本中刪除 -NoProfile ,則會收到以下錯誤:

新項目:不允許請求的注冊表訪問。 在 \\SERVER\\Users\\UserName\\pathToPowershellScript\\AddToRegistry.ps1:19 字符: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

我已經嘗試將 -ExecutionPolicy Bypass 添加到應該調用腳本的參數中,但它仍然說我沒有注冊表訪問權限...

讓我們探索一下您當前的腳本是做什么的

@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

這是我解決問題的嘗試

@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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM