ps/Modules/Cole.PowerShell.Developer/Public/Get-SiteTempDirectoryPath.ps1

104 lines
4.0 KiB
PowerShell
Raw Normal View History

2023-05-30 22:51:22 -07:00
function Get-SiteTempDirectoryPath {
[CmdletBinding()]
param (
[Parameter(Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[Alias('SiteName')]
[Alias('Site')]
[Alias('Name')]
[Alias('AppName')]
[string]$SiteOrAppName
)
Start-Job -InputObject @{ SiteOrAppName = $SiteOrAppName; } -ScriptBlock {
Import-Module WebAdministration
$SiteOrAppName = $Input.SiteOrAppName
$hashCode = @"
public class HashCode
{
// https://stackoverflow.com/a/41575325/109749 ish
public static string GetDirectoryName(string id, string appname, string path) {
string v_app_name64b = HashCode.GetStringHashCode(("/LM/W3SVC/" + id + "/" + appname + path).ToLower(System.Globalization.CultureInfo.InvariantCulture)).ToString("x", System.Globalization.CultureInfo.InvariantCulture);
return HashCode.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 $hashCode -CompilerParameters $cp;
$rootOrAppName = "root";
$physicalPath = "";
$siteId = "";
$website = (Get-Website $siteOrAppName);
if ($null -ne $website) {
$physicalPath = $website.PhysicalPath.TrimEnd("\") + "\";
$siteId = $website.id;
} else {
$appPool = (Get-WebApplication $siteOrAppName);
if ($null -eq $appPool) {
Write-Warning "could not match to any site or apppool. Did you provide the right information?"
exit -1;
}
$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";
exit -1;
}
}
$tempFilePath = "C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Temporary ASP.NET Files\$rootOrAppName\" + [HashCode]::GetDirectoryName($siteid, $rootOrAppName, $physicalPath);
return $tempFilePath
} | Receive-Job -Wait -AutoRemoveJob
}