如何将 HashTable 作为参数传递给函数并将数据导出到 csv 中?

问题描述

我已经构建了用于捕获数据的 PowerShell 模块函数,例如:

  • 开始日期
  • 开始时间
  • 服务器名称
  • 用户(执行脚本的人
  • 技术
  • 脚本名称
  • 脚本路径
  • 执行时间(脚本从开始到结束所用的秒数)
  • 错误信息(如果脚本中有任何错误信息)

以上几点提供了每个脚本的数据,我已经启用了模块并在我的每个脚本中调用函数。但是,有些脚本提供构建数据,有些脚本不提供。例如,有一个脚本可以为最终用户启用 MS 项目MS Visio 许可,它为我提供了关于 # of Project 的数据已分配许可证和 Visio 数量 已分配许可证。

我正在尝试找到如何从我的一些脚本中捕获构建数据的方法。我想用 headers 捕获数据,并将它们与我的模块脚本中的 HashTable 合并。总之,我想将数据导出到 csv 文件

我想了解我错过了什么?我使用了 param 方法,但无法输出任何数据。

这是我的 PowerShell 模块代码

function Get-Info(){

$date = (Get-Date -f  "MM_dd_yyyy_hh_mm")

#TimeStamp
$reportDate = (Get-Date -f  "MM/dd/yyyy")

$reportTime = (Get-Date -f  "hh:mm:ss tt")

#Server
$serverName = $env:COmpuTERNAME 

#user
$user = $env:UserName

#script name
$scriptName = split-path $MyInvocation.PSCommandpath -Leaf

#script path
$scriptPath = split-path $MyInvocation.PSCommandpath 

#service account
$serviceAccount = $tenantAdmin

#measure execution time (start to finish)
[timespan]$executionTime = Measure-Command {0..10000 | ForEach-Object {$i++} }

$runTime = $executionTime.Milliseconds

#Technology Type
$technology = Split-Path (Split-Path $scriptPath -Parent) -Leaf

#error handling

If($errorMessage -eq $null){

    $errorMessage = "None"
}

if ($errorMessage -ne $null){

    $errorMessage = $ErrorMessage

}

$fileName = $scriptName.Trim(".ps1")

$Hashtable = @()
$Hashtable += New-Object psobject  -Property @{

"Script Name" = $scriptName;
"Execution Date" = $reportDate;
"Execution Time" = $reportTime;
"Service Account" = $serviceAccount;
"User" = $user;
"Server Name" = $serverName;
"Script Path" = $scriptPath;
"RunTimeMS" = $runTime;
"Error Message" = $errorMessage;
"Technology" = $technology;

}

if($myHashTable -eq $null){

$Hashtable | Export-Csv "D:\MyFileName\$fileName$date.csv" -NoTypeinformation


}

if($myHashTable -ne $null){

$finalTable = $Hashtable + $myHashTable

$finalTable | Export-Csv "D:\MyFileName\$fileName$date.csv" -NoTypeinformation


}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)