簡體   English   中英

使用Powershell導入CSV時嘗試匹配字符串

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

我正在嘗試創建一個報告,以CSV文件形式顯示較舊的操作系統。 該程序將輸出顯示得很漂亮,但實際上不解析任何內容。 我正在嘗試分離Windows 2000、2003,XP和Windows NT計算機。 示例數據如下:

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.

我到目前為止所擁有的程序在這里:

$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

我認為您那里有一個錯誤,您有兩個用於不同目的的名為object的變量:

$object = $csvs.os

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

我將擺脫第一個對象分配,並編寫如下循環:

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}
}

盡管可以避免這樣的重復:

$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