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

时间:2018-06-18 16:12:24

标签: 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")
            }
} 

1 个答案:

答案 0 :(得分:0)

使用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。这是为了阻止它回显,这可能/可能对你没用。

希望这有帮助。