powershell을 사용하여 레지스트리 키의 값과 값만 가져 오는 방법
누구든지 레지스트리 키의 값을 가져 와서 PowerShell의 변수에 배치하도록 도울 수 있습니까? 지금까지 사용 Get-ItemProperty
했으며 reg query
둘 다 값을 가져 오지만 둘 다 추가 텍스트도 추가합니다. 레지스트리 키의 문자열 텍스트와 키의 문자열 텍스트 만 필요합니다. 추가 텍스트를 제거하는 함수를 만들 수 있지만 무언가 변경되면 (예 : reg 키 이름) 영향을 미칠 수 있습니다.
$key = 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion'
(Get-ItemProperty -Path $key -Name ProgramFilesDir).ProgramFilesDir
나는 이것이 공급자가 다음과 같이 구현 된 방식을 결코 좋아하지 않았습니다 : /
기본적으로, 모든 레지스트리 값이 수 PSCustomObject
와 객체 PsPath
, PsParentPath
, PsChildname
, PSDrive
그리고 PSProvider
다음 속성과 실제 값에 대한 속성을. 따라서 이름으로 항목을 요청했지만 그 값을 얻으려면 이름을 한 번 더 사용해야합니다.
이러한 답변 중 어느 것도 값 이름에 PowerShell에서 예약 된 공백, 점 또는 기타 문자가 포함 된 상황에서 작동하지 않습니다. 이 경우 http://blog.danskingdom.com/accessing-powershell-variables-with-periods-in-their-name/ 에 따라 이름을 큰 따옴표로 묶어야합니다 . 예 :
PS> Get-ItemProperty Registry::HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\Microsoft\VisualStudio\SxS\VS7
14.0 : C:\Program Files (x86)\Microsoft Visual Studio 14.0\
12.0 : C:\Program Files (x86)\Microsoft Visual Studio 12.0\
11.0 : C:\Program Files (x86)\Microsoft Visual Studio 11.0\
15.0 : C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\
PSPath : Microsoft.PowerShell.Core\Registry::HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\Microsoft\VisualStudio\SxS\V
S7
PSParentPath : Microsoft.PowerShell.Core\Registry::HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\Microsoft\VisualStudio\SxS
PSChildName : VS7
PSProvider : Microsoft.PowerShell.Core\Registry
14.0, 12.0, 11.0, 15.0 값 중 하나에 액세스하려는 경우 수락 된 답변의 솔루션 이 작동 하지 않으며 출력이 표시되지 않습니다.
PS> (Get-ItemProperty Registry::HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\Microsoft\VisualStudio\SxS\VS7 -Name 15.0).15.0
PS>
작동하는 것은 값 이름을 인용하는 것입니다. 어쨌든 안전을 위해해야 할 일입니다.
PS> (Get-ItemProperty "Registry::HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\Microsoft\VisualStudio\SxS\VS7" -Name "15.0")."15.0"
C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\
PS>
따라서 허용되는 답변은 다음과 같이 수정되어야합니다.
PS> $key = "Registry::HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\Microsoft\VisualStudio\SxS\VS7"
PS> $value = "15.0"
PS> (Get-ItemProperty -Path $key -Name $value).$value
C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\
PS>
이는 PowerShell 2.0에서 5.0까지 작동합니다 (하지만 Get-ItemPropertyValue
v5에서 사용해야 할 수도 있음 ).
- Andy Arismendi의 유용한 답변 은 값 데이터를 효율적 으로 가져 오기 위해 값 이름 을 반복 해야하는 번거 로움을 설명합니다 .
- M Jeremy Carter의 유용한 답변 은 더 편리 하지만 많은 수의 속성을 가진 객체를 구성해야하므로 값이 많은 키의 경우 성능 문제 가 될 수 있습니다 .
참고 : 아래의 모든 솔루션 은 Ian Kemp의 답변에 설명 된 문제를 우회 합니다 . 즉, 속성 이름으로 사용할 때 특정 값 이름에 대해 명시 적 인용을 사용해야 할 필요가 있습니다 . 예 : -값 이름이 매개 변수 로 전달 되고 속성 액세스가 변수 를 통해 발생하기 때문 입니다 . 예 :.'15.0'
.$ValueName
Harry Martyrossian은 자신의 답변에 대한 설명에서
Get-ItemPropertyValue
cmdlet 이 Powershell v5 에 도입되어 문제를 해결 했다고 언급했습니다 .
PS> Get-ItemPropertyValue 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion' 'ProgramFilesDir'
C:\Program Files
PowerShell v4-의 대안 :
다음은 효율성을 유지하면서 값 이름을 반복 할 필요가 없지만 여전히 약간 성가신 시도입니다.
& { (Get-ItemProperty `
-LiteralPath HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion `
-Name $args `
).$args } 'ProgramFilesDir'
스크립트 블록을 사용하면 값 이름을 한 번 매개 변수로 전달할 수 있으며 매개 변수 변수 ( $args
)는 블록 내에서 두 번만 사용할 수 있습니다.
또는 간단한 도우미 기능으로 통증을 완화 할 수 있습니다.
function Get-RegValue([String] $KeyPath, [String] $ValueName) {
(Get-ItemProperty -LiteralPath $KeyPath -Name $ValueName).$ValueName
}
이것이 변경되었는지 또는 사용중인 PS 버전과 관련이 있는지 확실하지 않지만 Andy의 예를 사용하면 -Name 매개 변수를 제거 할 수 있으며 여전히 reg 항목의 값을 얻습니다. :
PS C:\> $key = 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion'
PS C:\> (Get-ItemProperty -Path $key).ProgramFilesDir
C:\Program Files
PS C:\> $psversiontable.psversion
Major Minor Build Revision
----- ----- ----- --------
2 0 -1 -1
여기서 구체적이어야합니다. 내가 아는 한 레지스트리의 키는 속성의 "폴더"입니다. 그래서 재산의 가치를 얻으려고 했습니까? 그렇다면 다음과 같이 시도하십시오.
(Get-ItemProperty HKLM:\Software\Microsoft\PowerShell\1\PowerShellEngine -Name PowerShellVersion).PowerShellVersion
First we get an object containing the property we need with Get-ItemProperty
and then we get the value of for the property we need from that object. That will return the value of the property as a string. The example above gives you the PS version for "legacy"/compatibility-mdoe powershell (1.0 or 2.0).
Given a key \SQL
with two properties:
I'd grab the "MSSQLSERVER" one with the following in-cases where I wasn't sure what the property name was going to be to use dot-notation:
$regkey_property_name = 'MSSQLSERVER'
$regkey = get-item -Path 'HKLM:\Software\Microsoft\Microsoft SQL Server\Instance Names\SQL'
$regkey.GetValue($regkey_property_name)
Following code will enumerate all values for a certain Registry key, will sort them and will return value name : value pairs separated by colon (:):
$path = 'HKLM:\SOFTWARE\Wow6432Node\Microsoft\.NETFramework';
Get-Item -Path $path | Select-Object -ExpandProperty Property | Sort | % {
$command = [String]::Format('(Get-ItemProperty -Path "{0}" -Name "{1}")."{1}"', $path, $_);
$value = Invoke-Expression -Command $command;
$_ + ' : ' + $value; };
Like this:
DbgJITDebugLaunchSetting : 16
DbgManagedDebugger : "C:\Windows\system32\vsjitdebugger.exe" PID %d APPDOM %d EXTEXT "%s" EVTHDL %d
InstallRoot : C:\Windows\Microsoft.NET\Framework\
If you create an object, you get a more readable output and also gain an object with properties you can access:
$path = 'HKLM:\SOFTWARE\Wow6432Node\Microsoft\.NETFramework'
$obj = New-Object -TypeName psobject
Get-Item -Path $path | Select-Object -ExpandProperty Property | Sort | % {
$command = [String]::Format('(Get-ItemProperty -Path "{0}" -Name "{1}")."{1}"', $path, $_)
$value = Invoke-Expression -Command $command
$obj | Add-Member -MemberType NoteProperty -Name $_ -Value $value}
Write-Output $obj | fl
Sample output: InstallRoot : C:\Windows\Microsoft.NET\Framework\
And the object: $obj.InstallRoot = C:\Windows\Microsoft.NET\Framework\
The truth of the matter is this is way more complicated than it needs to be. Here is a much better example, and much simpler:
$path = 'HKLM:\SOFTWARE\Wow6432Node\Microsoft\.NETFramework'
$objReg = Get-ItemProperty -Path $path | Select -Property *
$objReg is now a custom object where each registry entry is a property name. You can view the formatted list via:
write-output $objReg
InstallRoot : C:\Windows\Microsoft.NET\Framework\
DbgManagedDebugger : "C:\windows\system32\vsjitdebugger.exe"
And you have access to the object itself:
$objReg.InstallRoot
C:\Windows\Microsoft.NET\Framework\
'IT TIP' 카테고리의 다른 글
Mongoose JS를 통한 MongoDB-findByID 란 무엇입니까? (0) | 2020.11.26 |
---|---|
Groovy에서 조건부 수집을 작성하는 방법은 무엇입니까? (0) | 2020.11.26 |
jQuery AJAX 호출에서 '최종'과 유사한 것이 있습니까? (0) | 2020.11.26 |
입력 텍스트 요소에 대한 올바른 읽기 전용 속성 구문은 무엇입니까? (0) | 2020.11.26 |
Dart 프로그램을 "수면"하려면 어떻게해야합니까? (0) | 2020.11.26 |