Private/Get-RegValue.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
function Get-RegValue
{

    <#
    .SYNOPSIS
           Sets the default value (REG_SZ) of the registry key on local or remote computers.
 
    .DESCRIPTION
           Use Get-RegValue to set the default value (REG_SZ) of the registry key on local or remote computers.
            
    .PARAMETER ComputerName
            An array of computer names. The default is the local computer.
 
    .PARAMETER Hive
           The HKEY to open, from the RegistryHive enumeration. The default is 'LocalMachine'.
           Possible values:
            
        - ClassesRoot
        - CurrentUser
        - LocalMachine
        - Users
        - PerformanceData
        - CurrentConfig
        - DynData
 
    .PARAMETER Key
           The path of the registry key to open.
 
    .PARAMETER Value
           The name of the registry value, Wildcards are permitted.
 
       .PARAMETER Type
 
           A collection of data types of registry values, from the RegistryValueKind enumeration.
           Possible values:
 
        - Binary
        - DWord
        - ExpandString
        - MultiString
        - QWord
        - String
         
        When the parameter is not specified all types are returned, Wildcards are permitted.
         
       .PARAMETER Recurse
           Gets the registry values of the specified registry key and its sub keys.
 
    .PARAMETER Ping
           Use ping to test if the machine is available before connecting to it.
           If the machine is not responding to the test a warning message is output.
                
    .EXAMPLE
        Get-RegValue -Key SOFTWARE\Microsoft\PowerShell\1 -Recurse
 
        ComputerName Hive Key Value Data Type
        ------------ ---- --- ----- ---- ----
        COMPUTER1 LocalMachine SOFTWARE\Microsof... Install 1 DWord
        COMPUTER1 LocalMachine SOFTWARE\Microsof... PID 89383-100-0001260... String
        COMPUTER1 LocalMachine SOFTWARE\Microsof... Install 1 DWord
        COMPUTER1 LocalMachine SOFTWARE\Microsof... ApplicationBase C:\Windows\System... String
        COMPUTER1 LocalMachine SOFTWARE\Microsof... PSCompatibleVersion 1.0, 2.0 String
        COMPUTER1 LocalMachine SOFTWARE\Microsof... RuntimeVersion v2.0.50727 String
        (...)
         
         
        Description
        -----------
        Gets all values of the PowerShell subkey on the local computer regardless of their type.
 
    .EXAMPLE
        "SERVER1" | Get-RegValue -Key SOFTWARE\Microsoft\PowerShell\1 -Type String,DWord -Recurse -Ping
 
        ComputerName Hive Key Value Data Type
        ------------ ---- --- ----- ---- ----
        SERVER1 LocalMachine SOFTWARE\Microsof... Install 1 DWord
        SERVER1 LocalMachine SOFTWARE\Microsof... PID 89383-100-0001260... String
        SERVER1 LocalMachine SOFTWARE\Microsof... Install 1 DWord
        SERVER1 LocalMachine SOFTWARE\Microsof... ApplicationBase C:\Windows\System... String
        SERVER1 LocalMachine SOFTWARE\Microsof... PSCompatibleVersion 1.0, 2.0 String
        (...)
     
        Description
        -----------
        Gets all String and DWord values of the PowerShell subkey and its subkeys from remote computer SERVER1, ping the remote server first.
 
    .EXAMPLE
        Get-RegValue -ComputerName SERVER1 -Key SOFTWARE\Microsoft\PowerShell -Type MultiString -Value t* -Recurse
 
        ComputerName Hive Key Value Data Type
        ------------ ---- --- ----- ---- ----
        SERVER1 LocalMachine SOFTWARE\Microsof... Types {virtualmachinema... MultiString
        SERVER1 LocalMachine SOFTWARE\Microsof... Types {C:\Program Files... MultiString
 
        Description
        -----------
        Gets all MultiString value names, from the subkey and its subkeys, that starts with the 't' letter from remote computer SERVER1.
 
    .OUTPUTS
        System.Boolean
        PSFanatic.Registry.RegistryValue (PSCustomObject)
 
    .NOTES
        Author: Shay Levy
        Blog : http://blogs.microsoft.co.il/blogs/ScriptFanatic/
     
    .LINK
        http://code.msdn.microsoft.com/PSRemoteRegistry
 
    .LINK
        Set-RegValue
        Test-RegValue
        Remove-RegValue
 
    #>
    
    

    [OutputType('System.Boolean','PSFanatic.Registry.RegistryValue')]
    [CmdletBinding(DefaultParameterSetName="__AllParameterSets")]
    
    param( 
        [Parameter(
            Position=0,
            ValueFromPipeline=$true,
            ValueFromPipelineByPropertyName=$true
        )]        
        [Alias("CN","__SERVER","IPAddress")]
        [string[]]$ComputerName="",        

        [Parameter(
            Position=1,
            ValueFromPipelineByPropertyName=$true,
            HelpMessage="The HKEY to open, from the RegistryHive enumeration. The default is 'LocalMachine'."
        )]
        [ValidateSet("ClassesRoot","CurrentUser","LocalMachine","Users","PerformanceData","CurrentConfig","DynData")]
        [string]$Hive="LocalMachine",

        [Parameter(
            Mandatory=$true,
            Position=2,
            ValueFromPipelineByPropertyName=$true,
            HelpMessage="The path of the subkey to open."
        )]
        [string]$Key,
        
        [Parameter(
            Mandatory=$false,
            Position=3,
            ValueFromPipelineByPropertyName=$true,
            HelpMessage="The name of the value to set."
        )]    
        [string]$Value="*",        

        [Parameter(
            Mandatory=$false,
            Position=4,
            ValueFromPipelineByPropertyName=$true,
            HelpMessage="The data type of the registry value."
        )]
        [ValidateSet("String","ExpandString","Binary","DWord","MultiString","QWord")]
        [string[]]$Type="*",
        
        [switch]$Ping,
        
        [switch]$Recurse
    ) 

    begin
    {
        Write-Verbose "Enter begin block..."
    
        function Recurse($Key){
        
            Write-Verbose "Start recursing, key is [$Key]"

            try
            {
            
                $subKey = $reg.OpenSubKey($key)
                
                if(!$subKey)
                {
                    Throw "Key '$Key' doesn't exist."
                }
                

                foreach ($v in $subKey.GetValueNames())
                {
                    $vk = $subKey.GetValueKind($v)
                    
                    foreach($t in $Type)
                    {    
                        if($v -like $Value -AND $vk -like $t)
                        {                        
                            $pso = New-Object PSObject -Property @{
                                ComputerName=$c
                                Hive=$Hive
                                Value=if(!$v) {"(Default)"} else {$v}
                                Key=$Key
                                Data=$subKey.GetValue($v)
                                Type=$vk
                            }

                            Write-Verbose "Recurse: Adding format type name to custom object."
                            $pso.PSTypeNames.Clear()
                            $pso.PSTypeNames.Add('PSFanatic.Registry.RegistryValue')
                            $pso
                        }
                    }
                }
                
                foreach ($k in $subKey.GetSubKeyNames())
                {
                    Recurse "$Key\$k"        
                }
                
            }
            catch
            {
                Write-Error $_
            }
            
            Write-Verbose "Ending recurse, key is [$Key]"
        }
        
        Write-Verbose "Exit begin block..."
    }
    

    process
    {


            Write-Verbose "Enter process block..."
        
        foreach($c in $ComputerName)
        {    
            try
            {                
                if($c -eq "")
                {
                    $c=$env:COMPUTERNAME
                    Write-Verbose "Parameter [ComputerName] is not presnet, setting its value to local computer name: [$c]."
                    
                }
                
                if($Ping)
                {
                    Write-Verbose "Parameter [Ping] is presnet, initiating Ping test"
                    
                    if( !(Test-Connection -ComputerName $c -Count 1 -Quiet))
                    {
                        Write-Warning "[$c] doesn't respond to ping."
                        return
                    }
                }
                
                
                Write-Verbose "Starting remote registry connection against: [$c]."
                Write-Verbose "Registry Hive is: [$Hive]."
                $reg = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey([Microsoft.Win32.RegistryHive]$Hive,$c)        
    
                                
                if($Recurse)
                {
                    Write-Verbose "Parameter [Recurse] is presnet, calling Recurse function."
                    Recurse $Key
                }
                else
                {                    
                
                    Write-Verbose "Open remote subkey: [$Key]."            
                    $subKey = $reg.OpenSubKey($Key)
                    
                    if(!$subKey)
                    {
                        Throw "Key '$Key' doesn't exist."
                    }
                    
                    Write-Verbose "Start get remote subkey: [$Key] values."
                    foreach ($v in $subKey.GetValueNames())
                    {                        
                        $vk = $subKey.GetValueKind($v)
                        
                        foreach($t in $Type)
                        {                    
                            if($v -like $Value -AND $vk -like $t)
                            {                                                        
                                $pso = New-Object PSObject -Property @{
                                    ComputerName=$c
                                    Hive=$Hive
                                    Value= if(!$v) {"(Default)"} else {$v}
                                    Key=$Key
                                    Data=$subKey.GetValue($v)
                                    Type=$vk
                                }

                                Write-Verbose "Adding format type name to custom object."
                                $pso.PSTypeNames.Clear()
                                $pso.PSTypeNames.Add('PSFanatic.Registry.RegistryValue')
                                $pso
                            }
                        }
                    }                
                }
                
                Write-Verbose "Closing remote registry connection on: [$c]."
                $reg.close()
            }
            catch
            {
                Write-Error $_
            }
        } 
    
        Write-Verbose "Exit process block..."
    }
}