Last active
February 27, 2023 07:30
-
-
Save VimalShekar/c781ed8a9d638270b88634ad3eaada72 to your computer and use it in GitHub Desktop.
Instantiating IInternetSecurityManager interface in PowerShell
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Define CoCreateInstance function and IInternetSecurityManager interface | |
Add-Type -TypeDefinition @" | |
using System; | |
using System.Runtime.InteropServices; | |
using System.Runtime.InteropServices.ComTypes; | |
public class NativeMethods | |
{ | |
private const string CLSID_InternetSecurityManager = "7b8a2d94-0ac9-11d1-896c-00c04fb6bfc4"; | |
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); | |
} | |
[DllImport("ole32.Dll")] | |
public static extern UInt32 CoCreateInstance(ref Guid clsid,[MarshalAs(UnmanagedType.IUnknown)] object inner,uint context,ref Guid uuid,[MarshalAs(UnmanagedType.IUnknown)] out object rReturnedComObject); | |
[DllImport("ole32.dll", CharSet=CharSet.Unicode)] | |
public static extern int CLSIDFromProgID(string lpszProgID,out Guid pclsid); | |
//COPIED FROM https://www.pinvoke.net/default.aspx/Interfaces/IInternetSecurityManager.html | |
[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); | |
} | |
} // end class NativeMethods; | |
"@ -Language CSharp | |
[NativeMethods]::MapUrlToZone('http://www.microsoft.com') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment