Please read the very first pinned post on top of the list of this forum: Read Me Before Posting! You’ll be Glad You Did! And please format your code as code using the code tag button (pre). If you post some example data or output from a Powershell console you should format this as code as well.
You read your input file with Get-Content oder Import-CSv and iterate over each single line with a loop using your command line.
First, didn’t know you could do variable assignment with split like that, so props to @js on that one.
The parse works and does not error, so it’s erroring there. If the command you are running does require both server and IP, you need to wrap an ‘if’ around it:
$txt = @"
Server123;10.3.4.5
Server124;10.3.4.6
Server125;10.3.4.7
;10.3.4.8
Server126;
"@ -split [environment]::NewLine
'Processing {0} lines' -f $txt.Count
foreach ($line in $txt) {
$server, $ip = $line -split ';'
if ($server -and $ip) {
"Running command with {0} and {1}" -f $server, $ip
}
else {
"Don't have enough to run command with server {0} and ip {1}" -f $server, $ip
}
}
I wouldn’t call it a myth, it’s strongly-typed if I were to describe it. Powershell does automatic type conversions for simplicity, but wrapping it in @() or defining the type defines and\or forces the conversion to an array.