简体   繁体   English

替换PowerShell输出中的NULL值

[英]Replace NULL value in powershell output

I'm really struggling with what seems to be a simple thing. 我真的很挣钱似乎是一件简单的事情。 Any help is appreciated... 任何帮助表示赞赏......

tldr; tldr; i'm trying to find and replace blank or NULL values from powershell output to "No Data" 我正在尝试查找并将powershell输出中的空值或NULL值替换为“无数据”

I am using the following powershell script to obtain installed application information 我使用以下powershell脚本来获取已安装的应用程序信息

Get-ItemProperty HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\*

Below is sample output of the above script (filtered to my liking). 下面是上面脚本的示例输出(按我的喜好过滤)。 I am taking this data, exporting it to CSV, in order to import into another application for analysis at a later time. 我正在获取此数据,将其导出为CSV,以便稍后导入另一个应用程序进行分析。

Host             : Computer1
DisplayName      : AutoDWG DWG DXF Converter 2015
Version          : 
Publisher        : 
InstallDate      : 
arrival_datetime : 2015-11-03T09:42:18

Host             : Computer2
DisplayName      : Beyond Compare Version 3.1.11
Version          : 
Publisher        : Scooter Software
InstallDate      : 20150218
arrival_datetime : 2015-11-03T09:42:18

the CSV export puts the data in the correct format, but where items have no data for version, publisher, etc..., it represents it as ",," (see below) CSV导出将数据放入正确的格式,但是如果项目没有版本,发布者等数据,则表示为“,,”(见下文)

"Computer1","AutoDWG DWG DXF Converter 2015",,,,"2015-11-03T09:54:21"
"Computer2","Beyond Compare Version 3.1.11",,"Scooter Software","20150218","2015-11-03T09:54:21"

When importing the CSV, the blank values are re-interpreted as NULL, which throws the program for a loop, as it is expecting a string. 导入CSV时,空值会被重新解释为NULL,这会抛出程序循环,因为它需要一个字符串。 I am attempting to change those to the string "No Data" but am having lots of trouble... 我试图将这些更改为字符串“No Data”但是我遇到了很多麻烦......

What would be the best way to handle this? 处理这个问题的最佳方法是什么?

You could inspect the property values as they are piped from the Import-Csv cmdlet, and change them. 您可以检查从Import-Csv cmdlet传送的属性值,并更改它们。 Example: 例:

Get-ItemProperty HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\* | 
ForEach-Object { 
foreach ($p in $_.PSObject.Properties) 
{ 
 if ($p.Value -eq [string]::Empty)
 {
  $p.Value = 'No Data'
 }
} 
 Write-Output $_
}

Using Select-Object would be your best bet. 使用Select-Object将是您最好的选择。 Just pipe the data to Select-Object , but customize each desired property as follows: 只需将数据传递给Select-Object ,但按如下方式自定义每个所需的属性:

Get-ItemProperty HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\* | 
  Select-Object Host, DisplayName, @{
    Label = "Version"
    Expression = { if ($_.Version) { $_.Version } else { "No Data" } }
  }, Other Properties

Building off of this answer , you could generate the calculated properties like this: 基于此答案 ,您可以生成如下计算的属性:

$SelectProperties = "Host","DisplayName"
$NoDataFields = "Version","Publisher","InstallDate"

$SelectProperties += $NoDataFields|ForEach-Object {
    @{
      Label = $_
      Expression = [scriptblock]::Create("if(`$_.""$_""){ `$_.""$_"" } else { ""No Data"" }")
    }
}

$Data = Import-Csv -Path "C:\SomePath.csv" | 
  Select-Object $SelectProperties

Do your columns have headers? 你的专栏是否有标题? This was how I did it, when I exported a CSV of groups and their owners. 当我导出一组CSV及其所有者时,我就​​是这样做的。 For any group that didn't have an owner in the "ManagedBy" column, it fills the field with "No Owner" instead of a blank space: 对于在“ManagedBy”列中没有所有者的任何组,它将使用“No Owner”而不是空格填充该字段:

$CSVData = Import-Csv $TempCSV -Header "samAccountName", "ManagedBy"  
$CSVData | %{
if($_.ManagedBy -eq "") {$_.ManagedBy="No Owner"}
} 
$CSVData | Export-Csv $Filename -NoTypeInformation

You could just change the "ManagedBy" to your header name and the "$_.ManagedBy" to what you need, then obviously "No Owner" would be "No Data". 您可以将“ManagedBy”更改为您的标题名称,将“$ _.ManagedBy”更改为您需要的内容,然后显然“No Owner”将是“No Data”。

Another option that might work: 可能有用的另一种选择:

$TargetFile = "C:\temp\replaceNull.csv"
$File = Get-Content $TargetFile
$Output = @()
foreach ($Line in $File) {
    $Output += $Line -replace ",,",",No Data,"
}
$Output | Set-Content $TargetFile

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

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