简体   繁体   English

当返回的值是$ null或类似值时,将自定义值输出到PS表中

[英]Output custom value into PS table when value returned is $null or similar

I am generating a report out of AD of users with contract expiry dates listed. 我正在根据列出的合同到期日期的用户AD生成报告。 So far I have been able to come up with this, which outputs to a CSV file with custom headings and the date shortened. 到目前为止,我已经能够提出这个建议,将其输出到带有自定义标题并缩短日期的CSV文件。

Get-QADUser -SizeLimit 0 -IncludedProperties AccountExpires,Domain,Name -DontUseDefaultIncludedProperties | Where { $_.Title -ne "Resource" } | Select @{Label="Domain";Expression={(($_.Domain).Name)}},@{Label="Employee Name";Expression={($_.Name)}},@{Label="Contract Expiry Date";Expression={(($_.AccountExpires).ToShortDateString())}},title,department,Office | Export-Csv C:\ExportForHR.csv -NoTypeInformation

What I'm wanting to do is put a custom value in column "Contract Expiry Date", if they have no value. 我想做的是将自定义值(如果没有值)放在“合同到期日期”列中。

Ie if I don't have a contract, the default value is null, therefore nothing shows in the spreadsheet. 即,如果我没有合同,则默认值为null,因此电子表格中未显示任何内容。 But what if I wanted to put "No contract expiry" or "Full time expiry" in the field instead? 但是,如果我想在字段中输入“无合同到期”或“全职到期”怎么办?

Couldn't find anything that would be suitable using the command above. 使用上述命令找不到适合的内容。 I could probably write a full powershell script to output to a CSV file by not using things like Select and export-csv, but I'd rather not, unless I really have to. 通过不使用Select和export-csv之类的方法,我可能可以编写一个完整的powershell脚本以将其输出到CSV文件,但是我宁愿不这样做,除非我真的必须这样做。

You're about 95% of the way there. 您大约到达那里的方式的95%。 The script block in the Expression section of Select-Object is a full script block, so you can perform the test right in there: Select-Object的Expression部分中的脚本块是一个完整的脚本块,因此您可以在其中执行测试:

Get-QADUser -SizeLimit 0 -IncludedProperties AccountExpires,Domain,Name -DontUseDefaultIncludedProperties | 
Where { $_.Title -ne "Resource" } | 
Select-Object 
  @{Label="Domain";Expression={(($_.Domain).Name)}},
  @{Label="Employee Name";Expression={($_.Name)}},
  @{Label="Contract Expiry Date";Expression={
    if ($_.AccountExpires -ne $null) { 
      $_.AccountExpires).ToShortDateString()
    } else { 
      $CustomValue 
    }
  },
  title,
  department,
  Office 
| Export-Csv C:\ExportForHR.csv -NoTypeInformation

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

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