Advertisement
Guest User

Untitled

a guest
Feb 12th, 2016
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.26 KB | None | 0 0
  1. [HKLMSOFTWAREMicrosoftSystemCertificates]
  2. [HKCUSoftwareMicrosoftSystemCertificates]
  3.  
  4. Function Get-RegistryKeyTimestamp {
  5. <#
  6. .SYNOPSIS
  7. Retrieves the registry key timestamp from a local or remote system.
  8.  
  9. .DESCRIPTION
  10. Retrieves the registry key timestamp from a local or remote system.
  11.  
  12. .PARAMETER RegistryKey
  13. Registry key object that can be passed into function.
  14.  
  15. .PARAMETER SubKey
  16. The subkey path to view timestamp.
  17.  
  18. .PARAMETER RegistryHive
  19. The registry hive that you will connect to.
  20.  
  21. Accepted Values:
  22. ClassesRoot
  23. CurrentUser
  24. LocalMachine
  25. Users
  26. PerformanceData
  27. CurrentConfig
  28. DynData
  29.  
  30. .NOTES
  31. Name: Get-RegistryKeyTimestamp
  32. Author: Boe Prox
  33. Version History:
  34. 1.0 -- Boe Prox 17 Dec 2014
  35. -Initial Build
  36.  
  37. .EXAMPLE
  38. $RegistryKey = Get-Item "HKLM:SystemCurrentControlSetControlLsa"
  39. $RegistryKey | Get-RegistryKeyTimestamp | Format-List
  40.  
  41. FullName : HKEY_LOCAL_MACHINESystemCurrentControlSetControlLsa
  42. Name : Lsa
  43. LastWriteTime : 12/16/2014 10:16:35 PM
  44.  
  45. Description
  46. -----------
  47. Displays the lastwritetime timestamp for the Lsa registry key.
  48.  
  49. .EXAMPLE
  50. Get-RegistryKeyTimestamp -Computername Server1 -RegistryHive LocalMachine -SubKey 'SystemCurrentControlSetControlLsa' |
  51. Format-List
  52.  
  53. FullName : HKEY_LOCAL_MACHINESystemCurrentControlSetControlLsa
  54. Name : Lsa
  55. LastWriteTime : 12/17/2014 6:46:08 AM
  56.  
  57. Description
  58. -----------
  59. Displays the lastwritetime timestamp for the Lsa registry key of the remote system.
  60.  
  61. .INPUTS
  62. System.String
  63. Microsoft.Win32.RegistryKey
  64.  
  65. .OUTPUTS
  66. Microsoft.Registry.Timestamp
  67. #>
  68. [OutputType('Microsoft.Registry.Timestamp')]
  69. [cmdletbinding(
  70. DefaultParameterSetName = 'ByValue'
  71. )]
  72. Param (
  73. [parameter(ValueFromPipeline=$True, ParameterSetName='ByValue')]
  74. [Microsoft.Win32.RegistryKey]$RegistryKey,
  75. [parameter(ParameterSetName='ByPath')]
  76. [string]$SubKey,
  77. [parameter(ParameterSetName='ByPath')]
  78. [Microsoft.Win32.RegistryHive]$RegistryHive,
  79. [parameter(ParameterSetName='ByPath')]
  80. [string]$Computername
  81. )
  82. Begin {
  83. #region Create Win32 API Object
  84. Try {
  85. [void][advapi32]
  86. } Catch {
  87. #region Module Builder
  88. $Domain = [AppDomain]::CurrentDomain
  89. $DynAssembly = New-Object System.Reflection.AssemblyName('RegAssembly')
  90. $AssemblyBuilder = $Domain.DefineDynamicAssembly($DynAssembly, [System.Reflection.Emit.AssemblyBuilderAccess]::Run) # Only run in memory
  91. $ModuleBuilder = $AssemblyBuilder.DefineDynamicModule('RegistryTimeStampModule', $False)
  92. #endregion Module Builder
  93.  
  94. #region DllImport
  95. $TypeBuilder = $ModuleBuilder.DefineType('advapi32', 'Public, Class')
  96.  
  97. #region RegQueryInfoKey Method
  98. $PInvokeMethod = $TypeBuilder.DefineMethod(
  99. 'RegQueryInfoKey', #Method Name
  100. [Reflection.MethodAttributes] 'PrivateScope, Public, Static, HideBySig, PinvokeImpl', #Method Attributes
  101. [IntPtr], #Method Return Type
  102. [Type[]] @(
  103. [Microsoft.Win32.SafeHandles.SafeRegistryHandle], #Registry Handle
  104. [System.Text.StringBuilder], #Class Name
  105. [UInt32 ].MakeByRefType(), #Class Length
  106. [UInt32], #Reserved
  107. [UInt32 ].MakeByRefType(), #Subkey Count
  108. [UInt32 ].MakeByRefType(), #Max Subkey Name Length
  109. [UInt32 ].MakeByRefType(), #Max Class Length
  110. [UInt32 ].MakeByRefType(), #Value Count
  111. [UInt32 ].MakeByRefType(), #Max Value Name Length
  112. [UInt32 ].MakeByRefType(), #Max Value Name Length
  113. [UInt32 ].MakeByRefType(), #Security Descriptor Size
  114. [long].MakeByRefType() #LastWriteTime
  115. ) #Method Parameters
  116. )
  117.  
  118. $DllImportConstructor = [Runtime.InteropServices.DllImportAttribute].GetConstructor(@([String]))
  119. $FieldArray = [Reflection.FieldInfo[]] @(
  120. [Runtime.InteropServices.DllImportAttribute].GetField('EntryPoint'),
  121. [Runtime.InteropServices.DllImportAttribute].GetField('SetLastError')
  122. )
  123.  
  124. $FieldValueArray = [Object[]] @(
  125. 'RegQueryInfoKey', #CASE SENSITIVE!!
  126. $True
  127. )
  128.  
  129. $SetLastErrorCustomAttribute = New-Object Reflection.Emit.CustomAttributeBuilder(
  130. $DllImportConstructor,
  131. @('advapi32.dll'),
  132. $FieldArray,
  133. $FieldValueArray
  134. )
  135.  
  136. $PInvokeMethod.SetCustomAttribute($SetLastErrorCustomAttribute)
  137. #endregion RegQueryInfoKey Method
  138.  
  139. [void]$TypeBuilder.CreateType()
  140. #endregion DllImport
  141. }
  142. #endregion Create Win32 API object
  143. }
  144. Process {
  145. #region Constant Variables
  146. $ClassLength = 255
  147. [long]$TimeStamp = $null
  148. #endregion Constant Variables
  149.  
  150. #region Registry Key Data
  151. If ($PSCmdlet.ParameterSetName -eq 'ByPath') {
  152. #Get registry key data
  153. $RegistryKey = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey($RegistryHive, $Computername).OpenSubKey($SubKey)
  154. If ($RegistryKey -isnot [Microsoft.Win32.RegistryKey]) {
  155. Throw "Cannot open or locate $SubKey on $Computername"
  156. }
  157. }
  158.  
  159. $ClassName = New-Object System.Text.StringBuilder $RegistryKey.Name
  160. $RegistryHandle = $RegistryKey.Handle
  161. #endregion Registry Key Data
  162.  
  163. #region Retrieve timestamp
  164. $Return = [advapi32]::RegQueryInfoKey(
  165. $RegistryHandle,
  166. $ClassName,
  167. [ref]$ClassLength,
  168. $Null,
  169. [ref]$Null,
  170. [ref]$Null,
  171. [ref]$Null,
  172. [ref]$Null,
  173. [ref]$Null,
  174. [ref]$Null,
  175. [ref]$Null,
  176. [ref]$TimeStamp
  177. )
  178. Switch ($Return) {
  179. 0 {
  180. #Convert High/Low date to DateTime Object
  181. $LastWriteTime = [datetime]::FromFileTime($TimeStamp)
  182.  
  183. #Return object
  184. $Object = [pscustomobject]@{
  185. FullName = $RegistryKey.Name
  186. Name = $RegistryKey.Name -replace '.*\(.*)','$1'
  187. LastWriteTime = $LastWriteTime
  188. }
  189. $Object.pstypenames.insert(0,'Microsoft.Registry.Timestamp')
  190. $Object
  191. }
  192. 122 {
  193. Throw "ERROR_INSUFFICIENT_BUFFER (0x7a)"
  194. }
  195. Default {
  196. Throw "Error ($return) occurred"
  197. }
  198. }
  199. #endregion Retrieve timestamp
  200. }
  201. }
  202.  
  203. $RegistryKey = Get-Item "HKLM:<key name>"
  204. $RegistryKey | Get-RegistryKeyTimestamp | Format-List
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement