Jump to content
Sign in to follow this  
lmilesuk

Windows 2003 Server Add Port Failure

Recommended Posts

lmilesuk

Hello,

I'm having issues when running the following code on Windows 2003 Server;

#include <WinAPI.au3>
Global $sFile, $hFile, $nBytes, $tBuffer, $Addr, $PNumber
$sFile = @ScriptDir & '\port.txt'
$tBuffer = DllStructCreate("byte[15]")
$hFile = _WinAPI_CreateFile($sFile, 2, 2)
_WinAPI_ReadFile($hFile, DllStructGetPtr($tBuffer), 15, $nBytes)
_WinAPI_CloseHandle($hFile)
$Addr = BinaryToString(DllStructGetData($tBuffer, 1))
;Create an IP Port
$PNumber = "9100"
MakeNewPort($Addr, $PNumber)
Func MakeNewPort($Address, $PortNumber)
    $objWMIService = ObjGet("winmgmts:\\" & @ComputerName & "\root\cimv2")
    $objNewPort = $objWMIService.Get("Win32_TCPIPPrinterPort").SpawnInstance_
    $objNewPort.Name = ""&$Address
    $objNewPort.Protocol = 1
    $objNewPort.HostAddress = $Address
    $objNewPort.PortNumber = $PortNumber
    $objNewPort.SNMPEnabled = False
    $objNewPort.Put_
EndFunc

This code works on XP, Vista, 7 and 2008 Server however when I attempt to use it on 2003 Server I get an Error message;

Error: The requested action with this object has failed.

Does anyone have any ideas??

Many Thanks for your time!

Share this post


Link to post
Share on other sites
UEZ

Maybe this link helps you: http://forums.windrivers.com/showpost.php?p=673522&postcount=9

Or try this:

Global $sFile, $hFile, $nBytes, $tBuffer, $Addr, $PNumber
$Addr = "169.254.110.14"
;Create an IP Port
$PNumber = "9100"
 
MakeNewPort($Addr, $PNumber)
 
Func MakeNewPort($Address, $PortNumber, $srv = ".")
    $objWMIService = ObjGet("winmgmts:{impersonationLevel=impersonate,(LoadDriver)}!\\" & $srv & "\root\cimv2")
    $objNewPort = $objWMIService.Get("Win32_TCPIPPrinterPort").SpawnInstance_
    $objNewPort.Name = "IP_" & $Address
    $objNewPort.Protocol = 1
    $objNewPort.HostAddress = $Address
    $objNewPort.PortNumber = $PortNumber
    $objNewPort.SNMPEnabled = False
    $objNewPort.Put_
EndFunc

Br,

UEZ

Edited by UEZ

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Share this post


Link to post
Share on other sites
lmilesuk

Thanks for the response,

How would I go about reading the IP address in from a text file?

If I add the code from my first file, the program fails again?

Does that have anything to do with the WinApi being added?

Share this post


Link to post
Share on other sites
UEZ

Is the code from post#2 working or not? That means can you see the ip port?

If yes then it is easy to load a text file and the ports.

If not then download the Windows Resource Kit for Server 2003, install it and register with regsvr32 prnadmin.dll from the install folder and try to run the script from post#2 again.

Br,

UEZ

Edited by UEZ

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Share this post


Link to post
Share on other sites
lmilesuk

It is working, thanks! It's just the reading from file.

Share this post


Link to post
Share on other sites
lmilesuk

Is the code from post#2 working or not? That means can you see the ip port?

If yes then it is easy to load a text file and the ports.

If not then download the Windows Resource Kit for Server 2003, install it and register with regsvr32 prnadmin.dll from the install folder and try to run the script from post#2 again.

Br,

UEZ

It does work however I cannot read in the ip address from a text file without the Win-API???

Share this post


Link to post
Share on other sites
UEZ

How does your IP file look like?

Br,

UEZ


Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Share this post


Link to post
Share on other sites
lmilesuk

It's just a blank .txt and from another application i input the numbers so before being set the file will be 0.0.0.0.

Share this post


Link to post
Share on other sites
UEZ

Try this:

Global $i, $sFile, $PNumber, $aIPs
$sFile = "ip.txt"
$PNumber = "9100"
$aIPs = StringSplit(StringStripCR(FileRead($sFile)), @LF, 2)
 
For $i = 0 To UBound($aIPs) - 1
    MakeNewPort($aIPs[$i], $PNumber)
Next
 
Func MakeNewPort($Address, $PortNumber, $srv = ".")
    $objWMIService = ObjGet("winmgmts:{impersonationLevel=impersonate,(LoadDriver)}!\" & $srv & "rootcimv2")
    $objNewPort = $objWMIService.Get("Win32_TCPIPPrinterPort").SpawnInstance_
    $objNewPort.Name = "IP_" & $Address
    $objNewPort.Protocol = 1
    $objNewPort.HostAddress = $Address
    $objNewPort.PortNumber = $PortNumber
    $objNewPort.SNMPEnabled = False
    $objNewPort.Put_
EndFunc

ip.txt should have following content as an example:

10.10.10.10
192.178.0.1
10.20.30.40

Br,

UEZ

Edited by UEZ

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Share this post


Link to post
Share on other sites
lmilesuk

Try this:

Global $i, $sFile, $PNumber, $aIPs
$sFile = "ip.txt"
$PNumber = "9100"
$aIPs = StringSplit(StringStripCR(FileRead($sFile)), @LF, 2)
 
For $i = 0 To UBound($aIPs) - 1
    MakeNewPort($aIPs[$i], $PNumber)
Next
 
Func MakeNewPort($Address, $PortNumber, $srv = ".")
    $objWMIService = ObjGet("winmgmts:{impersonationLevel=impersonate,(LoadDriver)}!\" & $srv & "rootcimv2")
    $objNewPort = $objWMIService.Get("Win32_TCPIPPrinterPort").SpawnInstance_
    $objNewPort.Name = "IP_" & $Address
    $objNewPort.Protocol = 1
    $objNewPort.HostAddress = $Address
    $objNewPort.PortNumber = $PortNumber
    $objNewPort.SNMPEnabled = False
    $objNewPort.Put_
EndFunc

ip.txt should have following content as an example:

10.10.10.10
192.178.0.1
10.20.30.40

Br,

UEZ

Hi thanks for your help bu that doesn't work, I keep getting the following error;

Line 10 (File "C:\Add_Port.exe");

Error: Variable must be of type "Object".

Share this post


Link to post
Share on other sites
lmilesuk

Any Ideas Uez??

Share this post


Link to post
Share on other sites
UEZ

Probably you have blank lines in your ip.txt file. The code above has no error check whether $Address is a valid ip address token or not!

Btw, did you register the prnadmin.dll from post#4?

Try this:

#include <Array.au3>
Global Const $oErrorHandler = ObjEvent("AutoIt.Error", "ObjErrorHandler")

Global $i, $sFile, $PNumber, $aIPs
$sFile = "ip.txt"
$PNumber = 9100
$aIPs = StringSplit(StringStripCR(FileRead($sFile)), @LF, 2)

For $i = 0 To UBound($aIPs) - 1
    MakeNewPort($aIPs[$i], $PNumber)
Next

Func MakeNewPort($Address, $PortNumber, $srv = ".") ;coded by UEZ 2011
    If Not IsInt(Number($PortNumber)) Then Return SetError(1, 0, 0)
    If Number($PortNumber) < 0 Or Number($PortNumber) > 0xFFFF Then Return SetError(2, 0, 0)
    StringRegExp($Address, "^(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})$", 3)
    If @error Then Return SetError(3, 0, 0)
    $objWMIService = ObjGet("winmgmts:{impersonationLevel=impersonate,(LoadDriver)}!\\" & $srv & "\root\cimv2")
    If @error Then Return SetError(4, 0, 0)
    $objNewPort = $objWMIService.Get("Win32_TCPIPPrinterPort").SpawnInstance_
    $objNewPort.Name = "IP_" & $Address
    $objNewPort.Protocol = 1
    $objNewPort.HostAddress = $Address
    $objNewPort.PortNumber = $PortNumber
    $objNewPort.SNMPEnabled = False
    $objNewPort.Put_
EndFunc

Func ObjErrorHandler()
    MsgBox(16, "Error", _
            "A COM Error has occured!" & @CRLF & @CRLF & _
            "err.description is: " & @TAB & $oErrorHandler.description & @CRLF & _
            "err.windescription:" & @TAB & $oErrorHandler & @CRLF & _
            "err.number is: " & @TAB & Hex($oErrorHandler.number, 8) & @CRLF & _
            "err.lastdllerror is: " & @TAB & $oErrorHandler.lastdllerror & @CRLF & _
            "err.scriptline is: " & @TAB & $oErrorHandler.scriptline & @CRLF & _
            "err.source is: " & @TAB & $oErrorHandler.source & @CRLF & _
            "err.helpfile is: " & @TAB & $oErrorHandler.helpfile & @CRLF & _
            "err.helpcontext is: " & @TAB & $oErrorHandler.helpcontext & @CRLF _
            )
EndFunc   ;==>ObjErrorHandler

Br,

UEZ

Edited by UEZ

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Share this post


Link to post
Share on other sites
lmilesuk

HI Again Uez it appears that this issue hasn't gone away on 2003 server, the script is working in regards to not getting any error messages however, the port isn't being added;

#include <Array.au3>

Global Const $oErrorHandler = ObjEvent("AutoIt.Error", "ObjErrorHandler")

Global $i, $sFile, $PNumber, $aIPs

$sFile = 'port.txt'

$PNumber = "9100"

$aIPs = StringSplit(StringStripCR(FileRead($sFile)), @LF, 2)

For $i = 0 To UBound($aIPs) - 1

MakeNewPort($aIPs[$i], $PNumber)

Next

Func MakeNewPort($Address, $PortNumber, $srv = ".")

StringRegExp($Address, "^(d{1,3}.d{1,3}.d{1,3}.d{1,3})$", 3)

If @error Then Return SetError(1, 0, 0)

$objWMIService = ObjGet("winmgmts:{impersonationLevel=impersonate,(LoadDriver)}!" & $srv & "rootcimv2")

$objNewPort = $objWMIService.Get("Win32_TCPIPPrinterPort").SpawnInstance_

$objNewPort.Name = "" & $Address

$objNewPort.Protocol = 1

$objNewPort.HostAddress = $Address

$objNewPort.PortNumber = $PortNumber

$objNewPort.SNMPEnabled = False

$objNewPort.Put_

EndFunc

Func ObjErrorHandler()

MsgBox(16, "Error", _

"A COM Error has occured!" & @CRLF & @CRLF & _

"err.description is: " & @TAB & $oErrorHandler.description & @CRLF & _

"err.windescription:" & @TAB & $oErrorHandler & @CRLF & _

"err.number is: " & @TAB & Hex($oErrorHandler.number, 8) & @CRLF & _

"err.lastdllerror is: " & @TAB & $oErrorHandler.lastdllerror & @CRLF & _

"err.scriptline is: " & @TAB & $oErrorHandler.scriptline & @CRLF & _

"err.source is: " & @TAB & $oErrorHandler.source & @CRLF & _

"err.helpfile is: " & @TAB & $oErrorHandler.helpfile & @CRLF & _

"err.helpcontext is: " & @TAB & $oErrorHandler.helpcontext & @CRLF _

)

EndFunc ;==>ObjErrorHandler

This is true for Server 2003 and win XP x32, however if I run the following code on xp x32 it works;

#include <WinAPI.au3>

Global $sFile, $hFile, $nBytes, $tBuffer, $Addr, $PNumber

$sFile = @ScriptDir & 'port.txt'

$tBuffer = DllStructCreate("byte[15]")

$hFile = _WinAPI_CreateFile($sFile, 2, 2)

_WinAPI_ReadFile($hFile, DllStructGetPtr($tBuffer), 15, $nBytes)

_WinAPI_CloseHandle($hFile)

$Addr = BinaryToString(DllStructGetData($tBuffer, 1))

;Create an IP Port

$PNumber = "9100"

MakeNewPort($Addr, $PNumber)

Func MakeNewPort($Address, $PortNumber)

$objWMIService = ObjGet("winmgmts:" & @ComputerName & "rootcimv2")

$objNewPort = $objWMIService.Get("Win32_TCPIPPrinterPort").SpawnInstance_

$objNewPort.Name = "IP_"&$Address

$objNewPort.Protocol = 1

$objNewPort.HostAddress = $Address

$objNewPort.PortNumber = $PortNumber

$objNewPort.SNMPEnabled = False

$objNewPort.Put_

EndFunc

But I get an error on server 2003.

Sorry to be pain but I don't really understand how or what the issue can be.

Share this post


Link to post
Share on other sites
UEZ

I just tested the script again on Win2k3 R2 standard and it seems to work properly!

I cannot say what's going wrong on your server!

Br,

UEZ


Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Share this post


Link to post
Share on other sites
lmilesuk

I'm not doing anything stange just trying to run the add port with text file in the same folder and I keep getting the same message;

Posted Image

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×