Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save sopsmattw/edcb911046f1f28764027c5a70238666 to your computer and use it in GitHub Desktop.
Save sopsmattw/edcb911046f1f28764027c5a70238666 to your computer and use it in GitHub Desktop.
# Map URLs to Internet Explorer Security Zones via PowerShell
$csSource = @'
using System;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.ComTypes;
public class IEZones
{
private const string CLSID_InternetSecurityManager = "7b8a2d94-0ac9-11d1-896c-00c04fb6bfc4";
private const int E_FAIL = unchecked((int)0x80004005);
private const int ERROR_FILE_EXISTS = unchecked((int)0x80070050);
private const uint SZM_CREATE = 0;
private const uint SZM_DELETE = 1;
public const uint ZoneLocalMachine = 0;
public const uint ZoneIntranet = 1;
public const uint ZoneTrusted = 2;
public const uint ZoneInternet = 3;
public const uint ZoneUntrusted = 4;
public static uint MapUrlToZone(string url)
{
IInternetSecurityManager manager = CreateInternetSecurityManager();
uint Zone;
manager.MapUrlToZone(url, out Zone, 0);
return Zone;
}
public static IInternetSecurityManager CreateInternetSecurityManager()
{
Type iismType = Type.GetTypeFromCLSID(new Guid(CLSID_InternetSecurityManager));
return (IInternetSecurityManager)Activator.CreateInstance(iismType);
}
}
[ComImport, GuidAttribute("79EAC9EE-BAF9-11CE-8C82-00AA004BA90B")]
[InterfaceTypeAttribute(ComInterfaceType.InterfaceIsIUnknown)]
public interface IInternetSecurityManager
{
[return: MarshalAs(UnmanagedType.I4)]
[PreserveSig]
int SetSecuritySite([In] IntPtr pSite);
[return: MarshalAs(UnmanagedType.I4)]
[PreserveSig]
int GetSecuritySite([Out] IntPtr pSite);
[return: MarshalAs(UnmanagedType.I4)]
[PreserveSig]
int MapUrlToZone([In, MarshalAs(UnmanagedType.LPWStr)] string pwszUrl,
out UInt32 pdwZone, UInt32 dwFlags);
[return: MarshalAs(UnmanagedType.I4)]
[PreserveSig]
int GetSecurityId([MarshalAs(UnmanagedType.LPWStr)] string pwszUrl,
[MarshalAs(UnmanagedType.LPArray)] byte[] pbSecurityId,
ref UInt32 pcbSecurityId, uint dwReserved);
[return: MarshalAs(UnmanagedType.I4)]
[PreserveSig]
int ProcessUrlAction([In, MarshalAs(UnmanagedType.LPWStr)] string pwszUrl,
UInt32 dwAction, out byte pPolicy, UInt32 cbPolicy,
byte pContext, UInt32 cbContext, UInt32 dwFlags,
UInt32 dwReserved);
[return: MarshalAs(UnmanagedType.I4)]
[PreserveSig]
int QueryCustomPolicy([In, MarshalAs(UnmanagedType.LPWStr)] string pwszUrl,
ref Guid guidKey, ref byte ppPolicy, ref UInt32 pcbPolicy,
ref byte pContext, UInt32 cbContext, UInt32 dwReserved);
[return: MarshalAs(UnmanagedType.I4)]
[PreserveSig]
int SetZoneMapping(UInt32 dwZone,
[In, MarshalAs(UnmanagedType.LPWStr)] string lpszPattern,
UInt32 dwFlags);
[return: MarshalAs(UnmanagedType.I4)]
[PreserveSig]
int GetZoneMappings(UInt32 dwZone, out IEnumString ppenumString,
UInt32 dwFlags);
}
[ComImport, GuidAttribute("6D5140C1-7436-11CE-8034-00AA006009FA")]
[InterfaceTypeAttribute(ComInterfaceType.InterfaceIsIUnknown)]
public interface IServiceProvider
{
void QueryService(ref Guid guidService, ref Guid riid,
[MarshalAs(UnmanagedType.Interface)] out object ppvObject);
}
'@
Add-Type -TypeDefinition $csSource -Language CSharp
[IEZones]::MapUrlToZone('http://www.microsoft.com')
[IEZones]::MapUrlToZone('http://intranet') -eq [IEZones]::ZoneIntranet
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment