Home > other >  Export reg value to csv
Export reg value to csv

Time:10-02

i have 1 question:

i need verify 3 reg key on 20 pc and export result on csv file.

I used this string

Get-ItemProperty -Path hklm:"\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon\" -Name "keyname" | Export-csv -path "csvpath"

and recive the all value for thi key but i don't need see the "PSPath, PSParentPath, PSChildName, PSDrive, PSProvider.

now i was thinking of making a script with variables to simplify it, but at this point i would like it to tell me even if the key was not found and the basic thing i can run it from the DC to all machines (about 20).

this could be a starting point

$key1 = name key 1

$key2 = name key 2

$key3 = name key 3

$hostname= hostname

$regkey= get-itemprperty -path ecc....

and now i'm seeing how you implement the verification loop and export everything to csv

thx

CodePudding user response:

To verify the key existence, use Test-Path.

Computer names and Key names as arrays of strings.

No experience with remoting, I think you'll be using Invoke-Command, but this should give you an idea of looping and getting all non-PS properties:

Computer1
Computer2
Computer3
'@ -split '\n'

$keyNames = @'
KeyName1
KeyName2
KeyName3
`@ -split '\n'

ForEach ( $Comoputer in $Computers) {
    ForEach ( $KeyName in $KeyNames ) {
        If ( Test-Path $KeyName )
        {
            $AllProps = ($key = Get-Item $KeyName).Property
            (Get-ItemProperty $key).PSobject.Properties | where name -in $AllProps | select Name , Value
            <<  Create output   >>
        }
        Else
        {
            "$ComputerName: $KeyName not found."
        }
    }
} | Export-Csv "\\Path\to\CsvFile"

CodePudding user response:

To probe multiple computers for 3 registry properties and output the result in a CSV file, you can use Invoke-Command like below:

$computers     = 'pc01','pc02','pc03'  # etc. the 20 computers you want to probe
$propertynames = 'property1','property2','property3'  # you may use wildcards here

# loop over the computers
$result = foreach ($computer in $computers) {
    if (!(Test-Connection -ComputerName $computer -Count 1 -Quiet)) {
        Write-Warning "Computer '$computer' is not responding"
        continue   # skip this computer and proceed with the next
    }
    Invoke-Command -ComputerName $computer -ScriptBlock {
        $regPath = "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon"
        # create a temporary Hashtable to store the items
        $hash = [ordered]@{}
        # loop over the properties
        foreach ($prop in $using:propertynames) {
            $entry = Get-ItemProperty -Path $regPath -Name $prop -ErrorAction SilentlyContinue
            if ($entry) {
                $hash['ComputerName'] = $using:computer
                $entry = $entry | Select-Object * -ExcludeProperty PS*
                # use a loop in case you have used wildards for the property names 
                foreach ($item in $entry.PsObject.Properties) {
                    $hash[$item.Name] = $item.Value
                }
            }
            else {
                Write-Warning "Could not find property '$prop'"
            }
        }
        if ($hash.Count) {
            # output the hash converted to PSObject
            [PsCustomObject]$hash
        }
    }
}

# remove the properties added by Invoke-Command
$result = $result | Select-Object * -ExcludeProperty PS*,RunspaceId

# output to gridview
$result | Out-GridView

# output to CSV file
$result | Export-Csv -Path 'X:\Path\To\TheResults.csv' -NoTypeInformation
  • Related