因此,我在一个列中选择数据时遇到了问题,该列中的数据比我需要的更多,并且只将这些数据输出到一个新的csv中。我有一个8列CSV,我只从其中选择2列,然后删除插件输出列中包含TCP的所有行。这件事很好,但我想不出下面的任务。
Import-csv c:\csv.csv | select "Plugin Output",Host |
Where-Object ($_."Plugin Output" -notmatch "tcp"|
export-csv -path C:\new.csv -NoTypeInformation继续下一个任务。取单元说明“远程操作系统: Microsoft Windows 7专业可信度: 99方法: MSRPC远程主机正在运行”插件输出“中的Microsoft Windows 7 Professional”,并仅从其中选择Windows 7、Unix或Cisco。
发布于 2015-04-22 13:58:40
我已经更新了下面的代码,向您展示了如何实现这一点的两个示例,一个是一行,另一个是使用不同方法的多行格式。
#Define any functions first
function CheckOS ([string]$CellContent) {
switch -Wildcard ($CellContent) {
"*windows 7*" {"Windows 7"}
"*unix*" {"Unix"}
"*cisco*" {"Cisco"}
#if id doesn't match anything, return the original
default {return $CellContent}
}
}
#Based on your example, this will be "Windows 7"
#$OS = CheckOS $Cell
#Method 1, in one line
Import-csv c:\csv.csv | select "Plugin Output",Host |
Where-Object ($_."Plugin Output" -notmatch "tcp") |
foreach-object {$_."Plugin Output" = (CheckOS $_."Plugin Output")} |
export-csv -path C:\new.csv -NoTypeInformation
<#
#Method 2, not in one line, not using a function or switch
$CSV = Import-csv c:\csv.csv | select "Plugin Output",Host |
Where-Object ($_."Plugin Output" -notmatch "tcp")
#iterate through each item in the CSV
foreach ($Cell in $CSV) {
if ($Cell."Plugin Output" -like"*windows 7*") {
$Cell = "Windows 7"
} elseif ($Cell."Plugin Output" -like "*unix*") {
$Cell = "Unix"
} elseif ($Cell."Plugin Output" -like "*cisco*") {
$Cell = "Cisco"
}
}
#export the csv when done
$CSV | Export-Csv -path C:\new.csv -NoTypeInformation
#>编辑:我最初犯了一些错误。首先,在方法1中,我把它设置为一个变量,这个变量是错误的,它是我大脑中遗留下来的,引发了一个不同的想法。在方法2中,我应该使用-like,而不是使用-contains 这是错误的。
https://stackoverflow.com/questions/29799588
复制相似问题