Try adding your objects to an array like below
$objects = [System.Collections.ArrayList]@()
$myObject = [PSCustomObject]@{
Name = 'Kevin'
Language = 'PowerShell'
State = 'Texas'
}
$objects.add($myObject)
$myObject1= [PSCustomObject]@{
Name = 'Kevin'
Language = 'PowerShell'
State = 'Texas'
}
$objects.add($myObject1)
foreach(
objects){
$obj.firstname
}
Answer from TheGameiswar on Stack OverflowTry adding your objects to an array like below
$objects = [System.Collections.ArrayList]@()
$myObject = [PSCustomObject]@{
Name = 'Kevin'
Language = 'PowerShell'
State = 'Texas'
}
$objects.add($myObject)
$myObject1= [PSCustomObject]@{
Name = 'Kevin'
Language = 'PowerShell'
State = 'Texas'
}
$objects.add($myObject1)
foreach(
objects){
$obj.firstname
}
I'm assuming your using System.Windows.Forms to create your form. If that's the case when adding controls you should assign a name to the control. Then you can loop through your controls and check if the control name matches your list of mandatory controls and then execute your check.
$MandatoryFields = @(
'txtUsername',
'txtFirst',
'txtLast',
'txtEmail',
'ComboLicense'
)
$Controls = MyForm.controls
ForEach (
Controls) {
ForEach ($Field in $MandatoryFields) {
if ($c.Name -eq $Field) {
If ([string]::IsNullOrWhitespace($c.text)) {
$Validation = "failed"
$c.BackColor = "red"
}
}
}
}
Videos
Hi all,
I've googled it for a bit but I can't find exactly what I am looking for.
I know a bit about arrays and how to address certain elements but what I need to do is to build something that will go loop through continuously.
So if my array has a, b and c in it I want the loop to start with a, then b, then c and then back to a and just keep going but it is running from within an util loop.
I can't figure out how to incrementally go through an array and make it loop like this.
Any help would be great.
I use Arraylists as I like the way they are exported (csv / html)
$serverlist = 'srv010','srv012'
$arraylist = New-Object System.Collections.Arraylist
foreach (
serverlist)
{
$arraylist.add($(Get-Certificate -Computername $i -StoreName My -StoreLocation LocalMachine -DaysUntilExpired 3000 -HideExpired | Select Subject, DaysUntilExpired, NotAfter)) | Out-null
#Out-null is used to remove the output of the add() function of the arraylist object. Otherwise it shows the last item index every time something is added
}
You can then check what’s inside the $arraylist and export it as html / csv / clixml
gents,
I like to run a foreach loop like this.
$serverlist = 'srv010','srv012'
foreach (
serverlist)
{
$CertList = Get-Certificate -Computername $i -StoreName My -StoreLocation LocalMachine -DaysUntilExpired 3000 -HideExpired | Select Subject, DaysUntilExpired, NotAfter
}
I like to mail the results so I thought maybe its good to put all output to an array and then call a mailsend procedure.
But I do not know how to add the results to the array instead of overwriting on each loop. Am I thinking too complicated?
Hi
I’m testing the Foreach loop with arrays. I’m getting correct results with this simple first basic array.
$add_server = @(‘Server one’, ‘Server two’, ‘Server three’)
foreach ($i in $add_server) {
Add-Content -Path “C:\Users\sunny\Documents\SampleFile3.txt” -Value “$i”
}
But for the next 2 I keep getting “Sever three” in the SampleFile3.txt
1)$add_server | ForEach-Object (Add-Content -Path “C:\Users\sunny\Documents\SampleFile2.txt” -Value “$i”)
2)$add_server2.ForEach({
Add-Content -Path “C:\Users\sunny\Documents\SampleFile5.txt” -Value “$i”
})
I think I need to intialize the array to index 0. Not sure how to do that.
Thanks
Sunny
Hi there! Welcome to the forum.
Please use the ‘insert code’ () option when inserting code to your post. It upsets the elders, when you don’t.
Did you notice how you changed the content of your ForEach-Object statement between your first and second iteration?
foreach ($i in $add_server) {
Add-Content -Path "C:\Users\sunny\Documents\SampleFile3.txt" -Value "$i"
}
1)$add_server | ForEach-Object (Add-Content -Path "C:\Users\sunny\Documents\SampleFile2.txt" -Value "$i")