dotps1 / HPWarranty

PowerShell module to help retrieve Hewlett-Packard Warranty Information
https://www.powershellgallery.com/packages/HPWarranty/
43 stars 17 forks source link

Problem with Spaces in WMI Output #2

Closed RonnyBunke closed 9 years ago

RonnyBunke commented 9 years ago

The variable cannot be verified because the value of CZJ74703ZY is not a valid value for the SerialNumber variable. In C:\Windows\system32\WindowsPowerShell\v1.0\Modules\HPWarranty\HPWarranty.psm1:155 Characters:17

+ $SerialNumber = (Get-WmiObject -Class Win32_Bios -ComputerName $ ...

Thats its right way. $SerialNumber = ((Get-WmiObject -Class Win32_Bios -ComputerName $ComputerName -ErrorAction Stop).SerialNumber).Trim()

dotps1 commented 9 years ago

I'm not sure why you would have spaces in the SerialNumber, if you look at line 170, i do use the .Trim() method when i replace the SerialNumber in the XML before i execute the SOAP Request to HP ISEE. The reason i do it there, is incase a space is added when the -SerailNumber or -ProductModel parameter is used, rather then the WMI information. but, it would affect the value of $SerialNumber regardless of if it comes from WMI or the -SerailNumber Parameter.

RonnyBunke commented 9 years ago

I do not know why but the WMI request delivered to us space. And you define the variable -SerailNumber with 10 characters.

Thus, it comes without the .Trim () to he error message.

dotps1 commented 9 years ago

The 10 char length validation is only on the parameter, so if it is throwing an error at line 155 that means the -SerailNumber parameter is NOT being used, thus that means the parameter validation is not being executed. The only way the code block containing line 155 is being executed is if you either used the current machine as the -ComputerName Parameter or a remote machine as the -ComputerName. Hence, a character length is irrelevant. I specify the expected length of an HP Serial Number, 10 chars. So, if you want to use WMI to populate that value, use the .Trim() in the Parameter.

Invoke-HPWarrantyRegistrationRequest -SerialNumber ((Get-WmiObject -Class Win32_Bios).SerialNumber).Trim() -ProductNumber <ProductNumber>

Otherwise, if you just pass a -ComputerName ParameterValue, it can go get the value remotely via WMI and it can be any length, and it will be Trimmed at the time it is replaced in the SOAP Request.

Thank you.

Sacknuss commented 9 years ago

I encountered the same problem. I think the limitation also affects the variable $SerialNumber on line 155. If you add the trim on line 155 and line 156 it works.

$SerialNumber = (Get-WmiObject -Class Win32_Bios -ComputerName $ComputerName -ErrorAction Stop).SerialNumber.Trim()
$ProductModel = (Get-WmiObject -Class Win32_ComputerSystem -ComputerName $ComputerName -ErrorAction Stop).Model.Trim()

Also I had to adjust some lines from 169 to 171, I guess it's some kind of typo but I'm not able to find it. Maybe I added it myself, have to compare next week my version and the original.

[Xml]$registrationSOAPRequest = (Get-Content "$PSScriptRoot\RegistrationSOAPRequest.xml")`
        -replace "<UniversialDateTime>",(Get-Date).ToString('yyyy\/MM\/dd HH:mm:ss \G\M\T') `
        -replace '<SerialNumber>', $SerialNumber `
        -replace '<ProductModel>', $ProductModel

I'll try to attach the error messages and verify my guess on the second problem with the typo next week.

Cheers

dotps1 commented 9 years ago

Ok, i have no problem really adding the .Trim() to the actions. I just am failing to see how a space returning from Get-WmiObject is causing an error. What happens if you run this command out side of the module:

$x = (Get-WmiObject -Class Win32_Bios).SerialNumber
$y = (Get-WmiObject -Class Win32_ComputerSystem).Model

Because that is essentially what line 155 and 156 is doing.

on a second note, im not sure what the typo is, it looks fine to me, but i see your removed the .ToUniversalTime() method, that could cause errors in the SOAP Request, but not a syntax error.

Thanks!

dotps1 commented 9 years ago

Updated Code in latest commit, Module Version 0.0.2.0

Sacknuss commented 9 years ago

I'm able to execute the commands without any issues. I think the problem is that you limit the variable $SerialNumber to 10 chars. What's the reason behind that decision? We also have a few serial numbers with 12 chars.

[Parameter(ParameterSetName = 'Static', 
    Mandatory = $true)] 
[ValidateLength(10,10)] 
[Alias("SN")] 
[String] 
$SerialNumber, 
dotps1 commented 9 years ago

That validation started when i first began dev on this project, and i was keying in 100s of serials, and out of the 1600 workstations i manage, they all have 10 digit S/Ns. I have yet to see a serial with more or less then 10 characters. I suppose it maybe time to put some research into that. Thanks for the info.

Sacknuss commented 9 years ago

I did a quick search and found this:

http://h30499.www3.hp.com/t5/ProLiant-Servers-ML-DL-SL/Decoding-Proliant-10-digit-Serial-Number/m-p/3544305#M41706

Long story short:

Compaq Serial Number -> 12 HP Serial Number -> 10

dotps1 commented 9 years ago

thanks for the info, and i have updated the param validation to:

[ValidateLength(10,12)]

Thanks again!