Hi Team,
I am in the process of getting the remote registry value on remote servers.My requirement is to export the result to csv or notepad
and excepting result should be like below
PSComputerName HttpCompatibilityListener
-------------- -------------------------
Server1 0
Server2 HttpCompatibilityListener Value not exist
Server3 HttpCompatibilityListener Value not exist
Server4 1
I am trying below code. But some servers will have registry path exists but value might be missing. I am not getting how to write the code using nested if.
$servers = Get-Content -Path C:\users\user1\Desktop\servers.txt
$ro =@()
ForEach ($server in $servers)
{
$RS = invoke-command -ComputerName $server -scriptblock { Test-path 'HKLM:\Software\Policies\Microsoft\Windows\WinRM\Service' }
$sr= {Get-ItemProperty 'HKLM:\Software\Policies\Microsoft\Windows\WinRM\Service' -name HttpCompatibilityListener }
IF ($RS -eq $true){
#Write-Host "path exist on $server"
$ro += Invoke-Command -ComputerName $server -ScriptBlock $sr | Select-Object PSComputerName,HttpCompatibilityListener
$r = $ro | Export-Csv -Path "c:\users\user1\\desktop\t.csv" -NoTypeInformation
}
else
{
Write-output "path not exist on $server"
}
}