Skip to content

Instantly share code, notes, and snippets.

@mbrownnycnyc
Last active August 29, 2015 14:07
Show Gist options
  • Save mbrownnycnyc/9e21600623337dd7ebc3 to your computer and use it in GitHub Desktop.
Save mbrownnycnyc/9e21600623337dd7ebc3 to your computer and use it in GitHub Desktop.
script that returns info about a user's outlook profile, specifically if they are configured to use a CAS hub or a hostname... error handling and logging are super cheaply done (I'm not proud of it)
function get-adusersid{
#http://community.spiceworks.com/how_to/show/2776-powershell-sid-to-user-and-user-to-sid
param(
[string]$domain,
[string]$user
)
$objUser = New-Object System.Security.Principal.NTAccount("$domain", "$user")
$strSID = $objUser.Translate([System.Security.Principal.SecurityIdentifier])
$strSID.Value
}
function get-outlookprofileserver {
param(
[string]$targetcomputer
)
# get user who is logged on at target
$fullusername = @(Get-WmiObject -computer $targetcomputer -class win32_computersystem)[0].username
$user = $fullusername.substring($fullusername.LastIndexOf('\')+1,$fullusername.length-$fullusername.LastIndexOf('\')-1)
$domain = $fullusername.substring(0,$fullusername.LastIndexOf('\'))
$usersid = get-adusersid -domain $domain -user $user
#query registry of the machine
# we will use powershell remoting for this, although there are other methods for remote access to the registry.
# HKEY_USERS\[SID]\Software\Microsoft\Windows NT\CurrentVersion\Windows Messaging Subsystem\Profiles\Outlook\*\ (where * is the GUID of the outlook profile)
# REG_SZ: 001e6602 <= this value should be present and be the CAS hub server
# also grab the following values for the log:
# REG_SZ: 001e6603
# REG_SZ: 001e6612
#search for 001e6602
# http://www.leeholmes.com/blog/2006/07/12/how-do-i-search-the-registry-for-a-value-in-powershell/
#should make this a bit more extensible... storing the results in an object, so that user can easily use convertto- cmdlets.
$invokable = '$regpath = "HKEY_USERS\' + $usersid + '\Software\Microsoft\Windows NT\CurrentVersion\Windows Messaging Subsystem\Profiles\Outlook\"; new-psdrive -psprovider registry -name HKLOUO -Root $regpath | out-null; get-childitem HKLOUO:\ -recurse -erroraction silentlycontinue | foreach { if ( (get-itemproperty -Path $_.PsPath) -match "001e6602" ) { $oldpath = $_.name; $newpath = $oldpath.replace("HKEY_USERS\' + $usersid + '\Software\Microsoft\Windows NT\CurrentVersion\Windows Messaging Subsystem\Profiles\Outlook\", "HKLOUO:\"); write-output "' + $targetcomputer + ',' + $domain + '\' + $user + ',$((get-itemproperty $newpath).""001e6602"")" } }'
$scriptbblock = $ExecutionContext.InvokeCommand.NewScriptBlock($invokable)
#the use of `tee` is part of the "cheapness" of the above usage of `write-output`
Invoke-Command -scriptblock $scriptbblock -computername $targetcomputer 2>&1 | tee -append -filepath report.log
}
$contents = $( get-adcomputer -filter "objectCategory -eq 'computer'" -SearchBase "OU=Workstations,OU=New York,DC=contoso,DC=corp" | select-object -expand name )
foreach ($targetcomputer in $contents) {
if (fastping $targetcomputer -erroraction silentlycontinue) {
get-outlookprofileserver -targetcomputer $targetcomputer
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment