Skip to content

Instantly share code, notes, and snippets.

@petrsvihlik
Last active August 16, 2016 10:03
Show Gist options
  • Save petrsvihlik/4727fa0944caf0988e60cb1cddd85938 to your computer and use it in GitHub Desktop.
Save petrsvihlik/4727fa0944caf0988e60cb1cddd85938 to your computer and use it in GitHub Desktop.
# Configure path to the root of your web project
$webRoot = "C:\inetpub\wwwroot\Kentico\CMS\"
$bin = $webRoot + "bin\"
# Load settings from web.config
[System.AppDomain]::CurrentDomain.SetData("APP_CONFIG_FILE", $webRoot + "web.config")
Add-Type -AssemblyName System.Configuration
[Configuration.ConfigurationManager].GetField("s_initState", "NonPublic, Static").SetValue($null, 0)
[Configuration.ConfigurationManager].GetField("s_configSystem", "NonPublic, Static").SetValue($null, $null)
([Configuration.ConfigurationManager].Assembly.GetTypes() | where {$_.FullName -eq "System.Configuration.ClientConfigPaths"})[0].GetField("s_current", "NonPublic, Static").SetValue($null, $null)
# Add DLL resolution path
[System.AppDomain]::CurrentDomain.AppendPrivatePath($bin);
# Load CMSDependencies
Get-ChildItem -recurse "$webRoot\CMSDependencies\"|Where-Object {($_.Extension -EQ ".dll")} | ForEach-Object { $AssemblyName=$_.FullName; Try {Add-Type -Path $AssemblyName} Catch{ "Failed to load assembly: " + $AssemblyName + " " + $_.Exception.LoaderExceptions}}
# Load all assemblies from \bin\ (to be sure that all handlers get attached etc.)
Get-ChildItem -recurse "$bin"|Where-Object {($_.Extension -EQ ".dll")} | ForEach-Object { $AssemblyName=$_.FullName; Try {Add-Type -Path $AssemblyName} Catch{ "Failed to load assembly: " + $AssemblyName + " " + $_.Exception.LoaderExceptions}}
# Optionally, replace the above with loading only the minimum set of assemblies
#$references = @(("CMS.Base.dll"),("CMS.DataEngine.dll"),("CMS.DataProviderSQL.dll"),("CMS.Membership.dll")) | Foreach-Object{ $bin + $_ }
#Add-Type -Path $references
# If the API you are going to use is about to touch the file system set the web application root path
#[CMS.Base.SystemContext]::WebApplicationPhysicalPath = $webRoot
# Initialize application
"Application initialized:" + [CMS.DataEngine.CMSApplication]::Init();
# Get users
$user = [CMS.Membership.UserInfoProvider]::GetUserInfo("administrator")
$user.UserNickName = "Joe"
[CMS.Membership.UserInfoProvider]::SetUserInfo($user)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment