0

I'm trying to write a script that will detect what COM port a device is plugged into, then map it to a new port.

Here is the output from the "change port" command:

PS C:\> change port
COM11 = \Device\19H2KP0
COM2 = \Device\Serial1
COM5 = \Device\Serial2
KEYSPAN#*USA19HMAP#00_00#{4d36e978-e325-11ce-bfc1-08002be10318} = \Device\ComUs19H-00
KEYSPAN#*USA19HMAP#00_00#{86e0d1e0-8089-11d0-9ce4-08003e301f73} = \Device\ComUs19H-00
USA19HP0 = \Device\19H2KP0

I need to capture the COM number prior to "\Device\19H2KP0". So in this example output, I would capture COM11 into a variable.

Next I need to run the "change port" command with that variable. i.e.:

change port COM1=$CapturedText

Thank you in advance for any assistance!

2 Answers 2

1

Do you already know what the 19H2KP0 bit will be? If so, you could use a regular expression to detect the unique ID using a look-ahead.

Here's a fully working example, using your example text:

$Output = @"
COM11 = \Device\19H2KP0
COM2 = \Device\Serial1
COM5 = \Device\Serial2
KEYSPAN#*USA19HMAP#00_00#{4d36e978-e325-11ce-bfc1-08002be10318} = \Device\ComUs19H-00
KEYSPAN#*USA19HMAP#00_00#{86e0d1e0-8089-11d0-9ce4-08003e301f73} = \Device\ComUs19H-00
USA19HP0 = \Device\19H2KP0
"@

$Output -match "COM[0-9]{1,2}(?=.*$Id)";

Write-Host -Object ('COM port is: {0}' -f $matches[0]);

And now here is the example, using the in-line command:

$Id = '19H2KP0';
$Output = change port;
$Output -match "COM[0-9]{1,2}(?=.*$Id)";
Write-Host -Object ('COM port is: {0}' -f $matches[0]);

Result

COM port is: COM11
2
  • So the script definetly detects the correct string based on the $Id, but I get the following error: COM7 = \Device\19H2KP0 Cannot index into a null array. At line:4 char:1 + Write-Host -Object ('COM port is: {0}' -f $matches[0]); + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (:) [], RuntimeException + FullyQualifiedErrorId : NullArray
    – sqone2
    Mar 27, 2014 at 23:25
  • That error means that the $matches variable is not getting populated. The regular expression must not be working ... what is the output in the new example? Can you update your question with the new example?
    – user189198
    Mar 28, 2014 at 0:32
0

Longer, but perhaps more intuitive, you can also use chained -match and -replace operators with simpler regexes:

$CapturedText = (change port) -match 'COM.+19h2kp0' -replace '^(COM\d+).+','$1'
$CapturedText
2
  • Hey thanks! This seems to work perfectly. Now I'm having an issue running the command: "change port COM1=COM7" from a variable. The text seems to execute just fine, but it doesn't actully run the DOS command "change port". $ComNum = (change port) -match 'COM.+19h2kp0' -replace '^(COM\d+).+','$1' change port COM1=) + $ComNum $changeport
    – sqone2
    Mar 28, 2014 at 14:14
  • Oops, the code got messed up. $ComNum = (change port) -match 'COM.+19h2kp0' -replace '^(COM\d+).+','$1' $changeport = (change port COM1=) + $ComNum $changeport
    – sqone2
    Mar 28, 2014 at 14:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.