0

Good day, I have the following script below which i am using to get a specific value from a remote pc within the company network.

I am stuck on where I would add the export-csv function so that i can export all the data into a csv file.

any help would be appreciated.

$computers = Get-Content "c:\temp\Servers.txt"

$key = 'SOFTWARE\Fortinet\FortiClient\Sslvpn\Tunnels\RCL SSL VPN'
$valuename = 'Server'

$computers = Get-Content Servers.txt
foreach ($computer in $computers) {
    
    $reg = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey('LocalMachine', $computer)
    $MachineName = $computer
    $regkey = $reg.opensubkey($key)
    Write-host $MachineName "," $regkey.getvalue($valuename) `r`n

}
Bacon Bits
  • 30,782
  • 5
  • 59
  • 66
Bob
  • 1
  • 1
  • It's usually not a good practice to stick `Export-Csv` in your script but, it's not a crime. Lol I'd suggest throwing the data into a *PSCustomobject* and exporting it after that. You can't do so after your `write-host` as it only writes to the console (*host*) – Abraham Zinala Jul 08 '21 at 13:17

2 Answers2

0

You could use PsCustomObject and then add to it with each loop

$computers = Get-Content "c:\temp\Servers.txt"
$objarray = @()

$key = 'SOFTWARE\Fortinet\FortiClient\Sslvpn\Tunnels\RCL SSL VPN'
$valuename = 'Server'

$computers = Get-Content Servers.txt
foreach ($computer in $computers) {
    
    $reg = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey('LocalMachine', $computer)
    $MachineName = $computer
    $regkey = $reg.opensubkey($key)
    Write-host $MachineName "," $regkey.getvalue($valuename) `r`n

    $objArray += [PSCustomObject]@{
    'MachineName' = $computer
    'RegKey' = $RegKey             
    }

}

$objarray | export-csv c:\export.csv
Aziz
  • 283
  • 1
  • 14
  • 2
    [Try to avoid using the increase assignment operator (`+=`) to create a collection](https://stackoverflow.com/a/60708579/1701026) as it exponentially expensive – iRon Jul 08 '21 at 14:18
0

Output objects inside the loop and pipe these through at the very end to the Export-Csv cmdlet like this:

$computers = Get-Content "c:\temp\Servers.txt"

$key = 'SOFTWARE\Fortinet\FortiClient\Sslvpn\Tunnels\RCL SSL VPN'
$valuename = 'Server'

$computers = Get-Content Servers.txt
$result = foreach ($computer in $computers) {
    # do this in a Try{..] Catch {..} construct
    try {
        $regBase = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey('LocalMachine', $computer)
        $regkey = $regBase.OpenSubKey($key)
        # output an object with the properties you want
        [PsCustomObject]@{'MachineName' = $computer; RegistryValue = $regkey.GetValue($valuename) }
    }
    catch {
        Write-Warning "Could not open registry on server '$computer'"
        # output an object with the error
        [PsCustomObject]@{'MachineName' = $computer; RegistryValue = "Error opening registry.." }
    }
    # for good measure, Close the opened registry
    finally {
        if ($regkey) { $regkey.Close() }
        if ($regBase) { $regBase.Close() }
    }
}

# output on screen
$result | Format-Table -AutoSize

#output to CSV file
$result | Export-Csv -Path 'c:\temp\ServerRegistryValues.csv' -NoTypeInformation
Theo
  • 57,719
  • 8
  • 24
  • 41