Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PowerShell系统。对象[]数组导出CSV_Powershell_Csv - Fatal编程技术网

PowerShell系统。对象[]数组导出CSV

PowerShell系统。对象[]数组导出CSV,powershell,csv,Powershell,Csv,上面给出了以下输出: $path = "\\path\to\folder" $ts = (Get-Date -format yyyyMMdd_HHmmss) Invoke-WebRequest -Uri "https://fsrm.experiant.ca/api/v1/combined" -UseBasicParsing -OutFile ($path + $ts + ".json") $json = Get-Content -Raw ($path + $ts + ".json") $x =

上面给出了以下输出:

$path = "\\path\to\folder"
$ts = (Get-Date -format yyyyMMdd_HHmmss)
Invoke-WebRequest -Uri "https://fsrm.experiant.ca/api/v1/combined" -UseBasicParsing -OutFile ($path + $ts + ".json")
$json = Get-Content -Raw ($path + $ts + ".json")
$x = $json | ConvertFrom-Json
$x | Select filters | Export-Csv -NoTypeInformation -Path ($path + $ts + ".csv")
将脚本的最后一行更改为:

filters
System.Object[]
下面是每件物品的长度计数:

$x | Select-Object -ExpandProperty filters | Export-Csv -NoTypeInformation -Path ($path + $ts + ".csv")
我不想要长度,我想要实际值,正如你从.json链接中看到的,它调用的web请求应该是一个文件类型列表;导入FSRM以对抗勒索软件

我在“选择对象”页面中看不到有关选择可以返回的内容的任何内容,那么我缺少了什么


尝试这样修改

Length
6
6
6
6
7
...

Invoke-RestMethod
包括从JSON转换响应,而
Invoke-WebRequest
则不包括,因此在这里使用它是一个有用的cmdlet

.filters
在该站点的JSON输出中是一个字符串列表。您可以将它们放入一个文件中,每行一个,包括:

  $path = "c:\temp\"
  $ts = (Get-Date -format yyyyMMdd_HHmmss)
  Invoke-WebRequest -Uri "https://fsrm.experiant.ca/api/v1/combined" -UseBasicParsing -OutFile ($path + $ts + ".json")
  $json = Get-Content -Raw ($path + $ts + ".json")
  $x = $json | ConvertFrom-Json
  $x |%{ $_.filters}  | select @{ N="Value";E={$_.ToString()}} | Export-Csv -Delimiter ";" -NoTypeInformation -Path ($path + $ts + ".csv")
如果确实需要列标题行:

$filters = (Invoke-RestMethod -Uri https://fsrm.experiant.ca/api/v1/combined).filters
$filters | Set-Content "d:\path\$(get-date -Format u).csv"
如果它确实需要通过CSV,它将引用每一行并有一个标题行,那么:

$Path = "d:\path\$(get-date -Format u).csv"

$filters = (Invoke-RestMethod -Uri https://fsrm.experiant.ca/api/v1/combined).filters
'Filter' | Set-Content $Path
$filters | Add-Content $Path
无需
|%{$\u.Filters}
,因为只有一个对象从JSON转换而来,并且Filters是它的一个属性,所以请访问它一次:

$path = "d:\temp"
$ts = (Get-Date -format yyyyMMdd_HHmmss)

$json = Invoke-RestMethod -Uri "https://fsrm.experiant.ca/api/v1/combined"
$json.filters | select @{N="Value";E={$_}} | Export-Csv -NoTypeInformation -Path "$path\$ts.csv"
而且不需要
E={$\u0.ToString()}
,因为过滤器值已经是字符串了

导入CSV和导出CSV的工作方式是:

  • 行是一个对象
  • 列映射到对象的属性
  • CSV映射到一组对象
Select
允许您从通过管道输入的某些对象开始,然后仅使用您选择的某些属性沿管道发送它们

通过计算特性,可以将新特性动态添加到通过管道传递的对象中

在这种情况下:

{
    filters: [
       ]
}
通过的是线

$json.filters | 
他拿了一根绳子

$json.filters | select @{Name="Value";Expression={$_}}
并将其转换为具有一个属性的PSCustomObject:

"*.vbs"
现在映射到CSV行,其中有一列(“值”)和列内容
*.vbs

这条管道变得

@{Value="*.vbs"}

Export CSV
将它们处理成CSV

Select Object@{Name=“Filters”;Expression={$.Filters-join';}
@MathiasR.Jessen在记事本中查看时,文本中有不需要的引号,例如:
过滤器“\n”*.ext\n*.ext2\n*.ext3”
(我用“`n”回复了
-join
字符)@adampski如果它只是一个文件扩展名列表而没有其他列,那么CSV格式意味着什么?“不需要的引号”是什么意思?引号是CSV格式的一部分,而不是数据的一部分,这就像说“我希望我的图像是JPEG…但现在它充满了不需要的JPEG压缩信息”。如果你只想每行一个
$x.filters | set content which.csv
@TessellatingHeckler如果你密切注意我评论中的引号,你就会明白我所说的不想要的是什么意思。这给了我想要的输出,谢谢。如果我要进一步阅读以了解有关
@{N=“Value”E={$\uu.ToString()}}
的所有信息,我需要搜索哪些与PowerShell相关的关键字?“计算属性”
@{Value="*.vbs"}
@{Value="*.exe"}
@{Value="*.js"}