-1

好的,到目前为止,关于 PowerShell 的一切都非常棒,但是对于非常棒的东西,它们确实使将结果导出到文件变得非常复杂。无论如何,如何将Export $Results变量保存到分隔文件中,以便可以将其导入Excel

最终脚本

[cmdletbinding()]

[cmdletbinding()]
param(
    [parameter(ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true)]
    [string[]]$ComputerName = "HellBombs-PC"
)

begin {
    $UninstallRegKey="SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall"
}

process {
    foreach($Computer in $ComputerName) {
        Write-Verbose "Working on $Computer"
        if(Test-Connection -ComputerName $Computer -Count 1 -ea 0) {
            $HKLM   = [microsoft.win32.registrykey]::OpenRemoteBaseKey('LocalMachine',$computer)
            $UninstallRef  = $HKLM.OpenSubKey($UninstallRegKey)
            $Applications = $UninstallRef.GetSubKeyNames()

            foreach ($App in $Applications) {
                $AppRegistryKey  = $UninstallRegKey + "\\" + $App
                $AppDetails   = $HKLM.OpenSubKey($AppRegistryKey)
                $AppGUID   = $App
                $AppDisplayName  = $($AppDetails.GetValue("DisplayName"))
                $AppVersion   = $($AppDetails.GetValue("DisplayVersion"))
                $AppPublisher  = $($AppDetails.GetValue("Publisher"))
                $AppInstalledDate = $($AppDetails.GetValue("InstallDate"))
                $AppUninstall  = $($AppDetails.GetValue("UninstallString"))
                if(!$AppDisplayName) {
                    continue
                }
                $OutputObj = New-Object -TypeName PSobject
                $OutputObj | Add-Member -MemberType NoteProperty -Name ComputerName -Value $Computer.ToUpper()
                $OutputObj | Add-Member -MemberType NoteProperty -Name AppName -Value $AppDisplayName
                $OutputObj | Add-Member -MemberType NoteProperty -Name AppVersion -Value $AppVersion
                $OutputObj | Add-Member -MemberType NoteProperty -Name AppVendor -Value $AppPublisher
                $OutputObj | Add-Member -MemberType NoteProperty -Name InstalledDate -Value $AppInstalledDate
                $OutputObj | Add-Member -MemberType NoteProperty -Name UninstallKey -Value $AppUninstall
                $OutputObj | Add-Member -MemberType NoteProperty -Name AppGUID -Value $AppGUID
                $Result += @($OutputObj)
            }
        }
    }
    $Result | select -Property * | export-csv -notypeinformation -path Info.txt
}

end {}
4

1 回答 1

2

使用export-csv

尝试:

$Result | select -Property * | export-csv -notypeinformation -append -path .\test.txt   
于 2012-11-03T06:59:13.690 回答