Wednesday 21 June 2017

Better update your photo with powershell for windows Outlook/Lync/Skype-for-business/etc

Yesterdays post required the Remote Server Administration Toolskit installed.

Here I show how to do it without that, just using powershell. Thanks to maxx-ode who gives the answer on this Microsoft Technet Forum post .

This code will set picture.jpg as your thumbnail picture.

$root = [System.DirectoryServices.ActiveDirectory.Domain]::GetCurrentDomain().GetDirectoryEntry()
$search = [System.DirectoryServices.DirectorySearcher]$root
$search.Filter = "(&(objectclass=user)(objectcategory=person) (samAccountName=$env:username))"
$result = $search.FindOne()
$user = $result.GetDirectoryEntry()
$user.put("thumbnailPhoto", [byte[]](Get-Content "picture.jpg" -Encoding byte))
$user.setinfo()

or you want to delete the photo:

$user.putex(1, "thumbnailPhoto", "")
$user.setinfo()

but if you are re-using $user you may want to precede with: $user.refreshcache()

If you get an error like this:

Exception calling "setinfo" with "0" argument(s): "A constraint violation occurred."
At line:1 char:1
+ $user.setinfo()
+ ~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : CatchFromBaseAdapterMethodInvokeTI

then it could be that your picture is too big, keep it less that 100K, and ideally much smaller than that.
A 100x100 picture should be around 4K which is more ideal.


Tuesday 20 June 2017

Update your photo with powershell for windows Outlook/Lync/Skype-for-business/etc


NOTE: See my following post which does not required the Remote Server Administration Tools to be installed.

Thanks to the EZ IT guy who writes explaining that by installing the Remote Server Administration Tools for Windows 10 (or Windows 7 if you need it) you can then read your windows domain thumbnail photo picture.jpg like this:

$record = Get-ADUser $ENV:USERNAME -Properties thumbnailPhoto
$record.thumbnailPhoto | Set-Content "picture.jpg" -Encoding byte

And, provided that your photo is less that 100K you can also upload it:

Set-ADUser $ENV:USERNAME -Replace @{thumbnailPhoto=([byte[]](Get-Content "picture.jpg" -Encoding byte))}

All this requires that you be logged into your domain. Also thanks to the WOSHUB for help simplifying it.

If only there were a way that didn't require installing Remote Server Administration Tools.

Also some talk here from heyscriptingguy.

Friday 16 June 2017

Putting self-tests in a C header file


You can compile a header file to C using gcc -x c like this:

gcc -x c -o feature-test feature.h

Without the -x c options it would have built a pre-compiled header instead of a program you can run.

But of course the compile will ultimately fail as you don't have a main( ) function, and when you think about it you know that you won't normally want one.

Except when compiling the top level header file directly.

The pre-processor symbol __INCLUDE_LEVEL__ is conveniently 0 for the top level file, which lets us do:

#if __INCLUDE_LEVEL__
int test1( ) {
  ...
}
...
int main(int argc, char **argv) {
  return test1() && test2() ... ;
}
#endif

So if the top level header file is build directly, then it gets a main and some test functions too.