Hey Scripting Guy!
I am implementing a Command Line Interface to a Content Management System using powershell and web services. So far I've managed to do almost everything I want to except that I am stuck on one item ... NOT setting passwords. I have a function called "updateUser" that finds the named user via a webservice.getMemberByLoginName([string]Name) which returns a [Member] object filled in with the user details (Firstname, email, etc.) ... one of the fields is "Password" and it, of course for security reasons, is empty when returned. I want to use the webservice.updateMember([Member]member) to update any of the fields, leaving them as they arrive otherwise. For all of the fields except "Password", this works perfectly fine, however it fails for "Password" because Powershell is sending the empty string for Password and the call requires $null (or non-existent).
The webservice.updatePassword(user, [string]newpass, [string]oldpass) had a similar problem because if the authenticated webservice has sufficient privileges (a site administrator) then "oldpass" can be $null and the password will be set to "newpass", otherwise "oldpass" must contain the current password of "user". My failed powershell code to implement this simply leaves $oldpass as $null and invoke things as
$webservice.updatePassword($userid, $newpassword, $oldpassword
however that proved to fail at the web service because the web service wasn't given $null but the empty string, which is not the same thing (and an understandable difference when it comes to changing a password). I learned about this by using Fiddler to find out that when $oldpassword=$null the XML sent was <Password /> but what was needed was nothing at all. After a bit of research I got around this problem by more directly invoking the method:
$webservice.gettype().getmethod("UpdatePassword").Invoke($webservice,@($userid, $newpassword, $oldpassword))Now I can't really explain why this works, perhaps it has something to do with the array creation? The "why" hasn't been important to me until now because, well it worked! However in this case we are talking about a method parameter, in the case that is now failing we are talking about a property within an object.
[string] $user
$webservice = New-WebServiceProxy -Uri $server [Member] $member = $webservice.getMemberByLoginName($user) if ($tmp = Read-Host "Firstname?") {$member.FirstName = $tmp} if ($tmp = Read-Host "Password") {$member.Password = $tmp} $webservice.updateMember($member)
throws an error "Passwords must contain at least 6 characters" when $tmp = $null. I've verified that the Raw XML sent for password is the same <Password /> which I again interpret as the empty string and not $null. I will point out (again)
that an empty string for a pasword would indicate the user wants no password (something the CMS system wont allow) whereas $null indicates that no update shall occur to that property. I know the web service works for C# and Java clients, this is a powershell
specific problem.
So, sorry for the long intro but, how can I send $null as the value of an object property via web services?