Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save petrsvihlik/eae68ea160c9db788c39e5aa9eb774a1 to your computer and use it in GitHub Desktop.
Save petrsvihlik/eae68ea160c9db788c39e5aa9eb774a1 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}}
# Define references
$references = @(("CMS.Base.dll"),("CMS.DataEngine.dll"),("CMS.DataProviderSQL.dll"),("CMS.Membership.dll")) | Foreach-Object{ $bin + $_ }
# 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
#Add-Type -Path $references
# Inline source
$source = @"
using System;
using CMS.DataEngine;
using CMS.Membership;
public class UserUpdater
{
public static void UpdateUser()
{
// Initialize application
CMSApplication.Init();
// Get user
var user = UserInfoProvider.GetUserInfo("administrator");
// Make some changes
user.UserNickName = "John";
// Store the user back in the database
UserInfoProvider.SetUserInfo(user);
}
}
"@
# Load the class to the PowerShell's AppDomain
Add-Type -TypeDefinition $source -ReferencedAssemblies $references -IgnoreWarnings
# Call the method
[UserUpdater]::UpdateUser()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment