]> insang Git - newton-cn_pos.git/blob
b0553b1672abf2482d55afc8d760dab8281176b3
[newton-cn_pos.git] /
1 # This PS module contains functions for Desired State Configuration (DSC) "Environment" provider
2
3 # Fallback message strings in en-US
4 DATA localizedData
5 {
6     # culture = "en-US"
7     ConvertFrom-StringData @'        
8         EnvVarCreated = (CREATE) Environment variable '{0}' with value '{1}'        
9         EnvVarSetError = (ERROR) Failed to set environment variable '{0}' to value '{1}'
10         EnvVarPathSetError = (ERROR) Failed to add path '{0}' to environment variable '{1}' holding value '{2}'
11         EnvVarRemoveError = (ERROR) Failed to remove environment variable '{0}' holding value '{1}'
12         EnvVarPathRemoveError = (ERROR) Failed to remove path '{0}' from variable '{1}' holding value '{2}'
13         EnvVarUnchanged = (UNCHANGED) Environment variable '{0}' with value '{1}'
14         EnvVarUpdated = (UPDATE) Environment variable '{0}' from value '{1}' to value '{2}'
15         EnvVarPathUnchanged = (UNCHANGED) Path environment variable '{0}' with value '{1}'
16         EnvVarPathUpdated = (UPDATE) Environment variable '{0}' from value '{1}' to value '{2}'        
17         EnvVarNotFound = (NOT FOUND) Environment variable '{0}'
18         EnvVarFound = (FOUND) Environment variable '{0}' with value '{1}'
19         EnvVarFoundWithMisMatchingValue = (FOUND MISMATCH) Environment variable '{0}' with value '{1}' mismatched the specified value '{2}'        
20         EnvVarRemoved = (REMOVE) Environment variable '{0}'
21 '@
22 }
23 Import-LocalizedData  LocalizedData -filename MSFT_EnvironmentResource.strings.psd1
24
25  
26 #-------------------------------------
27 # Script-level Constants and Variables
28 #-------------------------------------
29 $EnvVarRegPathMachine = "HKLM:\\System\\CurrentControlSet\\Control\\Session Manager\\Environment"
30 $EnvVarRegPathUser = "HKCU:\\Environment"
31
32 $EnvironmentVariableTarget = @{ Process = 0; User = 1; Machine = 2 }
33 $MaxSystemEnvVariableLength = 1024
34 $MaxUserEnvVariableLength = 255
35
36 Function Throw-InvalidArgumentException
37 {
38     param(
39         [string] $Message,
40         [string] $ParamName
41     )
42     
43     $exception = new-object System.ArgumentException $Message,$ParamName
44     $errorRecord = New-Object System.Management.Automation.ErrorRecord $exception,$ParamName,"InvalidArgument",$null
45     throw $errorRecord
46 }
47
48 function GetEnvironmentVariable
49 {
50     param
51     (
52         [parameter(Mandatory = $true)]
53         [ValidateNotNullOrEmpty()]
54         [String] $Name, 
55
56         [parameter(Mandatory = $true)]
57         [int] $Target
58     )
59
60     if ($Target -eq $EnvironmentVariableTarget.Process) 
61     {
62         return [System.Environment]::GetEnvironmentVariable($Name);
63     }
64
65     if ($Target -eq $EnvironmentVariableTarget.Machine)
66     {
67         $retVal = Get-ItemProperty $EnvVarRegPathMachine -Name $Name -ErrorAction SilentlyContinue
68         return $retVal.$Name
69     }
70
71     if ($Target -eq $EnvironmentVariableTarget.User)
72     {
73         $retVal = Get-ItemProperty $EnvVarRegPathUser -Name $Name -ErrorAction SilentlyContinue
74         return $retVal.$Name
75     }
76 }
77
78 function SetEnvironmentVariable
79 {
80     param
81     (
82         [parameter(Mandatory = $true)]
83         [ValidateNotNullOrEmpty()]
84         [String] $Name, 
85
86         [String] $Value,
87
88         [parameter(Mandatory = $true)]
89         [int] $Target
90     )
91
92     if ($Target -eq $EnvironmentVariableTarget.Process) 
93     {
94         [System.Environment]::SetEnvironmentVariable($Name, $Value);
95     }
96
97     if ($Target -eq $EnvironmentVariableTarget.Machine) 
98     {
99         if ($Name.Length -ge $MaxSystemEnvVariableLength) {
100             Throw-InvalidArgumentException -Message "Argument is too long." -ParamName $Name
101         }
102         $Path = $EnvVarRegPathMachine
103     }
104     elseif ($Target -eq $EnvironmentVariableTarget.User) 
105     {
106         if ($Name.Length -ge $MaxUserEnvVariableLength) {
107             Throw-InvalidArgumentException -Message "Argument is too long." -ParamName $Name
108         }
109         $Path = $EnvVarRegPathUser
110     }
111
112     $environmentKey = Get-ItemProperty $Path -Name $Name -ErrorAction SilentlyContinue
113     if ($environmentKey) 
114     {
115         if (!$Value) 
116         {
117             Remove-ItemProperty $Path -Name $Name -ErrorAction SilentlyContinue
118         }
119         else 
120         {
121             Set-ItemProperty $Path -Name $Name -Value $Value -ErrorAction SilentlyContinue
122         }
123     }
124 }
125
126
127 #------------------------------
128 # The Get-TargetResource cmdlet
129 #------------------------------
130 FUNCTION Get-TargetResource
131 {    
132     param
133     (
134         [parameter(Mandatory = $true)]
135         [ValidateNotNullOrEmpty()]
136         [System.String]
137         $Name           
138     )
139         
140     $retVal = GetItemProperty $EnvVarRegPathMachine -Name $Name -Expand:$false -ErrorAction SilentlyContinue
141     
142     if($retVal -eq $null)
143     {        
144         Write-Verbose ($localizedData.EnvVarNotFound -f $Name)
145         
146         return @{Ensure='Absent'; Name=$Name}      
147     }    
148
149     Write-Verbose ($localizedData.EnvVarFound -f $Name, $retVal.$Name)
150
151     return @{Ensure='Present'; Name=$Name; Value=$retVal.$Name}
152 }
153
154 function Set-EnvVar
155 {
156     param
157     (           
158         [parameter(Mandatory = $true)]
159         [ValidateNotNullOrEmpty()]
160         [System.String]
161         $Name,
162         
163         [ValidateNotNull()]
164         [System.String]
165         $Value = [String]::Empty
166     )
167
168     $err = Set-ItemProperty $EnvVarRegPathMachine -Name $Name -Value $Value 2>&1
169
170     if($err)
171     {
172         Write-Verbose ($localizedData.EnvVarSetError -f $Name, $Value)
173
174         throw $err
175     }                
176
177     try
178     {
179         if($value)
180         {
181             SetEnvironmentVariable -Name $Name -Value $Value -Target $EnvironmentVariableTarget.Machine
182             SetEnvironmentVariable -Name $Name -Value $Value -Target $EnvironmentVariableTarget.Process
183         }
184     }
185     catch 
186     {
187         Write-Verbose ($localizedData.EnvVarSetError -f $Name, $Value)
188
189         throw $_
190     }
191
192 }
193 function Remove-EnvVar
194 {
195     param
196     (           
197         [parameter(Mandatory = $true)]
198         [ValidateNotNullOrEmpty()]
199         [System.String]
200         $Name
201     )
202
203     $curVarProperties = Get-ItemProperty $EnvVarRegPathMachine -Name $Name -ErrorAction SilentlyContinue
204     $currentValueFromEnv = GetEnvironmentVariable -Name $name -Target $EnvironmentVariableTarget.Process
205
206     if($curVarProperties -ne $null)
207     {
208         $err = Remove-ItemProperty $EnvVarRegPathMachine -Name $Name 2>&1
209
210         if($err)
211         {
212             Write-Log -Message ($localizedData.EnvVarRemoveError -f $Name, $Value)
213
214             throw $err
215         }
216     }
217
218     if($currentValueFromEnv -ne $null)
219     {
220         try
221         {
222             SetEnvironmentVariable -Name $Name -Value $null -Target $EnvironmentVariableTarget.Machine
223             SetEnvironmentVariable -Name $Name -Value $null -Target $EnvironmentVariableTarget.Process
224         }
225         catch 
226         {
227             Write-Verbose ($localizedData.EnvVarRemoveError -f $Name, $Value)
228
229             throw $_
230         }
231     }
232 }
233
234
235 #------------------------------
236 # The Set-TargetResource cmdlet
237 #------------------------------
238 FUNCTION Set-TargetResource
239 {
240     [CmdletBinding(SupportsShouldProcess=$true)]
241     param
242     (           
243         [parameter(Mandatory = $true)]
244         [ValidateNotNullOrEmpty()]
245         [System.String]
246         $Name,
247         
248         [ValidateNotNull()]
249         [System.String]
250         $Value = [String]::Empty,
251         
252         [ValidateSet("Present", "Absent")]
253         [System.String]
254         $Ensure = "Present",
255         
256         [System.Boolean]
257         $Path = $false
258     )
259     
260     $ValueSpecified = $PSBoundParameters.ContainsKey("Value")    
261     
262     $curVarProperties = GetItemProperty $EnvVarRegPathMachine -Name $Name -Expand:(-not $Path) -ErrorAction SilentlyContinue
263     $currentValueFromEnv = GetEnvironmentVariable -Name $name -Target $EnvironmentVariableTarget.Process
264
265     # ----------------
266     # ENSURE = PRESENT
267     if ($Ensure -ieq "Present")
268     {        
269         if (($curVarProperties -eq $null) -or (($currentValueFromEnv -eq $null) -and ($curVarProperties.$Name -ne [string]::Empty)))  # The specified variable doesn't exist already
270         {
271             # Given the specified $Name environment variable doesn't exist already,
272             # simply create one with the specified value and return. If no $Value is 
273             # specified, the default value is set to empty string "" (per spec).
274             # Both path and non-path cases are covered by this.
275             
276             $successMessage = $localizedData.EnvVarCreated -f $Name, $Value
277
278             if ($PSCmdlet.ShouldProcess($successMessage, $null, $null))
279             {    
280                 Set-EnvVar -Name $Name -Value $Value
281             }            
282                         
283             return
284         }
285         
286         # If the control reaches here, the specified variable exists already
287
288         if (!$ValueSpecified)
289         {
290             # Given no $Value was specified to be set and the variable exists, 
291             # we'll leave the existing variable as is.
292             # This covers both path and non-path variables.
293
294             Write-Log -Message ($localizedData.EnvVarUnchanged -f $Name, $curVarProperties.$Name)
295
296             return
297         }
298
299         # If the control reaches here: the specified variable exists already and a $Value has been specified to be set.
300
301         if (!$Path)
302         {
303             # For non-path variables, simply set the specified $Value as the new value of the specified 
304             # variable $Name, then return.
305
306             $successMessage = $localizedData.EnvVarUpdated -f $Name, $curVarProperties.$Name, $Value
307             if ($Value -ceq $curVarProperties.$Name)
308             {
309                 $successMessage = $localizedData.EnvVarUnchanged -f $Name, $curVarProperties.$Name
310             }
311
312             if ($PSCmdlet.ShouldProcess($successMessage, $null, $null) -and ($Value -cne $curVarProperties.$Name))
313             {    
314                 Set-EnvVar -Name $Name -Value $Value
315             }             
316
317             return
318         }
319         
320         # If the control reaches here: the specified variable exists already, it is a path variable and a $Value has been specified to be set.                               
321             
322         # Check if an empty, whitespace or semi-colon only string has been specified. If yes, return unchanged.
323         $trimmedValue = $Value.Trim(";"," ")
324         if ([String]::IsNullOrEmpty($trimmedValue))
325         {
326             Write-Log -Message ($localizedData.EnvVarPathUnchanged -f $Name, $curVarProperties.$Name)
327
328             return        
329         }
330
331
332         $setValue = $curVarProperties.$Name + ";"
333         $specifiedPaths = $trimmedValue -split ";"
334         $currentPaths = $curVarProperties.$Name -split ";"                                
335         $varUpdated = $false
336
337         foreach ($specifiedPath in $specifiedPaths)            
338         {            
339             if (FindSubPath -QueryPath $specifiedPath -PathList $currentPaths)
340             {
341                 # Found this $specifiedPath as one of the $currentPaths, no need to add this again, skip/continue to the next $specifiedPath
342                 
343                 continue
344             }
345
346             # If the control reached here, we didn't find this $specifiedPath in the $currentPaths, add it
347             # and mark the environment variable as updated.
348
349             $varUpdated = $true
350             $setValue += $specifiedPath + ";"                            
351         }  
352
353         # Remove any extraneous ";" at the end (and potentially start - as a side-effect) of the value to be set
354         $setValue = $setValue.Trim(";")        
355                                            
356         # Set the expected success message
357         $successMessage = $localizedData.EnvVarPathUnchanged -f $Name, $curVarProperties.$Name                   
358         if ($varUpdated)
359         {
360             $successMessage = $localizedData.EnvVarPathUpdated -f $Name, $curVarProperties.$Name, $setValue
361         }
362                 
363         if ($PSCmdlet.ShouldProcess($successMessage, $null, $null))
364         {    
365             # Finally update the existing environment path variable        
366
367             Set-EnvVar -Name $Name -Value $setValue
368         }        
369     }
370
371     # ---------------
372     # ENSURE = ABSENT
373     elseif ($Ensure -ieq "Absent")
374     {
375         if(($curVarProperties -eq $null) -and ($currentValueFromEnv -eq $null))
376         {
377             # Variable not found, condition is satisfied and there is nothing to set/remove, return
378
379             Write-Log -Message ($localizedData.EnvVarNotFound -f $Name)
380                         
381             return
382         }
383         
384         if(!$ValueSpecified -or !$Path)
385         {
386             # If no $Value specified to be removed, simply remove the environment variable (holds true for both path and non-path variables
387             # OR
388             # Regardless of $Value, if the target variable is a non-path variable, simply remove it to meet the absent condition
389
390             $successMessage = $localizedData.EnvVarRemoved -f $Name
391
392             if ($PSCmdlet.ShouldProcess($successMessage, $null, $null))
393             {    
394                 Remove-EnvVar -Name $Name
395             }             
396
397             return
398         }
399                 
400         # If the control reaches here: target variable is an existing environment path-variable and a specified $Value needs be removed from it
401
402         # Check if an empty string or semi-colon only string has been specified as $Value. If yes, return unchanged as we don't need to remove anything.
403         $trimmedValue = $Value.Trim(";")
404         if ([String]::IsNullOrEmpty($trimmedValue))
405         {
406             Write-Log -Message ($localizedData.EnvVarPathUnchanged -f $Name, $curVarProperties.$Name)
407
408             return        
409         }
410                 
411         $finalPath = ""
412         $specifiedPaths = $trimmedValue -split ";"
413         $currentPaths = $curVarProperties.$Name -split ";"                                
414         $varAltered = $false
415
416         foreach ($subpath in $currentPaths)            
417         {
418             if (FindSubPath -QueryPath $subpath -PathList $specifiedPaths)
419             {
420                 # Found this $subpath as one of the $specifiedPaths, skip adding this to the final value/path of this variable
421                 # and mark the variable as altered.
422
423                 $varAltered = $true
424                 continue
425             }
426
427             # If the control reaches here, the current $subpath was not part of the $specifiedPaths (to be removed), 
428             # so keep this $subpath in the finalPath
429             
430             $finalPath += $subpath + ";"                            
431         }                          
432         
433         # Remove any extraneous ";" at the end (and potentially start - as a side-effect) of the $finalPath        
434         $finalPath = $finalPath.Trim(";")
435                           
436             
437         # Set the expected success message
438         $successMessage = $localizedData.EnvVarPathUnchanged -f $Name, $curVarProperties.$Name
439         if ($varAltered)
440         {
441             $successMessage = $localizedData.EnvVarPathUpdated -f $Name, $curVarProperties.$Name, $finalPath
442             
443             if ([String]::IsNullOrEmpty($finalPath))
444             {
445                 $successMessage = $localizedData.EnvVarRemoved -f $Name
446             }            
447         }
448         
449         # Handle WhatIf case and update resource as appropriate                
450         if ($PSCmdlet.ShouldProcess($successMessage, $null, $null))
451         {    
452             # Finally, update the environment path-variable
453
454             if ([String]::IsNullOrEmpty($finalPath))
455             {
456                 Remove-EnvVar -Name $Name
457             }
458             else
459             {
460                 Set-EnvVar -Name $Name -Value $finalPath
461             }
462
463             if($err)
464             {
465                 Write-Log -Message ($localizedData.EnvVarPathRemoveError -f $Value, $Name, $curVarProperties.$Name)
466
467                 throw $err
468             }
469         } 
470     }
471 }
472
473
474 #-------------------------------
475 # The Test-TargetResource cmdlet
476 #-------------------------------
477 FUNCTION Test-TargetResource
478 {
479     param
480     (           
481         [parameter(Mandatory = $true)]
482         [ValidateNotNullOrEmpty()]
483         [System.String]
484         $Name,
485         
486         [ValidateNotNull()]
487         [System.String]
488         $Value,
489
490         [ValidateSet("Present", "Absent")]
491         [System.String]
492         $Ensure = "Present",
493         
494         [System.Boolean]
495         $Path = $false
496     )
497     
498     $ValueSpecified = $PSBoundParameters.ContainsKey("Value")
499     $curVarProperties = GetItemProperty $EnvVarRegPathMachine -Name $Name -Expand:(-not $Path) -ErrorAction SilentlyContinue
500     $currentValueFromEnv = GetEnvironmentVariable -Name $name -Target $EnvironmentVariableTarget.Process
501
502     # ----------------
503     # ENSURE = PRESENT
504     if ($Ensure -ieq "Present")
505     {        
506         if (($curVarProperties -eq $null) -or (($currentValueFromEnv -eq $null) -and ($curVarProperties.$Name -ne [string]::Empty)) )
507         {
508             # Variable not found, return failure
509
510             Write-Verbose ($localizedData.EnvVarNotFound -f $Name)
511
512             return $false
513         }
514
515         if (!$ValueSpecified)
516         {
517             # No value has been specified for test, so the existence of the variable means success
518
519             Write-Verbose ($localizedData.EnvVarFound -f $Name, $curVarProperties.$Name)
520
521             return $true
522         }
523         
524         if (!$Path)
525         {
526             # For this non-path variable, make sure that the specified $Value matches the current value.
527             # Success if it matches, failure otherwise
528
529             if ($Value -ceq $curVarProperties.$Name)
530             {
531                 Write-Verbose ($localizedData.EnvVarFound -f $Name, $curVarProperties.$Name)
532                 
533                 return $true                
534             }
535             else
536             {
537                 Write-Verbose ($localizedData.EnvVarFoundWithMisMatchingValue -f $Name, $curVarProperties.$Name, $Value)
538
539                 return $false
540             }
541         }             
542                        
543         # If the control reaches here, the expected environment variable exists, it is a path variable and a $Value is specified to test against
544                 
545         if (FindPath -ExistingPaths $curVarProperties.$Name -QueryPaths $Value -FindCriteria All)
546         {
547             # The specified path was completely present in the existing environment variable, return success
548
549             Write-Verbose ($localizedData.EnvVarFound -f $Name, $curVarProperties.$Name)
550
551             return $true
552         }   
553                     
554         # If the control reached here some part of the specified path ($Value) was not found in the existing variable, return failure
555                 
556         Write-Verbose ($localizedData.EnvVarFoundWithMisMatchingValue -f $Name, $curVarProperties.$Name, $Value)
557
558         return $false 
559     }
560
561     # ---------------
562     # ENSURE = ABSENT
563     elseif ($Ensure -eq "Absent")
564     {
565         if(($curVarProperties -eq $null) -and ($currentValueFromEnv -eq $null))
566         {
567             # Variable not found (path/non-path and $Value both do not matter then), return success
568
569             Write-Verbose ($localizedData.EnvVarNotFound -f $Name)
570
571             return $true
572         }
573
574         if (!$ValueSpecified)
575         {
576             # Given no value has been specified for test, the mere existence of the variable fails the test
577
578             Write-Verbose ($localizedData.EnvVarFound -f $Name, $curVarProperties.$Name)
579
580             return $false
581         }
582
583         # If the control reaches here: the variable exists and a value has been specified to test against it
584                 
585         if (!$Path)
586         {            
587             # For this non-path variable, make sure that the specified value doesn't match the current value
588             # Success if it doesn't match, failure otherwise
589             
590             if ($Value -cne $curVarProperties.$Name)
591             {
592                 Write-Verbose ($localizedData.EnvVarFoundWithMisMatchingValue -f $Name, $curVarProperties.$Name, $Value)                
593                 
594                 return $true                
595             }
596             else
597             {
598                 Write-Verbose ($localizedData.EnvVarFound -f $Name, $curVarProperties.$Name)
599
600                 return $false
601             }
602         }
603                     
604         # If the control reaches here: the variable exists, it is a path variable, and a value has been specified to test against it                               
605         
606         if (FindPath -ExistingPaths $curVarProperties.$Name -QueryPaths $Value -FindCriteria Any)
607         {
608             # One of the specified paths in $Value exists in the environment variable path, thus the test fails
609
610             Write-Verbose ($localizedData.EnvVarFound -f $Name, $curVarProperties.$Name)
611
612             return $false
613         }
614                     
615         # If the control reached here, none of the specified paths were found in the existing path-variable, return success                                               
616
617         Write-Verbose ($localizedData.EnvVarFoundWithMisMatchingValue -f $Name, $curVarProperties.$Name, $Value)                
618
619         return $true        
620     }    
621 }
622
623
624 #----------------------------------------
625 # Utility to write WhatIf or Verbose logs
626 #----------------------------------------
627 FUNCTION Write-Log
628 {
629     [CmdletBinding(SupportsShouldProcess=$true)]
630     param
631     (   
632         [parameter(Mandatory = $true)]
633         [ValidateNotNullOrEmpty()]
634         [System.String]
635         $Message
636     )
637
638     if ($PSCmdlet.ShouldProcess($Message, $null, $null))
639     {
640         Write-Verbose $Message        
641     }    
642 }
643
644
645 #-----------------------------------
646 # Utility to match environment paths
647 #-----------------------------------
648 FUNCTION FindPath
649 {    
650     param
651     (                           
652         [System.String]
653         $ExistingPaths,
654         
655         [System.String]
656         $QueryPaths,
657
658         [parameter(Mandatory = $true)]          
659         [ValidateSet("Any", "All")]
660         [System.String]
661         $FindCriteria
662     )
663
664     $existingPathList = $ExistingPaths -split ";"
665     $queryPathList = $QueryPaths -split ";"
666
667     switch ($FindCriteria)
668     {
669         "Any"
670         {
671             foreach ($queryPath in $queryPathList)
672             {            
673                 if (FindSubPath -QueryPath $queryPath -PathList $existingPathList)
674                 {
675                     # Found this $queryPath in the existing paths, return $true
676                     return $true
677                 }                             
678             }
679
680             # If the control reached here, none of the $QueryPaths were found as part of the $ExistingPaths, return $false
681             return $false   
682         }
683
684         "All"
685         {
686             foreach ($queryPath in $queryPathList)
687             {
688                 $found = $false
689                 if($queryPath) 
690                 {
691                     if (!(FindSubPath -QueryPath $queryPath -PathList $existingPathList))
692                     {
693                         # The current $queryPath wasn't found in any of the $existingPathList, return failure                    
694                         return $false
695                     }
696                 }                
697             }
698
699             # If the control reached here, all of the $QueryPaths were found as part of the $ExistingPaths, return $true
700             return $true
701         }    
702     }
703 }
704
705
706 #---------------------------------------
707 # Utility to search a path in a pathlist
708 #---------------------------------------
709 FUNCTION FindSubPath
710 {    
711     param
712     (
713         [System.String]
714         $QueryPath,
715                 
716         [String[]]
717         $PathList
718     )
719     
720     foreach ($path in $PathList)
721     {
722         if($QueryPath -ieq $path)
723         {
724             # If the query path matches any of the paths in $PathList, return $true
725             return $true
726         }                
727     }     
728     
729     return $false        
730 }
731
732 #---------------------------------------------------------------
733 # Utility to get item property without expanding it if necessary
734 #---------------------------------------------------------------
735 FUNCTION GetItemProperty
736 {
737     param
738     (
739         [parameter(Mandatory = $true)]
740         [ValidateNotNullOrEmpty()]
741         [System.String]
742         $Path,
743         
744         [ValidateNotNull()]
745         [System.String]
746         $Name,
747         
748         [switch]
749         $Expand = $false
750     )
751
752     if ($Expand)
753     {
754         return (Get-ItemProperty $EnvVarRegPathMachine -Name $Name -ErrorAction SilentlyContinue)
755     }
756     else
757     {
758         if (!(Test-Path -Path $Path))
759         {
760             return $null;
761         }
762
763         $PathTokens = $Path.Split('\',[System.StringSplitOptions]::RemoveEmptyEntries)
764         $Division = $PathTokens[0].Replace(':', '')
765         $Entry = $PathTokens[1..($PathTokens.Count-1)] -join '\'
766         
767         # Since the target registry path coming to this function is hardcoded for local machine
768         $Hive = [Microsoft.Win32.Registry]::LocalMachine
769
770         $NoteProperties = @{}
771         try
772         {
773             $Key = $Hive.OpenSubKey($Entry)
774             
775             $ValueNames = $Key.GetValueNames()
776             if ($ValueNames -inotcontains $Name)
777             {
778                 return $null
779             }
780             
781             [string] $Value = $Key.GetValue($Name, $null, [Microsoft.Win32.RegistryValueOptions]::DoNotExpandEnvironmentNames)
782             $NoteProperties.Add($Name, $Value)
783         }
784         finally
785         {
786             if ($key)
787             {
788                 $key.Close()
789             }
790         }
791
792         [System.Management.Automation.PSObject] $PropertyResults = New-Object -TypeName System.Management.Automation.PSObject -Property $NoteProperties
793
794         return $PropertyResults
795     }
796 }
797
798 Export-ModuleMember -function Get-TargetResource, Set-TargetResource, Test-TargetResource