简体   繁体   English

使用Powershell导入CSV时尝试匹配字符串

[英]Trying to match strings when importing a CSV with Powershell

I am trying to create a report showing older operating systems out of a CSV file. 我正在尝试创建一个报告,以CSV文件形式显示较旧的操作系统。 The program displays the output beautifully, but doesn't actually parse anything. 该程序将输出显示得很漂亮,但实际上不解析任何内容。 I'm trying to separate out Windows 2000, 2003, XP, and yes, windows NT machines. 我正在尝试分离Windows 2000、2003,XP和Windows NT计算机。 The sample data is below: 示例数据如下:

Finding Title,IP Address / FQDN,Port,Service,OS,FQDN,Default Username,Default Password,Remediation Notes,Misc Notes,Misc Notes 2,Contact,Remediation
Default Password (yada) for 'yadayada' Account on Windows,fakefake,445,cifs,Microsoft Windows Server 2003 Service Pack 2,fakefake,yadayada,yadayada,Change the password.,,,,
Default Password (yadayada) for 'yadayada' Account on Windows,fakefake1,445,cifs,Microsoft Windows Server 2003 Service Pack 2,fakefake1,yadayada,yadayada,Change the password.,,,,
Default Password (yadayada) for 'yadayada' Account on Windows,fakefake2,445,cifs,Microsoft Windows Server 2003 Service Pack 1,fakefake2,yadayada,yadayada,Change the password.,,,,
Default Password (yadayada) for 'yadayada' Account on Windows,fakefake3,445,cifs,Microsoft Windows Server 2008 R2 Standard,fakefake3,yadayada,yadayada,Change the password.

The program I have so far is here: 我到目前为止所拥有的程序在这里:

$name = Read-Host 'CSV File?'
$csvs = import-csv $name
$object = $csvs.os


ForEach ( $object in $csvs ) {

if ($object -like '*2003*') { out-file  -inputobject $object  -append $name}
elseif ($object -like '*2000*') { out-file  -inputobject $object  -append $name}
elseif ($objects -clike '*NT*') { out-file  -inputobject $object  -append $name}
elseif ($object -clike '*XP*') { out-file  -inputobject $object  -append $name}
write-output $outfile
}
$a = "<style>"
$a = $a + "BODY{background-color:white;}"
$a = $a + "TABLE{border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse;}"
$a = $a + "TH{border-width: 1px;padding: 2px;border-style: solid;border-color: black;background-color:thistle}"
$a = $a + "TD{border-width: 1px;padding: 2px;border-style: solid;border-color: black;background-color:white}"
$a = $a + "TD:Nth-Child(Odd) {Background-Color: #dddddd;}"
$a = $a + "</style>"


import-csv $name | ConvertTo-HTML -head $a| Out-File C:\Users\myuser\Downloads\scripts\Test.htm

I think you have a bug in there, you have 2 variables named object that you are using for different purposes: 我认为您那里有一个错误,您有两个用于不同目的的名为object的变量:

$object = $csvs.os

ForEach ( $object in $csvs ) # object above getting overwritten

I would just get rid of the first object assignment and write the loop like this: 我将摆脱第一个对象分配,并编写如下循环:

ForEach ( $object in $csvs ) {

if ($object.os -like '*2003*') { out-file  -inputobject $object  -append $name}
elseif ($object.os -like '*2000*') { out-file  -inputobject $object  -append $name}
elseif ($object.os -clike '*NT*') { out-file  -inputobject $object  -append $name}
elseif ($object.os -clike '*XP*') { out-file  -inputobject $object  -append $name}
}

Though you can avoid the repetition like this: 尽管可以避免这样的重复:

$csvs | where-object{ $_.os -like '*2003*' -or $_.os -like '*2000*' -or $_.os -clike '*NT*' -or $_.os -clike '*XP*'} |  out-file -append $name

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM