首页 > 解决方案 > 将变量输出中的多个值循环到 csv Powershell

问题描述

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

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

该脚本是让用户输入一个组,对照 Active Directory 检查它,然后生成一个 CSV。

...更新##我能够解决它。我正在删除原始测试代码并放入最终产品。以下脚本创建了一个表单,该表单在 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")
            }
} 

标签: powershell

解决方案


使用 ArrayList 或 Array 进行构建。在全局级别创建它,以便在代码中可以访问它。例子:

# 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

或者如果你不喜欢数组(我不喜欢有很多原因......)尝试一个列表......

将第二行替换为:

$myArray = New-Object System.Collections.ArrayList

将第 14 行替换为

$myArray.Add($myString) > $null

请注意,我正在将输出传递给 null。这是为了阻止它回响,这可能对您有用/可能没用。

希望这可以帮助。


推荐阅读