Remove items from array


removing range of items index # from array has me stumped.  have tried using .net with a $a=new-object system.collection.arraylist .  problem when use get-content or copy in array converts $a array normal powershell array , loses .remove , .removerange methods.  being able $a.removerange($index1,$index2) solve issue of removing items array.  cannot figure out how remove items array using a index range natively in powershell. 

example of have:

$msgcontent = new-object system.collections.arraylist
$msgcontent = get-content "file.txt"
$doclines= get-content "file.txt" |select-string "\[document\]"  #find sections in document check
$i = 0
foreach ($docline in $doclines){
 $i++
 $entrystart = ($docline.linenumber)-1
 $entryend = ($doclines[$i].linenumber)-2 #get line count of end of [document]section
 if ($doclines[$i].linenumber -eq $null){$docentry2 = $msgcontent.length; write-host "end of file."}
 $docentry = $msgcontent[$entrystart..$entryend]

 foreach ($name in $basename) {
  write-host "looking for: $name"
  if ($docentry -match "$name"){
  write-host "found  $name"
  $msgcontent.removerange($entrystart,$entryend) # not work method gone due above line2: $msgcontent=get-content
  }
 }

 }


you add contents of file.txt array thusly:

$myarray=new-object system.collections.arraylist get-content 'c:\temp\services.txt' | foreach{$myarray.add($_)}

then have arraylist - maintaining method want use.

i stared at the code while, can't figure out you're trying do... if need more help, explain in english?


inspired carlsberg.



Windows Server  >  Windows PowerShell



Comments

Popular posts from this blog

Error: 0x80073701 when trying to add Print Services Role in Windows 2012 Standard

Disconnecting from a Windows Server 2012 R2 file sharing session on a Windows 7,8,10 machine

Windows 2016 RDS event 1306 Connection Broker Client failed to redirect the user... Error: NULL