Powershell脚本读取CSV文件两次
我目前尝试读取CSV文件,将给定数据调整为powershell命令,然后将其写入其他文件。 问题: forEach循环,它通过CSV文件循环两次。因此,outputfile包含两次输出Powershell脚本读取CSV文件两次,powershell,loops,foreach,Powershell,Loops,Foreach,我目前尝试读取CSV文件,将给定数据调整为powershell命令,然后将其写入其他文件。 问题: forEach循环,它通过CSV文件循环两次。因此,outputfile包含两次输出 function ReadCSV($file_path, $delimeter, $UserInputUser, $UserInputPw){ $data=Import-CSV -Path $file_path -Delimiter $delimeter $destinationPath=Find-Folders
function ReadCSV($file_path, $delimeter, $UserInputUser, $UserInputPw){
$data=Import-CSV -Path $file_path -Delimiter $delimeter
$destinationPath=Find-Folders
ForEach($dataset in $data) {
$username=$dataset.$UserInputUser
$pass=$dataset.$userInputPW
if(($username -gt 0) -AND ($pass -gt 0)){
WriteOutputFile -username $username -pass $pass -destinationPath $destinationPath
}
}
#Run ReadOutputFile function
ReadOutputFile -destinationPath $destinationPath }
我认为forEach循环似乎是问题所在。我目前不知道如何解决这个问题
这是writeOutput文件
function WriteOutputFile($username, $pass, $destinationPath) {
$createUserComand="New-LocalUser -Name ""$username"" -Password (ConvertTo-SecureString ""$pass"" -AsPlainText -Force) -Confirm"
#write-output $createUserComand | add-content -path $destinationPath\output.ps1
$createUserComand | out-file -filepath $destinationPath\output.ps1 -Append
}
我假设
WriteOutputFile
是一个自定义函数。它在干什么?您的foreach
循环在每次迭代时都尝试调用它。您还可以将foreach
和if
循环组合替换为where object
,如$outputData=$data | where object{($.$UserInputUser-gt 0)-和{$.$UserInputPw-gt 0}
。但是,我不太确定您的writeOutput file
函数在做什么。请不要在注释中粘贴代码,因为这是不可读的。取而代之的是你的问题并将代码粘贴到其中我在问题中添加了writeOutput函数