首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将杀毒信息导出到file.csv中的PowerShell

将杀毒信息导出到file.csv中的PowerShell
EN

Stack Overflow用户
提问于 2021-06-24 09:00:58
回答 2查看 89关注 0票数 0

我试图获取防病毒信息并将其导出到file.csv中。在AVName结果中,我得到了system.object[],因为它是一个数组。有什么办法解决吗?

代码语言:javascript
复制
Get-Content "C:\Temp\computers.txt"  | ForEach-Object {
    $av_name=$null
    $errorMsg=''
    Try {
        $av_name = (Get-CimInstance -Namespace root/SecurityCenter2 -ClassName AntivirusProduct).displayName
    }
    catch {
        $errorMsg=$_.Exception.Message
    }
    if(!$av_name) {
        $av_name = "The machine is not protected $errorMsg"
        $av_name = "The machine is not protected"
    }
    else {
        $av_name = (Get-CimInstance -Namespace root/SecurityCenter2 -ClassName AntivirusProduct).displayName
    }
    New-Object -TypeName PSObject -Property @{
        ComputerName = $_
        AVName = $av_name
    } | Select-Object ComputerName,AVName | Export-Csv "C:\Temp\AV_Details.csv" -NoTypeInformation
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2021-06-24 10:17:42

基本上,错误是Export-Csv cmdlet是一个关闭的卷曲括号过高。

此外,不需要执行两次(Get-CimInstance -Namespace root/SecurityCenter2 -ClassName AntivirusProduct).displayName,并且在输出对象时不需要使用Select-Object,只需要使用之后选择的两个属性。

试一试

代码语言:javascript
复制
Get-Content "C:\Temp\computers.txt"  | ForEach-Object {
    # store this in a variable, because $_ will become the exception object inside the catch when things go wrong
    $computer = $_  
    $av_name  = $null
    $errorMsg = ''
    Try {
        $av_name = (Get-CimInstance -Namespace root/SecurityCenter2 -ClassName AntivirusProduct -ComputerName $computer).displayName -join '; '
    }
    catch {
        $errorMsg = $_.Exception.Message
    }
    if(!$av_name) {
        $av_name = "The machine is not protected $errorMsg"
    }

    [PsCustomObject]@{
        ComputerName = $computer
        AVName       = $av_name
    }
} | Export-Csv "C:\Temp\AV_Details.csv" -NoTypeInformation

在我的机器上,如果computers.txt文件仅包含IP地址,则输出如下:

代码语言:javascript
复制
"ComputerName","AVName"
"192.168.0.10","Windows Defender"
票数 0
EN

Stack Overflow用户

发布于 2021-06-24 09:42:55

转换到-Csv

把这个加在这里

代码语言:javascript
复制
} | Select-Object ComputerName,AVName |*AddMe*| Export-Csv "C:\Temp\AV_Details.csv" -NoTypeInformation
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68112701

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档