Remote Powershell to retrieve specific registry va

2019-09-02 19:26发布

I have the following..

$output = @()
$servers =Get-Content "C:\Windows\System32\List3.txt"

foreach ($server in $servers)
{
    trap [Exception] {continue}
    Import-Module PSRemoteRegistry
    $key="SOFTWARE\Microsoft\'Microsoft Antimalware'\'Signature Updates'"

    $regkey=Get-RegBinary -ComputerName $server -Key $Key -Value SignatuesLastUpdated

    #$regkey=(Get-Item HKLM:\SOFTWARE\Microsoft\'Microsoft Antimalware'\'Signature Updates').getValue('SignaturesLastUpdated')

    #$regkey=[datetime]::ParseExact("01/02/03", "dd/MM/yy", $null) | Export-csv -path c:\temp\avinfo.csv -append

    #$regkey
}

$output | Select $server , $Regkey | Export-Csv c:\temp\avinfo.csv -NoTypeInformation

I think it's pretty close but doesn't work as needed - can anyone tell me what I am doing wrong here - been reading a lot and managed to get this far, just need the help to finalise.

Thanks

1条回答
看我几分像从前
2楼-- · 2019-09-02 20:24

Ok... so there is alot that needed to be changed to get this to work. I will update the answer frequently after this is posted.

$servers = Get-Content "C:\Windows\System32\List3.txt"
$key="SOFTWARE\Microsoft\Microsoft Antimalware\Signature Updates"

$servers | ForEach-Object{
    $server = $_
    Try{
        Get-RegBinary -ComputerName $server -Key $Key -Value SignatuesLastUpdated -ErrorAction Stop
    } Catch [exception]{
        [pscustomobject]@{
            ComputerName = $server
            Data = "Unable to retrieve data"
        }
    }
} | Select ComputerName,@{Label=$value;Expression={If(!($_.Data -is [string])){[System.Text.Encoding]::Ascii.GetBytes($_.data)}Else{$_.Data}}} | Export-Csv c:\temp\avinfo.csv -NoTypeInformation

What the above code will do is more in line with your intentions. Take the list and for each item get the key data from that server. If there is an issue getting that data then we output a custom object stating that so we can tell in the output if there was an issue. The part that is up in the air is how you want to export the binary data to file. As it stands it should create a space delimited string of the bytes.

The issues that you did have that should be highlighted are

  1. No need to import the module for every server. Moved that call out of the loop
  2. You have declared the variable $output but do not populate it during your loop process. This is important for the foreach construct. You were, in the end, sending and empty array to you csv. My answer does not need it as it just uses standard output.
  3. As @Meatspace pointed out you had a typo here: SignatuesLastUpdated
  4. Get-RegBinary does not by default create terminating errors which are needed by try/catch blocks. Added -ErrorAction Stop. Don't think your code trap [Exception] {continue} would have caught anything.
  5. The single quotes you have in your $key might have prevented the path from being parsed. You were trying to escape spaces and just need to enclose the whole string in a set of quotes to achieve that.
  6. While Select can use variables they are there, in a basic form, to select property names. In short what you had was wrong.
查看更多
登录 后发表回答