ps/Modules/Alkami.PowerShell.IIS/Public/Get-SiteTempDirectoryPath.ps1

99 lines
3.7 KiB
PowerShell
Raw Normal View History

2023-05-30 22:51:22 -07:00
function Get-SiteTempDirectoryPath($siteOrAppName) {
<#
.SYNOPSIS
This function gets a website's temporary directory path
#>
[CmdletBinding()]
[OutputType([string])]
$AlkamiHashCode = @"
public class AlkamiHashCode
{
// https://stackoverflow.com/a/41575325/109749 ish
public static string GetDirectoryName(string id, string appname, string path) {
string v_app_name64b = AlkamiHashCode.GetStringHashCode(("/LM/W3SVC/" + id + "/" + appname + path).ToLower(System.Globalization.CultureInfo.InvariantCulture)).ToString("x", System.Globalization.CultureInfo.InvariantCulture);
return AlkamiHashCode.GetString64(v_app_name64b).ToString("x8") + "\\" + v_app_name64b;
}
// System.Web.Util.StringUtil.GetStringHashCode() (System.Web.4.0.0.0) via System.Web.Hosting.ApplicationManager.CreateAppDomainWithHostingEnvironment(string appId, IApplicationHost appHost, HostingEnvironmentParameters hostingParameters)
internal unsafe static int GetStringHashCode(string s)
{
fixed(char* ptr = s){
int num = 352654597;
int num2 = num;
int* ptr2 = (int*)ptr;
for (int i = s.Length; i > 0; i -= 4)
{
num = ((num << 5) + num + (num >> 27) ^ *ptr2);
if (i <= 2)
{
break;
}
num2 = ((num2 << 5) + num2 + (num2 >> 27) ^ ptr2[1]);
ptr2 += 2;
}
return num + num2 * 1566083941;
}
}
// https://stackoverflow.com/a/41575325/109749
internal static unsafe int GetString64(string s)
{
fixed (char* str = s)
{
int num3;
char* chPtr = str;
int num = 0x1505;
int num2 = num;
for (char* chPtr2 = chPtr; (num3 = chPtr2[0]) != '\0'; chPtr2 += 2)
{
num = ((num << 5) + num) ^ num3;
num3 = chPtr2[1];
if (num3 == 0)
{
break;
}
num2 = ((num2 << 5) + num2) ^ num3;
}
return (num + (num2 * 0x5d588b65));
}
}
}
"@;
$cp = [System.CodeDom.Compiler.CompilerParameters]::new($assemblies);
$cp.CompilerOptions = '/unsafe';
Add-Type -TypeDefinition $AlkamiHashCode -CompilerParameters $cp;
$rootOrAppName = "root";
$physicalPath = "";
$siteId = "";
$website = (Get-Website $siteOrAppName);
if ($null -ne $website) {
$physicalPath = $website.PhysicalPath.TrimEnd("\") + "\";
$siteId = $website.id;
} else {
#This part does not work as expected just yet
$appPool = (Get-WebApplication $siteOrAppName);
if ($null -eq $appPool) {
Write-Warning "could not match to any site or apppool for $siteOrAppName"
return "";
} else {
$sitename = $appPool.GetParentElement().attributes["name"].Value;
$website = (Get-Website $sitename);
if ($null -ne $website) {
$physicalPath = $website.PhysicalPath.TrimEnd("\") + "\";
$siteId = $website.id;
$rootOrAppName = $siteOrAppName
} else {
Write-Warning "Could not find the site for $siteOrAppName";
return "";
}
}
}
$tempFilePath = "C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Temporary ASP.NET Files\$rootOrAppName\" + [AlkamiHashCode]::GetDirectoryName($siteid, $rootOrAppName, $physicalPath);
return $tempFilePath
}