需要帮助从文本文件读取并输出到csv

时间:2019-10-28 16:54:12

标签: powershell

$sb = {
    write-host "Get_Registry_Value.ps1"
    Get-ChildItem HKLM:\HKEY_LOCAL_MACHINE\SOFTWARE\McAfee\AVSolution\DS -Recurse |
    ForEach-Object { Get-ItemProperty $_.pspath }
    }
Invoke-Command -ScriptBlock $sb -ComputerName xxx-xxx

1 个答案:

答案 0 :(得分:0)

这是从多个系统的注册表中获取信息的一种方法。由于我没有任何mcaffee软件,因此我使用Python作为占位符。代码注释似乎足够,但是请随时进行澄清。 [咧嘴]

#requires -RunAsAdministrator

# fake reading in a list of systems
#    in real life, use Get-Content
$SystemList = @'
LocalHost
10.0.0.1
127.0.0.1
BetterNotBeThere
'@ -split [System.Environment]::NewLine

$sb = {
    $RegPath = 'HKLM:\SOFTWARE\Python\PythonCore\3.8'
    Get-ChildItem -LiteralPath  $RegPath -Recurse |
        ForEach-Object {
            Get-ItemProperty $_.pspath
            }
    }

# the "-ErrorAction SilentlyContinue" will make PoSh NOT show error on the screen
#    if you need to get a list of non-responders, you can compare the input list to the items in "$Results"
$Results = Invoke-Command -ScriptBlock $sb -ComputerName $SystemList -ErrorAction SilentlyContinue

$Results |
    # filter out the items with no path info or lots of extra properties
    Where-Object {
        $_.'(default)'
        } |
    # remove unwanted properties & add a ComputerName prop
    Select-Object -Property @{n = 'ComputerName'; e = {$_.PSComputerName}}, * -ExcludeProperty 'RunspaceId', 'PSShowComputerName' |
    Export-Csv -LiteralPath "$env:TEMP\powershelltek_HKLM-Software-Info.csv" -NoTypeInformation

$ Results中的第一项...

(default)      : C:\Python38\Doc\python380.chm
PSPath         : Microsoft.PowerShell.Core\Registry::HKEY_LOCAL_MACHINE\SOFTWARE\Python\PythonCore\3.8\Help\Main Python 
                 Documentation
PSParentPath   : Microsoft.PowerShell.Core\Registry::HKEY_LOCAL_MACHINE\SOFTWARE\Python\PythonCore\3.8\Help
PSChildName    : Main Python Documentation
PSProvider     : Microsoft.PowerShell.Core\Registry
PSComputerName : LocalHost
RunspaceId     : a915a91a-5cfd-405f-ac0a-b3350cd90942

CSV文件的内容...

"ComputerName","(default)","PSPath","PSParentPath","PSChildName","PSProvider","PSComputerName"
"LocalHost","C:\Python38\Doc\python380.chm","Microsoft.PowerShell.Core\Registry::HKEY_LOCAL_MACHINE\SOFTWARE\Python\PythonCore\3.8\Help\Main Python Documentation","Microsoft.PowerShell.Core\Registry::HKEY_LOCAL_MACHINE\SOFTWARE\Python\PythonCore\3.8\Help","Main Python Documentation","Microsoft.PowerShell.Core\Registry","LocalHost"
"LocalHost","C:\Python38\Lib\idlelib\idle.pyw","Microsoft.PowerShell.Core\Registry::HKEY_LOCAL_MACHINE\SOFTWARE\Python\PythonCore\3.8\Idle","Microsoft.PowerShell.Core\Registry::HKEY_LOCAL_MACHINE\SOFTWARE\Python\PythonCore\3.8","Idle","Microsoft.PowerShell.Core\Registry","LocalHost"
"LocalHost","1","Microsoft.PowerShell.Core\Registry::HKEY_LOCAL_MACHINE\SOFTWARE\Python\PythonCore\3.8\IdleShortcuts","Microsoft.PowerShell.Core\Registry::HKEY_LOCAL_MACHINE\SOFTWARE\Python\PythonCore\3.8","IdleShortcuts","Microsoft.PowerShell.Core\Registry","LocalHost"
"LocalHost","C:\Python38\","Microsoft.PowerShell.Core\Registry::HKEY_LOCAL_MACHINE\SOFTWARE\Python\PythonCore\3.8\InstallPath","Microsoft.PowerShell.Core\Registry::HKEY_LOCAL_MACHINE\SOFTWARE\Python\PythonCore\3.8","InstallPath","Microsoft.PowerShell.Core\Registry","LocalHost"
"LocalHost","C:\Python38\Lib\;C:\Python38\DLLs\","Microsoft.PowerShell.Core\Registry::HKEY_LOCAL_MACHINE\SOFTWARE\Python\PythonCore\3.8\PythonPath","Microsoft.PowerShell.Core\Registry::HKEY_LOCAL_MACHINE\SOFTWARE\Python\PythonCore\3.8","PythonPath","Microsoft.PowerShell.Core\Registry","LocalHost"
"127.0.0.1","C:\Python38\Doc\python380.chm","Microsoft.PowerShell.Core\Registry::HKEY_LOCAL_MACHINE\SOFTWARE\Python\PythonCore\3.8\Help\Main Python Documentation","Microsoft.PowerShell.Core\Registry::HKEY_LOCAL_MACHINE\SOFTWARE\Python\PythonCore\3.8\Help","Main Python Documentation","Microsoft.PowerShell.Core\Registry","127.0.0.1"
"127.0.0.1","C:\Python38\Lib\idlelib\idle.pyw","Microsoft.PowerShell.Core\Registry::HKEY_LOCAL_MACHINE\SOFTWARE\Python\PythonCore\3.8\Idle","Microsoft.PowerShell.Core\Registry::HKEY_LOCAL_MACHINE\SOFTWARE\Python\PythonCore\3.8","Idle","Microsoft.PowerShell.Core\Registry","127.0.0.1"
"127.0.0.1","1","Microsoft.PowerShell.Core\Registry::HKEY_LOCAL_MACHINE\SOFTWARE\Python\PythonCore\3.8\IdleShortcuts","Microsoft.PowerShell.Core\Registry::HKEY_LOCAL_MACHINE\SOFTWARE\Python\PythonCore\3.8","IdleShortcuts","Microsoft.PowerShell.Core\Registry","127.0.0.1"
"127.0.0.1","C:\Python38\","Microsoft.PowerShell.Core\Registry::HKEY_LOCAL_MACHINE\SOFTWARE\Python\PythonCore\3.8\InstallPath","Microsoft.PowerShell.Core\Registry::HKEY_LOCAL_MACHINE\SOFTWARE\Python\PythonCore\3.8","InstallPath","Microsoft.PowerShell.Core\Registry","127.0.0.1"
"127.0.0.1","C:\Python38\Lib\;C:\Python38\DLLs\","Microsoft.PowerShell.Core\Registry::HKEY_LOCAL_MACHINE\SOFTWARE\Python\PythonCore\3.8\PythonPath","Microsoft.PowerShell.Core\Registry::HKEY_LOCAL_MACHINE\SOFTWARE\Python\PythonCore\3.8","PythonPath","Microsoft.PowerShell.Core\Registry","127.0.0.1"