将变量输出中的多个值循环到csv Powershell

将变量输出中的多个值循环到csv Powershell,powershell,Powershell,我创建了一个脚本,其中弹出一个对话框,您输入的值进入foreach。我不知道如何获取在运行脚本中创建的单个值并对其进行处理。虽然我知道这不是正确的方法,但它还是奏效了。。现在我创建了一个循环来再次提示,目标是将每个值附加到csv中。我的问题是,在写入csv之前,原始变量值会被输入提示符的下一个值覆盖。如何将每个条目构建到循环对话框中并创建csv 您可以在powershell脚本中看到,我从输入到对话框的值创建$x,然后将脚本循环到一个函数中,该函数重复对话框提示。当它这样做时,它将覆盖$x的第一

我创建了一个脚本,其中弹出一个对话框,您输入的值进入foreach。我不知道如何获取在运行脚本中创建的单个值并对其进行处理。虽然我知道这不是正确的方法,但它还是奏效了。。现在我创建了一个循环来再次提示,目标是将每个值附加到csv中。我的问题是,在写入csv之前,原始变量值会被输入提示符的下一个值覆盖。如何将每个条目构建到循环对话框中并创建csv

您可以在powershell脚本中看到,我从输入到对话框的值创建$x,然后将脚本循环到一个函数中,该函数重复对话框提示。当它这样做时,它将覆盖$x的第一个值。在将all写入csv之前,我试图找出如何构建多个值

此脚本用于让用户输入组,对照Active Directory进行检查,然后生成CSV

…更新##我能够解决它。我正在删除原始的测试代码,并将最终的产品放入其中。下面的脚本创建了一个表单,该表单要求在Active Directory中创建一个对象。它检查active directory,然后输出到电子表格,并再次询问,直到取消。重复构建变量

function ProcessOut ($x , $group) {
            $result = @()

            Foreach ($Line in $x){
                            $GroupName = "domain.local\" + $group
                            $OutList = New-Object System.Object
                            $OutList | Add-Member -type NoteProperty -Name "DisplayPath_GroupName" -value $GroupName
                            $OutList | Add-Member -type NoteProperty -Name "RuleName" -value "AutomaticApproval"
                            $OutList | Add-Member -type NoteProperty -Name "RuleClauses" -value '
                            $result+= $OutList
            }

            #Output to csv
            $outputfilepath = 'c:\users\technician\desktop\'
            $outputfilename = $outputfilepath + 'FinalFile.csv'
            $result | export-csv $outputfilename  -Append -encoding unicode -NoTypeInformation
}

function PromptInput {
            add-Type -AssemblyName System.Windows.Forms
            Add-Type -AssemblyName System.Drawing

            $form = New-Object System.Windows.Forms.Form
            $form.Text = 'Group Auto-Approval Setup'
            $form.Size = New-Object System.Drawing.Size(500,230)
            $form.StartPosition = 'CenterScreen'

            $OKButton = New-Object System.Windows.Forms.Button
            $OKButton.Location = New-Object System.Drawing.Point(170,100)
            $OKButton.Size = New-Object System.Drawing.Size(75,23)
            $OKButton.Text = 'OK'
            $OKButton.DialogResult = [System.Windows.Forms.DialogResult]::OK
            $form.AcceptButton = $OKButton
            $form.Controls.Add($OKButton)

            $CancelButton = New-Object System.Windows.Forms.Button
            $CancelButton.Location = New-Object System.Drawing.Point(260,100)
            $CancelButton.Size = New-Object System.Drawing.Size(75,23)
            $CancelButton.Text = 'Cancel'
            $CancelButton.DialogResult = [System.Windows.Forms.DialogResult]::Cancel

            $form.CancelButton = $CancelButton    
            $form.Controls.Add($CancelButton)      

            $label = New-Object System.Windows.Forms.Label
            $label.Location = New-Object System.Drawing.Point(200,40)
            $label.Size = New-Object System.Drawing.Size(280,20)
            $label.Text = "Enter a group name:"
            $form.Controls.Add($label)

            $textBox = New-Object System.Windows.Forms.TextBox
            $textBox.Location = New-Object System.Drawing.Point(100,65)
            $textBox.Size = New-Object System.Drawing.Size(300,120)
            $form.Controls.Add($textBox)

            $form.Topmost = $true

            $form.Add_Shown({$textBox.Select()})
            $result = $form.ShowDialog()

            if ($result -eq 'Cancel'){
                            Exit
            }

            if ($result -eq [System.Windows.Forms.DialogResult]::OK){
                            $x = $textBox.Text
            }

            return $x
}

$continue = $true
while($continue){
            $input = PromptInput
            Add-Type -AssemblyName microsoft.visualbasic

            $searcher = [ADSISearcher]"(SAMAccountName=$input)"
            $result = $searcher.FindOne()

            if($result){
                            ProcessOut $result $input
                            $additional = [System.Windows.Forms.MessageBox]::Show("Would you like to enter another group?" , "Status" , 4)
                            if ($additional -eq "NO"){
                                            Exit
                            }
            } else{
                            [System.Windows.Forms.MessageBox]::Show("Group name not found - Please enter a valid group name")
            }
} 

使用ArrayList或数组来构建内容。在全局级别创建它,以便在代码中可以访问它。例如:

# Create an empty array outside of the loop
$myArray = @()

# Get all running processes
$processes = Get-Process

# for each process in the list of processes we are going to do something
foreach($process in $processes)
{
    # Create a Pipe separated string
    $myString = $process.Name + "|" + $process.Id

    # Add the process name to my array.
    $myArray += $myString
}

# Export the array to a CSV file
$myArray | Export-Csv -Path c:\myfile.csv
或者,如果你不喜欢数组(我不喜欢有很多原因…),试试列表

将第2行替换为:

$myArray = New-Object System.Collections.ArrayList
将第14行替换为

$myArray.Add($myString) > $null
请注意,我正在将输出管道化为null。这是为了阻止它回响,而这可能/可能对你没有用处


希望这有帮助。

$result=@()
放在函数定义之前。如果将$x作为组名字符串的数组,也会更加糟糕。代码可以是
getgrouplist-groupxxx,yyy,zzz
。函数前面的数组有意义,但它在脚本中不起作用。我认为get-grouplist不起作用,因为我在提示符中一个接一个地添加它们。这似乎不起作用。但我正试着用它来接近它。这很棘手。我也偶尔遇到这种情况。你运行的是什么版本的PoSh?我可以通过使用“while”并将其传递给functionl来解决它