Powershell Microsoft.win32.registrykey Openremotebasekey Credentials

Assigning Permissions to a Registry Key. Click the key that you want to assign permissions. On the Edit menu, click Permissions. Click the group or user name that you want to work with. Powershell to get the list of Disconnected mailbox in the Exchange Server; PowerShell Script to copy Exchange GUID from Office 365 to Exchange On-prem User. Powershell to check if Account is Enable or Disabled. Powershell to Export list of Permission given to the mailbox to CSV file; Exchange 2010 DAG local and Site DR/Failover and Fail back.

As an IT pro I frequently need to read and write to registry keys on remote computers, either ad-hoc or via script. Sure I could use Regedit, or RDP to the server in question, but that involves a lot of clicking, and to be honest, moving my right hand to my mouse seems like such hard work 🙂

Registrykey

Torrent virtual guitarist 2 h2o crack. I though I’d show you a number of ways of doing this, as well as their limitations, as well as my personal favourite.

Option 1 – Get-ItemProperty

The Powershell cmdlet Get-ItemProperty can be used in conjunction with Invoke-Command to execute a command on a remote computer.

2
Invoke-Command-scriptblock{Get-ItemProperty-Path'HKLM:SoftwareMicrosoftWindows NTCurrentVersion

The nice thing about this command is you can also specify alternate credentials. However, it does require that WsManis correctly configured for powershell remoting to work. Which, 9 times out of 10 in most environments it is not.

Option 2 – The Microsoft.Win32.RegistryKey Class

The Microsoft.Win32.RegistryKey class is another way of accessing registry settings remotely. An example using this method is as follows:

2
$reg=[Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey('LocalMachine','Server01')
$subkey=$reg.OpenSubKey('SOFTWAREMicrosoftWindows NTCurrentVersion')

This is more likely to work as it is not reliant on WsMan being configured. However, it does require the RemoteRegistryservice to be running on the target computer. Which, by default is not. Also, there is no way to specify alternatecredentials, which can present a few problems depending on the computer you are talking to. e.g. non domain-joined machines mounted on the back of a 42″ LED TV mounted 5 meters off the floor… Quite a specific example there.

Option 3 (preferred) – WMI

My final and preferred option is using WMI. They beautiful thing about this method is WMI is typically available (I’m my experience) in most environments, also it accepts alternative credentials and is not reliant on the RemoteRegistry service.

2
4
6
$reg=Get-WmiObject-List-Namespacerootdefault-ComputerName RemotePC-Credential'Alt Credentials'|Where-Object{$_.Name-eq'StdRegProv'}
#Get a Value
$reg.GetStringValue($HKLM,'SOFTWAREMicrosoftWindows NTCurrentVersion','ProductName').sValue

You will be prompted for alternate credentials when running this script, if you wish these can be hardcoded, although I strongly discourage saving passwords as plain text.

2
$pass=ConvertTo-SecureString'Password'-AsPlainText-Force
$cred=New-Object-TypeName System.Management.Automation.PSCredential-ArgumentList$user,$pass

You can view all the required methods of the StdRegProv WMI class on MSDN here: https://msdn.microsoft.com/en-us/library/aa393664(v=vs.85).aspx

The only downside to this option was it took me a while to stumble across it!

I hope this is of use , thanks for taking the time to read my blog!

FunctionGet-SqlServerKey
{
<#
.SYNOPSIS
Gets SQL Server Product Keys from local on destination SQL Servers. Works with SQL Server 2005-2014
.DESCRIPTION
Using a string of servers, a text file, or Central Management Server to provide a list of servers, this script will go to each server and get the product key for all installed instances. Clustered instances are supported as well. Requires regular user access to the SQL instances, SMO installed locally, Remote Registry enabled and acessible by the account running the script.
Uses key decoder by Jakob Bindslet (http://goo.gl/1jiwcB)
.PARAMETER SqlServers
A comma separated list of servers. This can be the NetBIOS name, IP, or SQL instance name
.PARAMETER SqlCms
Compiles list of servers to inventory using all servers stored within a Central Management Server. Requires having SQL Management Studio installed.
.PARAMETER ServersFromFile
Uses a text file as input. The file must be formatted as such:
sqlserver1
sqlserver2
.PARAMETER SqlCredential
Allows you to login to servers using SQL Logins as opposed to Windows Auth/Integrated/Trusted. To use:
$cred = Get-Credential, this pass this $cred to the param.
Windows Authentication will be used if DestinationSqlCredential is not specified. To connect as a different Windows user, run PowerShell as that user.
.NOTES
Author: Chrissy LeMaire (@cl), netnerds.net
Requires: PowerShell Version 3.0, SQL Server SMO, Remote Registry
dbatools PowerShell module (https://dbatools.io, clemaire@gmail.com)
Copyright (C) 2016 Chrissy LeMaire
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
.LINK
https://dbatools.io/Get-SqlServerKey
.EXAMPLE
Get-SqlServerKey winxp, sqlservera, sqlserver2014a, win2k8
Gets SQL Server versions, editions and product keys for all instances within each server or workstation.
.EXAMPLE
Get-SqlServerKey -SqlCms sqlserver01
Gets SQL Server versions, editions and product keys for all instances within sqlserver01's Central Management Server
.EXAMPLE
Get-SqlServerKey -ServersFromFile C:Scriptsservers.txt
Gets SQL Server versions, editions and product keys for all instances listed within C:Scriptsservers.txt
#>

[CmdletBinding(DefaultParameterSetName='Default')]
Param(
[parameter(Position=0)]
[Alias('ServerInstance','SqlInstance')]
[string[]]$SqlServers,
# Central Management Server
[string]$SqlCms,
# File with one server per line
[string]$ServersFromFile,
[System.Management.Automation.PSCredential]$SqlCredential
)
BEGIN
{
FunctionUnlock-SqlServerKey
{
[CmdletBinding()]
param(
[Parameter(Mandatory=$true)]
[byte[]]$data,
[int]$version
)
try
{
if($version-ge11){$binArray=($data)[0.66]}
else{$binArray=($data)[52.66]}
$charsArray='B','C','D','F','G','H','J','K','M','P','Q','R','T','V','W','X','Y','2','3','4','6','7','8','9'
for($i=24;$i-ge0;$i--)
{
$k=0
for($j=14;$j-ge0;$j--)
{
$k=$k*256-bxor$binArray[$j]
$binArray[$j]=[math]::truncate($k/24)
$k=$k%

C# Registrykey

24
}
$productKey=$charsArray[$k]+$productKey
if(($i%5-eq0)-and($i-ne0))
{
$productKey='-'+$productKey
}
}
}
catch{$productkey='Cannot decode product key.'}
return$productKey
}
}
PROCESS
{
if($SqlCms)
{
if([Reflection.Assembly]::LoadWithPartialName('Microsoft.SqlServer.Management.RegisteredServers')-eq$null)
{throw'Can't load CMS assemblies. You must have SQL Server Management Studio installed to use the -SqlCms switch.'}
Write-Output'Gathering SQL Servers names from Central Management Server'
$server=Connect-SqlServer-SqlServer$SqlCms-SqlCredential$SqlCredential
$sqlconnection=$server.ConnectionContext.SqlConnectionObject
try{$cmstore=New-ObjectMicrosoft.SqlServer.Management.RegisteredServers.RegisteredServersStore($sqlconnection)}
catch{throw'Cannot access Central Management Server'}
$dbstore=$cmstore.DatabaseEngineServerGroup
$SqlServers=$dbstore.GetDescendantRegisteredServers().servername
# Add the CM server itself, which can't be stored in the CM server.
$servers+=$SqlCms
$basenames=@()
foreach($serverin$SqlServers){$basenames+=$server.Split(')[0]}
$SqlServers=$basenames|Get-Unique
}
If($ServersFromFile)
{
if((Test-Path$ServersFromFile)-eq$false){throw'Could not find file: $ServersFromFile'}
$SqlServers=Get-Content$ServersFromFile
}
if([string]::IsNullOrEmpty($SqlServers)){$SqlServers=$env:computername}
$basepath='SOFTWAREMicrosoftMicrosoft SQL Server'
# Loop through each server
$objectCollection=@()
foreach($servernamein$SqlServers)
{
$servername=$servername.Split(')[0]
if($servername-eq'.'-or$servername-eq'localhost'-or$servername-eq$env:computername)
{
$localmachine=[Microsoft.Win32.RegistryHive]::LocalMachine
$defaultview=[Microsoft.Win32.RegistryView]::Default
$reg=[Microsoft.Win32.RegistryKey]::OpenBaseKey($localmachine,$defaultview)
}
else
{
# Get IP for remote registry access. It's the most reliable.
try{$ipaddr=([System.Net.Dns]::GetHostAddresses($servername)).IPAddressToString}
catch{Write-Warning'Can't resolve $servername. Skipping.';continue}
try
{
$reg=[Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey('LocalMachine',$ipaddr)
}
catch{Write-Warning'Can't access registry for $servername. Is the Remote Registry service started?';continue}
}
$instances=$reg.OpenSubKey('$basepathInstance NamesSQL',$false)
if($instances-eq$null){Write-Warning'No instances found on $servername. Skipping.';continue}
# Get Product Keys for all instances on the server.
foreach($instancein$instances.GetValueNames())
{
if($instance-eq'MSSQLSERVER'){$sqlserver=$servername}
else{$sqlserver='$servername$instance'}
$subkeys=$regOpensubkey.OpenSubKey('$basepath',$false)
$instancekey=$subkeys.GetSubKeynames()|Where-Object{$_-like'*.$instance'}
if($instancekey-eq$null){$instancekey=$instance}# SQL 2k5
# Cluster instance hostnames are required for SMO connection
$cluster=$reg.Powershell openbasekeyOpenSubKey('$basepath$instancekeyCluster',$false)
if($cluster-ne$null)
{
$clustername=$cluster.GetValue('ClusterName')
if($instance-eq'MSSQLSERVER'){$sqlserver=$clustername}
else{$sqlserver='$clustername$instance'}
}
Write-Verbose'Attempting to connect to $sqlserver'
try{$server=Connect-SqlServer-SqlServer$SqlServer-SqlCredential$SqlCredential}
catch{Write-Warning'Can't connect to $sqlserver or access denied. Skipping.';continue}
$servicePack=$server.ProductLevel
Write-Debug'$servername $instance version is $($server.VersionMajor)'
switch($server.VersionMajor)
{
9{
$sqlversion='SQL Server 2005 $servicePack'
$findkeys=$reg.OpenSubKey('$basepath90ProductID',$false)
foreach($findkeyin$findkeys.GetValueNames())
{
if($findkey-like'DigitalProductID*'){$key='$basepath90ProductID$findkey'}
}
}Powershell openbasekey
10{
$sqlversion='SQL Server 2008 $servicePack'
$key='$basepathMSSQL10'
if($server.VersionMinor-eq50){$key+='_50';$sqlversion='SQL Server 2008 R2 $servicePack'}
$key+='.$instanceSetupDigitalProductID'
}
11{$key='$basepath110ToolsSetupDigitalProductID';$sqlversion='SQL Server 2012 $servicePack'}
12{$key='$basepath120ToolsSetupDigitalProductID';$sqlversion='SQL Server 2014 $servicePack'}
13{$key='$basepath130ToolsSetupDigitalProductID';$sqlversion='SQL Server 2016 $servicePack'}
default{Write-Warning'SQL version not currently supported.';continue}
}
if($server.Edition-notlike'*Express*')
{
try
{
$subkey=Split-Path$key;$binaryvalue=Split-Path$key-leaf
$binarykey=$($reg.OpenSubKey($subkey)).GetValue($binaryvalue)
}
catch{$sqlkey='Could not connect.'}
try{$sqlkey=Unlock-SqlServerKey$binarykey$server.VersionMajor}
catch{}
}
else{$sqlkey='SQL Server Express Edition'}
$server.ConnectionContext.Disconnect()
$object=New-ObjectPSObject-Property@{
'SQL Instance'=$sqlserver
'SQL Version'=$sqlversion
'SQL Edition'=$server.Edition
'Product Key'=$sqlkey
}
$objectCollection+=$object
}
$reg.Close()
}
$objectCollection|Select'SQL Instance','SQL Version','SQL Edition','Product Key'
}
END
{
# Write-Output 'Script completed'
}
}