[void][reflection.assembly]::Loadwithpartialname("Microsoft.SharePoint") | out-null [void][reflection.assembly]::Loadwithpartialname("Microsoft.Office.Server.Search") | out-null [void][reflection.assembly]::Loadwithpartialname("Microsoft.Office.Server") | out-null ################################################################################################################# # Get-ConsoleAsText.ps1 # Thanks to http://blogs.msdn.com/powershell/archive/2009/01/10/capture-console-screen.aspx for Get-ConsoleAsText.ps1 # The script captures console screen buffer up to the current cursor position and returns it in plain text format. # # Returns: ASCII-encoded string. # # Example: # # $textFileName = "$env:temp\ConsoleBuffer.txt" # .\Get-ConsoleAsText | out-file $textFileName -encoding ascii # $null = [System.Diagnostics.Process]::Start("$textFileName") ################################################################################################################# function Get-ConsoleAsText{ # Check the host name and exit if the host is not the Windows PowerShell console host. if ($host.Name -ne 'ConsoleHost') { write-host -ForegroundColor Red "This script runs only in the console host. You cannot run this script in $($host.Name)." exit -1 } # Initialize string builder. $textBuilder = new-object system.text.stringbuilder # Grab the console screen buffer contents using the Host console API. $bufferWidth = $host.ui.rawui.BufferSize.Width $bufferHeight = $host.ui.rawui.CursorPosition.Y $rec = new-object System.Management.Automation.Host.Rectangle 0,0,($bufferWidth - 1),$bufferHeight $buffer = $host.ui.rawui.GetBufferContents($rec) # Iterate through the lines in the console buffer. for($i = 0; $i -lt $bufferHeight; $i++) { for($j = 0; $j -lt $bufferWidth; $j++) { $cell = $buffer[$i,$j] $null = $textBuilder.Append($cell.Character) } $null = $textBuilder.Append("`r`n") } return $textBuilder.ToString() } ################################################################ # Pause Script function Pause ($Message="Script Complete. Press any key to continue...") { Write-Host -NoNewLine $Message $null = $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown") Write-Host "" } ################################################################ function Get-UserProfileConfigManager([string]$siteUrl) { # Need to get a PortalContext object # as we do not have a HttpContext we need to source one the hard way $site=new-object Microsoft.SharePoint.SPSite($siteUrl) $servercontext=[Microsoft.Office.Server.ServerContext]::GetContext($site) $site.Dispose() # clean up ################################################################ # Return the UserProfileConfigManager new-object Microsoft.Office.Server.UserProfiles.UserProfileConfigmanager($servercontext) } function Get-SPProfileManager([string]$siteUrl) { # Need to get a PortalContext object # as we do not have a HttpContext we need to source one the hard way $site=new-object Microsoft.SharePoint.SPSite($siteUrl) $servercontext=[Microsoft.Office.Server.ServerContext]::GetContext($site) $site.Dispose() # clean up ################################################################ # Return the UserProfileManager new-object Microsoft.Office.Server.UserProfiles.UserProfileManager($servercontext) } function Get-SPUserProfile([string]$siteUrl, [string] $u) { $upm= Get-SPProfileManager([string]$siteUrl) if ($upm.UserExists($u) -eq $false) { $upm.CreateUserProfile($u) $upm.GetUserProfile($u) } ################################################################ # Create function function Update-UserProfileProperty() { PARAM ( [string] $siteUrl = $( throw "You must provide a Site Collection Url e.g. 'http://moss/'"), [string] $userName = $( throw "You must provide a User Name e.g. 'DOMAIN\USERNAME'"), [string] $propName = $( throw "You must provide a User Profile Property Name e.g. 'WorkPhone'"), [string] $propValue = $( throw "You must provide a User Profile Property Value e.g. '0400 767 022'") ) END { if ($propValue -eq "NULL" -or $propValue -eq "" -or $propValue -eq "None") { Write-Host "Property '$propName' is not set ('$propValue')" } else { $cm = get-userprofileconfigmanager $siteUrl $spm = Get-SPProfileManager $siteUrl if ($spm.UserExists($userName)) { $userProfile = $spm.GetUserProfile($userName); $tempProp = $spm.Properties.GetPropertyByName($propName); if ($tempProp -eq $null) { throw "User Profile Property '$propName' does not exist!"; } else { $userProfile[$propName].Value = $propValue; $userProfile.Commit(); } write-host -foregroundcolor green "'$propName' User Profile Property updated to '$propValue' for '$userName'" Write-Output "'$propName' User Profile Property updated to '$propValue' for '$userName'" } else { Write-Host -ForegroundColor red "User '$userName' does not exist in User Profiles!" Write-output "User '$userName' does not exist in User Profiles!"; } } } } ################################################################ function MySiteProUpdate{ $username = "IB\Admin", "IB\Guest", "IB\PowerShell" # Set MySite URL $siteUrl = "http://mysites:3000" # Notifies Write-Host "Working... Please Wait" Write-Output " " foreach($u in $username){ # Executes command for all users in this script Write-Output "Executing this command : Update-UserProfileProperty $siteUrl $u ActiveEmployee true" Update-UserProfileProperty $siteUrl "$u" "ActiveEmployee" "true" } # Dump Console $textFileName = "C:\IBLogs\ConsoleDump.txt" Get-ConsoleAsText | out-file $textFileName -encoding ascii #$null = [System.Diagnostics.Process]::Start("$textFileName") pause } ################################################################ MySiteProUpdate | out-File C:\IBLogs\Progress.txt