질문하기질문하기
 

답변됨Set/Create Registry Values

  • 2009년 6월 30일 화요일 오후 7:20AnthonyP100 사용자 메달사용자 메달사용자 메달사용자 메달사용자 메달
     
    I am trying to search registry values and either create a value if
    it's not there or set the cirrect value it should have via the
    following function:

    function Compare2([string]$TermtoMatch, [string]$RegType, [int]
    $RegValue, [string]$RegistryPath) {
            $value = Get-ItemProperty -path $RegistryPath
            if ($value -match $TermtoMatch) { Set-ItemProperty $RegistryPath -
    Name $TermtoMatch -value $RegValue }
            Else { New-ItemProperty $RegistryPath -Name $TermtoMatch -value
    $RegValue -propertyType RegType }

     

    }

    Here's a snippet of the code calling the function:

    $RegistryPath = "HKLM:\SOFTWARE\Intel\LANDesk
    \VirusProtect6\CurrentVersion\Storages\FileSystem\RealTimeScan"

    Compare2("ConfigRestart", "dword", 1, $RegistryPath)

    Here's the error:
    Get-ItemProperty : Cannot bind argument to parameter 'Path' because it
    is an empty string.
    At C:\script.ps1:2 char:33
    + $value = Get-ItemProperty -Path <<<< $RegistryPath
    The '-match' operator failed: parsing "ConfigRestart dword 1 "HKLM:
    \SOFTWARE\Intel\LANDesk\VirusProtect6\CurrentVersion\Storages
    \FileSystem\RealTimeScan" - Unrecognized escape sequence \I..
    At C:\script.ps1:3 char:19
    + if ($value -match <<<< $TermtoMatch) {

    Any ideas what I'm missing here?  I'm thinking it has something to do
    with my casting.  When I manually put the values in for the variables
    it seems to work just fine.

    Thanks.

답변

  • 2009년 7월 1일 수요일 오전 1:20Marco ShawMVP, 중재자사용자 메달사용자 메달사용자 메달사용자 메달사용자 메달
     답변됨
    When you call your function, call it like this:
    PS>compare2 "configrestart" "dword" 1 $registrypath

    By putting commas, PowerShell is thinking this is all the first argument and passes it in as an array value.

모든 응답

  • 2009년 7월 1일 수요일 오전 1:20Marco ShawMVP, 중재자사용자 메달사용자 메달사용자 메달사용자 메달사용자 메달
     답변됨
    When you call your function, call it like this:
    PS>compare2 "configrestart" "dword" 1 $registrypath

    By putting commas, PowerShell is thinking this is all the first argument and passes it in as an array value.
  • 2009년 7월 1일 수요일 오전 2:27Mervyn ZhangMSFT, 중재자사용자 메달사용자 메달사용자 메달사용자 메달사용자 메달
     
    Hi,

    Additional to Marco's answer, it’s suggested to run the following commands to get more detailed information and instruction about PowerShell Functions.

    Get-Help about_functions

    Get-Help about_functions_advanced

    Get-Help about_functions_advanced_methods

    Get-Help about_functions_cmdletbindingattribute

    Thanks.

    This posting is provided "AS IS" with no warranties, and confers no rights.
  • 2009년 7월 1일 수요일 오후 1:17Marco ShawMVP, 중재자사용자 메달사용자 메달사용자 메달사용자 메달사용자 메달
     
    Some of what Mervyn just suggested requires PowerShell v2 (about_functions_advanced, for example).
  • 2009년 7월 1일 수요일 오후 4:25AnthonyP100 사용자 메달사용자 메달사용자 메달사용자 메달사용자 메달
     
    Wow, I feel like a complete idiot.  Thanks you!