H
H
hixr0k2021-06-12 12:36:04
PowerShell
hixr0k, 2021-06-12 12:36:04

How to solve the problem with displaying values ​​from the registry through Invoke-Command?

Good afternoon. There is a script with which I try to display license keys from CryptoPro (FIND_CRYPTOPRO_KEY) and CryptoARM (FIND_CRYPTOARM_KEY) on remote PCs from the registry.

Script
$compname = 'AST' 
$cripto5 = 'Registry::HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Installer\UserData\S-1-5-18\Products\08F19F05793DC7340B8C2621D83E5BE5\InstallProperties' 
$cripto4 = 'Registry::HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Installer\UserData\S-1-5-18\Products\7AB5E7046046FB044ACD63458B5F481C\InstallProperties'
$kriptoarm = 'Registry::HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\Digt\Trusted Desktop\License' 


Write-Output FIND_CRYPTOARM:(Invoke-Command -ComputerName $compname -ScriptBlock { Test-Path -Path $using:kriptoarm })
Write-Output FIND_CRYPTOPRO4:(Invoke-Command -ComputerName $compname -ScriptBlock { Test-Path -Path $using:cripto4 })   
Write-Output FIND_CRYPTOPRO5:(Invoke-Command -ComputerName $compname -ScriptBlock { Test-Path -Path $using:cripto5 }) 

Write-Output FIND_CRYPTOPRO_KEY
if (Invoke-Command -ComputerName $compname -ScriptBlock { Test-Path -Path $using:cripto5 }) { 
    Invoke-Command -ComputerName $compname  -ScriptBlock { Get-ItemProperty -Path $using:cripto5 -Name ProductID } | Select-Object "ProductID"  
}
elseif (Invoke-Command -ComputerName $compname -ScriptBlock { Test-Path -Path $using:cripto4 }) { 
    Invoke-Command -ComputerName $compname  -ScriptBlock { Get-ItemProperty -Path $using:cripto4 -Name ProductID } | Select-Object "ProductID"  
}
else {
    Write-Output CryptoPro not found
}

Write-Output ([System.Environment]::NewLine)
Write-Output FIND_CRYPTOARM_KEY
if (Invoke-Command -ComputerName $compname -ScriptBlock { Test-Path -Path $using:kriptoarm }) { 
    Invoke-Command -ComputerName $compname  -ScriptBlock { Get-ItemProperty -Path $using:kriptoarm -Name SerialNumber } | Select-Object "SerialNumber"  
}
else {
    Write-Output CryptoARM not found
}


But I ran into a problem, when working out the script, only the key from CryptoARM is displayed, and the CryptoPro key is not displayed. But it seems to me that the key to CryptoPRO is still found.
60c47d48ecb13995249248.png
After I changed the script and first set the search for the CryptoPro key, and then CryptoARM, the situation became the opposite. The CryptoPRO key began to be displayed, but the CryptoARM key did not.
60c47f4bf021d950385245.png

I can't figure out what the problem is.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
M
MaxKozlov, 2021-06-12
@hixr0k

It's easier to drive everything into one call once.
So it will work many times faster and you can run it simultaneously on several computers, only you need to include the name of the computer in the output

invoke-command -comp $comp {
  # а здесь все красоты
  # причём циклом
  $source @(
    @{ key name= '''; valuename=''},
    @{ key name= '''; valuename=''}
  )
  foreach ($src in $source) {
    get-itemproperty -path $src.keyname | select -expandproperty $src.valuename
  }
}

test omitted...
The problem you have in select-object
is that the standard call, if it's with properties, tries to adjust the subsequent output to match the previous returned properties. if you first ask for the "ProductID" property, then in the subsequent call to PS, the ProductID property is waiting, which is not there, you slip it "SerialNumber"
For this, in my version, the ExpandProperty parameter is used, which turns the object into a string

A
Alexey Dmitriev, 2021-06-12
@SignFinder

Execute the script block by block - check that each block does what you need.

A
Andrew AT, 2021-06-12
@AAT666

There are also such glitches.
Either output icm to a variable, or via write-output.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question