1

i'm trying to get the Installation ID for a few windows 7 units. this is what i have. iv'e tried moving the brackets and that helps or makes it worse depending on where i move it.

i have a list of ip addresses and i want it to output it with the ID and IP and it does that but it just repeats the first ip address. i'm assuming its just a formatting issue but thought id ask for help.

tried moving the array around, had to fiddle with the split command to get the output how i want it. now i just need to go ip by ip address and output like shown.

# Read all the computers from the file
$computers = Get-Content "C:\Users\Administrator\Desktop\DavidsScripts\Test\input.txt"

# Perform an operation for each row in the file
$result = @(
  foreach ($strComputer in $computers) {
    cscript C:\Windows\System32\slmgr.vbs $strComputer username password /dti
  }
)

$result.split(":")[3, 4, 6] | Out-File -FilePath .\output.txt -Append

no errors just not going ip by ip address

changed username and password for obvious security reasons

mklement0
  • 382,024
  • 64
  • 607
  • 775
david weaver
  • 37
  • 1
  • 7

1 Answers1

2

$result contains an array of strings.

While $result.split(":") does apply splitting to the each element (courtesy of member-access enumeration), it simply concatenates the resulting token arrays, returning a single, flat array.

Therefore, applying indexing to that flat array - [3,4,6] - returns only 3 elements in total, across all elements, which is not your intent.

Instead, you must iterate over the elements of $result and split and slice each element:

$(
  foreach ($line in $result) {
    $line.Split(':')[3, 4, 6]
  }
) | Out-File -LiteralPath .\output.txt
mklement0
  • 382,024
  • 64
  • 607
  • 775