Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/12.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
String 在PowerShell中将.csv格式(导入csv)转换为字符串_String_Powershell_Csv - Fatal编程技术网

String 在PowerShell中将.csv格式(导入csv)转换为字符串

String 在PowerShell中将.csv格式(导入csv)转换为字符串,string,powershell,csv,String,Powershell,Csv,我正在尝试编写一个PowerShell脚本 读取CSV文件 循环CSV文件中的每一行。 在每个循环中,我希望以System.String类型的格式将CSV头和行值传递给另一个脚本。 特别是,我正在努力转换为字符串类型的格式-最好的方法是什么 脚本1: $csvPath='C:\Temp\Scripts\trial\u csv.csv' 拿到头球 $header=获取内容$csvPath-TotalCount 1 $IntermediateOutput='C:\Temp\Scripts\outpu

我正在尝试编写一个PowerShell脚本

读取CSV文件 循环CSV文件中的每一行。 在每个循环中,我希望以System.String类型的格式将CSV头和行值传递给另一个脚本。 特别是,我正在努力转换为字符串类型的格式-最好的方法是什么

脚本1:

$csvPath='C:\Temp\Scripts\trial\u csv.csv' 拿到头球 $header=获取内容$csvPath-TotalCount 1 $IntermediateOutput='C:\Temp\Scripts\output' $scriptPath='C:\Temp\Scripts\Temp.ps1' 获取数据 获取内容$csvPath| 选择-跳过1| ForEach对象{ $argumentList=@ $argumentList+=-标题,$Header $argumentList+=-行,$row $argumentList+=-IntermediateOutput,$IntermediateOutput $argumentList+=-索引,$i 调用表达式&`$scriptPath`$argumentList } 脚本2:

从脚本1接收输入:

Param [ParameterMandatory=$True] [String]$header,CSV文件的头 [ParameterMandatory=$True] [String]$row,文件的行,如1,0,2,。。 [ParameterMandatory=$True] [字符串]$IntermediateOutput,中间目录 [ParameterMandatory=$True] [字符串]$index索引 以后做点什么 $a=$header $b=$row $c=$IntermediateOutput $d=$index
您可以直接从新创建的$csv对象获取标题。在下面的示例中,$header成为字符串数组:

$header=$csv[0].psobject.Properties| 其中{$\.MemberType-eq NoteProperty}.Name 另一种方法是使用Get-Content cmdlet读取第一行并将其拆分为,字符:

$header=获取内容$csvPath-TotalCount 1-split,
在代码中,$header等于null,因为$header=Write Host$rows[1]未分配变量。它只执行一个操作。

如果CSV文件中的数据没有被引用,您应该能够直接从.CSV文件中使用它,而无需执行导入CSV:

$csvPath = 'C:\Temp\Scripts\trial_csv.csv'
$IntermediateOutput = "'C:\Temp\Scripts\output'"
$Index = 1

# Get the header
$header = Get-Content $csvPath -TotalCount 1

# Get the data
Get-Content $csvPath |
Select -Skip 1 |
    ForEach-Object {
        $argumentlist = "-Header '{0}' -row '{1}' -IntermediateOutput {2} -Index {3}" -f $header,$_,$IntermediateOutput,$Index

        # Execute script with argument list

        $Index++
    }

最好问一些具体的问题:展示你的代码,解释它的功能,以及你无法理解的地方。在它的当前形式中,它是to vague:object.ToString转换为System.String,但您可能已经知道了。我收到一个错误,不知道为什么:缺少参数“header”的参数。请指定“System.String”类型的参数,然后重试。