Basicly I am trying to find a way to put a variable for replacing a line in powershell.
The current script:
$switches = get-outlookinbox | where subject -eq "Hello"
$e = $switches.body
$e = $e.replace("Hello:","")
$e = $e.replace(" Number","")
$e = $e.replace(":1","")
$e = $e.replace(":2","")
$e = $e.replace(":3","")
$e = $e.replace(":4","")
$e = $e.replace(":99","")
You can see what I am going for here... But I don't want 99 lines of replace code, any thoughts on this? Also, the numbers MUST have : infront of it, otherwise the replace will corrupt the file since it contains only IP's and ports, it's the ports I want to remove from the output.
You can use a simple foreach
loop and iterate from 99
to 1
:
foreach ($n in 99..1)
{
$e = $e.Replace(":$n", " ")
}
Or, if you prefer one line:
foreach ($n in 99..1) { $e = $e.Replace(":$n", " ") }
Demo:
PS > $mystr = "a:1:2:3:4:5:6:7:8:9:10:11:12:13:14:15:16:17:18:19:20b"
PS > foreach ($n in 20..1) { $mystr = $mystr.replace(":$n", "") }
PS > $mystr
ab
PS >
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments