0

I am trying to get registry key values from a remote machine using Invoke-Command. but when I am executing, it is not fetching the value. the session is getting connected successfully.

below is code

$TOD_Data= @(Import-Csv -Path "C:\data.csv")
$Remote_Server_Name = $TOD_Data.name

$cred = Get-Credential

$Session = New-PSSession -ComputerName $Remote_Server_Name -Credential $cred

$regkey = "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Run"
$regparam = "LOGINventory Offline Agent 8"
try
{
if (Invoke-Command -Session $Session -ScriptBlock {Get-ItemProperty -Path $regkey -Name $regparam -ErrorAction Ignore})
{
    Write-Host "Found"}
else
{
    Write-Host "Registry Key Not Found.."
}
}catch{write-host "$Error.Exception.Message" -BackgroundColor Red -ForegroundColor White}

Please do let me know on this

Empty Coder
  • 379
  • 1
  • 9
  • 1
    `$regkey` and `$regparam` do not exist in the scope of your `Invoke-Command`, either refer to them using the `$using:` scope modifier or use `-ArgumentList`. See linked duplicate for details. – Santiago Squarzon May 10 '22 at 15:07

0 Answers0