Script pulling up historic data

So I have a master script that works. I wanted to pull a small section to create a stand alone script. It works…the first time. Then if I repeat the process it seems to pull the information from the previous run. My scripting is still fairly bad so cant get my head around where the issue is.

Incidentally, there are normally about 80 2nd octets and 10 3rd octets but I stripped them out just to not be posting internal data

cls $IP = Read-Host -Prompt "What is the IP Address of the computer?" #split IP address and query second and third octets against following table to output Network and VLAN ForEach-Object { $IP = ($IP).Split('.')
switch ($IP[1]) { 168 { $Location = "ADSL Unmanaged Network " } default { $Location = "Other" } }
switch ($IP[2]) { 216 { $User = "Lab Test PC" } default { $User = "Other" } } "-----------------------------------------------------------------------------------------------------------------------------------" Write-host -ForegroundColor Green "`n" "> Network Information" "`n" $obj = New-Object –typename PSObject $obj | Add-Member –membertype NoteProperty –name 'IPAddress' –value ("$($IP)") –PassThru | Add-Member –membertype NoteProperty –name 'Network' –value ("$Location") –PassThru | Add-Member –membertype NoteProperty –name 'VLAN' –value ("$User") #$All += $obj }
Now, if I get this working, heres the tricky part - am i right in thinking that I can turn this into a command that I can run from powershell instead of just a script?

Of course you can, you can create a small PowerShell module and keep this code inside a function in that module.
You would not use Read-Host instead get the input using parameters. Follow below link to start with.

I’ll turn it into module once I fix. Any ideas why it’s broken?

Curious to know, Why are you using ForEach-Object here ? What was your expectation with ForEach-Object cmdlet usage ?
There is no content to iterate over. Can you explain what you were trying to achieve ?