[英]Loop Multiple Values In Variable Output To csv Powershell
I created a script where a dialogue box pops up and the value you enter goes into foreach. 我创建了一个脚本,其中会弹出一个对话框,并将您输入的值输入到foreach中。 I didn't know how to take a single value created in a running script and process it.
我不知道如何获取正在运行的脚本中创建的单个值并对其进行处理。 It worked, although I knew it wasn't the right way to do it.. Now I've created a loop to prompt again and the goal is to append each value into a csv.
尽管我知道这不是正确的方法,但它确实起作用。。现在,我创建了一个循环以再次提示,目标是将每个值附加到csv中。 My problem is now the original variable value is overwritten by the next value put into the prompt before writing out to a csv.
我的问题是,原来的变量值在写到csv之前被提示中的下一个值覆盖。 How can I build each entry into the looping dialogue box and create the csv?
如何在循环对话框中构建每个条目并创建csv?
You can see in the powershell script that I create $x from the value input to the dialogue box, then I cycle the script into a function that repeats the dialogue prompt. 您可以在powershell脚本中看到我是根据输入对话框的值创建$ x的,然后将脚本循环到一个重复对话框提示的函数中。 When it does that it overwrites the first value of $x.
这样做时,它将覆盖$ x的第一个值。 I'm trying to figure out how to build many values before writing the all to the csv.
我试图弄清楚如何在将全部内容写入csv之前建立许多值。
This script is to have a user enter a group, check it against Active Directory, and then generate a CSV. 该脚本是让用户输入一个组,对照Active Directory对其进行检查,然后生成CSV。
...Update## I was able to resolve it. ...更新##我能够解决它。 I'm removing the original test code and putting the final product.
我要删除原始测试代码,然后放入最终产品。 The following script creates a form which asks for an object in Active Directory.
以下脚本创建一个表单,该表单在Active Directory中要求一个对象。 It checks active directory then outputs to a spreadsheet, and asks again until canceled.
它检查活动目录,然后输出到电子表格,并再次询问直到被取消。 Building the variable repeatedly.
重复构建变量。
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")
}
}
Use an ArrayList or an Array to build things up. 使用ArrayList或Array进行构建。 Create this at the Global level so that it is accessible within the code.
在全局级别创建此代码,以便在代码内对其进行访问。 Example:
例:
# 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
Or if you don't like arrays (I don't for many reasons...) Try a List... 或者,如果您不喜欢数组(出于多种原因,我不...)请尝试使用列表...
Replace the 2nd line with: 将第二行替换为:
$myArray = New-Object System.Collections.ArrayList
Replacing the 14th line with 将第14行替换为
$myArray.Add($myString) > $null
Notice that I am piping the output to null. 注意,我将输出管道传递为空。 This is to stop it echoing back which may / maynot be useful to you.
这是为了阻止它回显,这可能/可能对您没有用。
Hope this helps. 希望这可以帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.