Changing Serial ports from COMX to COMY using WMI

rated by 0 users
This post has 3 Replies | 1 Follower

Not Ranked
Posts 3
kobos Posted: 07-01-2011 10:56 AM

Scenario: I have a thermal printer connected to a serial port currently assigned the number of COM1, I need to change this port number to say COM55. Now this has to be done silently (ie in code or script), if i had the luxury of just going through device manager i would but that isn't possible as this has to be part of an installer. The terminal services command "Change Port COM1=COM55" relies on group policy settings being correct and is reset when the computer shuts down. And so my only real option without trawling the registry and changing each corresponding key - some of which are dependent on the driver manufacturer - is to use WMI.



#change the qualifiers for "DeviceID" to supposedly allow you to write to it

$writeableProperties = Get-WmiObject -List -Recurse Win32_SerialPort |
    select -Expand Properties | Where-Object { $_.Name -eq 'DeviceID' }

$writeableProperties.Qualifiers.Add("write",$true)

#Pull out the SerialPort
$SP = Get-WMIobject Win32_SerialPort | Where-Object { $_.DeviceID -eq 'COM1' }

#change the Device ID
$SP.DeviceID = "COM55"


This doesn't work as intended, ie it changes the deviceid but that doesn't propagate further, i'm therefore under the impression that there is a parent object to this that determines the port number. I'm fully aware of how bad this all is, but i have little choice.


any help would be greatly appreciated.



K

Top 25 Contributor
Posts 296
Microsoft MVP
Top Contributor

You won't be able to set the id in this way.  The DeviceId property is read only.  See

http://msdn.microsoft.com/en-us/library/aa394413(VS.85).aspx

Its also the key for the class which makes it even harder to change as it is the property used to uniquely identify the port

I'll see if I can research a method for you

Not Ranked
Posts 3

Yea, this is all hackety-hack at the moment. Any help would be greatly appreciated,  I am considering hooking MMC and seeing what the Change ports button actually does.

Not Ranked
Posts 3

I hooked MMC in the end and crafted(hacked) a solution in C++/Win32, i need to generalize it and tidy up some of the more dangerous aspects but i'll try to post a code sample in the coming days that works smoothly.

Page 1 of 1 (4 items) | RSS
Copyright 2012 PowerShell.com. All rights reserved.