How to update an arraylist inside a foreach -parallel
Loop with foreach removing from arraylist.
How to iterate through an array of objects in Powershell - Stack Overflow
Update scripts to use arraylist instead of array in foreach loops ?
I can't update the arraylist. When I try to add an object to the array list, i get the following error: "You cannnot call a method on a null-valued expression."
Can anyone offer advice?
My code:
$OpenFiles = Get-SmbOpenFile
$FSShares = Get-SMBShare | where {$_.ScopeName -ne '*'}
$Alldata = [System.Collections.ArrayList]@()
$OPenFiles | foreach-object -parallel {
$UserAccount = $psitem.ClientUserName
$FullPath = $psitem.Path
$RPath = $psitem.ShareRelativePath
$SourceIP = $psitem.ClientComputerName
$RootShare = $fullpath.Replace("\$rpath","")
$ShareInfo = $FSShares | where {$_.Path -like $RootShare}
$Sharename = $ShareInfo.Name
$FSRole = $ShareInfo.ScopeName
$obj = new-object psobject
$obj | Add-Member -MemberType NoteProperty -Name 'Role' -Value $FSRole
$obj | Add-Member -MemberType NoteProperty -Name 'ShareName' -Value $ShareName
$obj | Add-Member -MemberType NoteProperty -Name 'PathToShare' -Value $RootShare
$obj | Add-Member -MemberType NoteProperty -Name 'Account' -Value $UserAccount
#$obj | Add-Member -MemberType NoteProperty -Name 'ShareName' -Value $ShareName
$Alldata.Add($Obj) | out-null
}Hello :)
Was wondering if any of you kind gentlemen/women could help me understand whats wrong with a small script im writing.
https://pastebin.com/rDNacxqb
Getting error: WARNING: ZLE0294: Exception calling "Find" with "2" argument(s): "Unspecified error"
Edit:
What i ended up running:
$Laptops = ((Get-ADComputer -Filter {Name -like "COMP*"}).name)
$Logfile = "C:\Temp\EXPORT\LocalAdminZLE $(get-date -f yyyy-MM-dd).csv"
while ($Laptops){
foreach ($laptop in $laptops){
if (Test-Connection -Computername $Laptop -BufferSize 16 -Count 1 -Quiet){
$C = Get-LocalGroup -Computername $laptop -Group Administrators
if($C){
$C | Export-Csv $Logfile -Append
$laptops = $laptops -ne $laptop
}
}
}
}Seems to be working so far.
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($obj in $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 ($c in $Controls) {
ForEach ($Field in $MandatoryFields) {
if ($c.Name -eq $Field) {
If ([string]::IsNullOrWhitespace($c.text)) {
$Validation = "failed"
$c.BackColor = "red"
}
}
}
}
I'm struggling to wrap my head around this one, but I understand that arraylists have less overhead than arrays do when creating them. I'd like to update some of my scripts to output results to arraylists instead of arrays.
Example of my current setup which outputs an array object
$results = foreach($computer in $list){
if($computer.name -like "*Finance*"){
[pscustomobject]@{
Department = "Finance"
Computer = $computer.name
}
}
elseif($computer.name -like "*Marketing*"){
[pscustomobject]@{
Department = "Marketing"
Computer = $computer.name
}
}
}
$results
Department Computer
______ _____
Finance Finance01comp
Marketing Marketing01compFrom what I gather, this will rewrite the entire array every time I loop through $list. Is there a way to just have each loop append an arraylist with consistent property names but different property values? (assume my $list is much larger than 2 computers lol)
You cannot change a collection like ArrayList while iterating its items like that.
If all you want to do is empty the list, use $global:accountArray.Clear()
If you want to use a loop, do that by using the element index and go from bottom to top:
Function Remove-Numbers {
# use a loop to remove the items. go from last to first
for ($i = $global:accountArray.Count -1; $i -ge 0; $i--) {
$global:accountArray.RemoveAt($i)
}
}
Or use the Remove method slightly different by letting $n not being a member of the list itself, but merely the value it stores:
Function Remove-Numbers {
# use a loop to remove the items. Iterate using the value of the item, not the item itself.
foreach ($n in $global:accountArray.ToArray()) {
$global:accountArray.Remove($n)
}
}
P.S. the comment character in PowerShell is #, not //
You can't modify a list you are iterating through. The collection is modified so it's not the same one it started with. You can call ToArray() on the list to fix this.
Function Remove-Numbers
{
foreach ($n in $global:accountArray.ToArray()) {
$global:accountArray.Remove($n)
}
}