2014-11-03 4 views
3

powershell을 사용하여 레지스트리에 키 구조가 있는지 확인하려고합니다. 구조체가 존재하지 않으면 생성해야하고 끝 폴더에 키를 만들어야합니다. 스 니펫을 개별적으로 실행하여 키를 만들면 괜찮습니다. 그러나 블록 자체를 실행 (레지스트리 내에 수동으로 키가 존재하지 않음을 확인)하면 폴더 구조가 생성되지 않습니다. 문제가 무엇인지 잘 모릅니다. 어떤 도움을 주시면 감사하겠습니다.Powershell로 레지스트리 키 만들기

다음
$Registry_Paths = "hkcu:\Software\Microsoft\Office\14.0", "hkcu:\Software\Microsoft\Office\14.0\Groove", "hkcu:\Software\Microsoft\Office\14.0\Groove\Development" 

foreach($Registry_Path in $Registry_Paths) 
{ 
$Test_Path_Result = Test-Path -Path $Registry_Path 
if($Test_Path_Result -eq $false) 
{ 
    $Registry_Key_Log += "Warning: No registry key path found at " + $Registry_Path +"`n" 
    $Registry_Key_Log += "Creating key now for " + $Registry_Path + "`n" + "`n" 

    if($Registry_Path -eq "hkcu:\Software\Microsoft\Office\14.0") 
    { 
     try{ 
     New-Item -Path "HKCU:\Software\Microsoft\Office\14.0" -ItemType Key 
     } 
     catch 
     { 
      $Error_Log += "Warning: There was an error when attempting to create a new registry key, or key property for $Registry_Path" 
      $Error_Log += $_.exception.message 
     } 

    } 
    if($Registry_Path -eq "hcku:\Software\Microsoft\Office\14.0\Groove") 
    { 
     try{ 
     New-Item -Path "HKCU:\Software\Microsoft\Office\14.0\Groove" -ItemType Key 
     } 
     catch 
     { 
      $Error_Log += "Warning: There was an error when attempting to create a new registry key, or key property for $Registry_Path" 
      $Error_Log += $_.exception.message 
     } 
    } 
    if($Registry_Path -eq "hcku:\Software\Microsoft\Office\14.0\Groove\Development") 
    { 
     try{ 
     New-Item -Path "HKCU:\Software\Microsoft\Office\14.0\Groove\Development" -ItemType Key 
     New-ItemProperty -Path "hkcu:\Software\Microsoft\Office\14.0\Groove\Development" -Value 00000001 -PropertyType dword -Name "EnableReleaseBuildDebugOutput" 
     New-ItemProperty -Path "hkcu:\Software\Microsoft\Office\14.0\Groove\Development" -Value 1 -Name "TraceIdentityMessaging" 
     New-ItemProperty -Path "hkcu:\Software\Microsoft\Office\14.0\Groove\Development" -Value 00000001 -PropertyType dword -Name "TraceTelespaceFetch" 
     New-ItemProperty -Path "hkcu:\Software\Microsoft\Office\14.0\Groove\Development" -Value 1 -Name "TraceConnectSequence" 

     } 
     catch 
     { 
      $Error_Log += "Warning: There was an error when attempting to create a new registry key, or key property for $Registry_Path" 
      $Error_Log += $_.exception.message 
     } 
    } 

} 
} 
+0

"스 니펫"이 효과가 있고 어떤 것이 효과가 없었습니까? * 생성 * 실패 * 탐지 * 실패? – briantist

답변

3

내가 그것을 할 거라고 어떻게 다음과 같이 코드입니다.

$Key = "HKEY_CURRENT_USER\TEST" 
If (-Not (Test-Path "Registry::$Key")){New-Item -Path "Registry::$Key" -ItemType RegistryKey -Force} 
Set-ItemProperty -path "Registry::$Key" -Name "Less" -Type "String" -Value "Less" 
0
function New-RegKey 
    { 
     # Create subkey in specified registry hive/key. 
     param (
     [string]$Key 
     ) 
     # Check if key exists or not: 
     if ($reg.GetSubKeyNames() -notcontains $Key) 
     { 
     Try 
      { 
      # The key DOES NOT exist, so create it: 
      $Script:reg = $reg.CreateSubKey($Key) 
      Write-Host "Registry key ""$($Script:reg.Name)"" has been successfully created." -foregroundColor Green 
      } 
     Catch 
      { 
      Write-Host "ERROR:$((($ERROR[0].Exception).InnerException).Message)." -ForegroundColor Yellow 
      } 
     } 
     else 
     { 
     Write-Host "Registry key ""$Key"" already exists. Do not need to create." -ForegroundColor White 
     # Open the subkey in writable mode and update the $reg in script scope: 
     Try 
      { 
      $Script:reg = $reg.OpenSubKey($Key, $Writable) 
      } 
     Catch 
      { 
      Write-Host "ERROR:$((($ERROR[0].Exception).InnerException).Message)." 
      -ForegroundColor Yellow 
      } 
     } 
    } 

    function Set-RegValue 
    { 
    param (
    [string]$RegName, 
    [string]$RegValue, 
    [string]$RegValueType 
    ) 
    # Create or update the specified registry value (valueName and valueValue): 
    # First check to see if the value name exists or not in the current key: 
     if ($reg.GetValueNames() -notcontains $RegName) { 
     # Registry value does not exist, so create one and assign value: 
      Try 
      { 
      $reg.SetValue($RegName, $RegValue, $RegValueType) 
      Write-Host "Registry value name ""$RegName"" does not exist, so create it and assign value ""$RegValue"" with type: ""$RegValueType""." 
      } 
      Catch 
      { 
      Write-Host "ERROR: $((($ERROR[0].Exception).InnerException).Message)."   -ForegroundColor Yellow 
      } 
     } 
     else 
     { 
      Try 
      { 
      Write-Host "Registry value name ""$RegName"" already exists, so update its value ""$RegValue"" with type: ""$RegValueType""." 
      $reg.SetValue($RegName, $RegValue, $RegValueType) 
      } 
      Catch 
      { 
      Write-Host "ERROR:$((($ERROR[0].Exception).InnerException).Message)." -ForegroundColor Yellow 
      } 
     } 
    } 
0

나는 작업을 얻었다.

if($Registry_Path -eq "hkcu:\Software\Microsoft\Office\14.0") 

if($Registry_Path -eq "hcku:\Software\Microsoft\Office\14.0\Groove") 

참고 "HKCU"이상 "HCKU"나는 (! HKCU = hcku) 오타가 있었다 밝혀졌습니다.

+3

당신은 여전히 ​​어렵게하고 있습니다. 예를 들어, -Force 명령을 사용하여'hkcu : \ Software \ Microsoft \ Office \ 14.0 \ Groove \ Development' 키를 만들면 동시에 모든 부모 키를 생성하여 약 8-10 줄을 절약 할 수 있습니다 코드 –

관련 문제