LockWorkstation using Powershell and pInvoke

[code language=”powershell” title=”Powershell Code”] $signature = @’ [DllImport("user32.dll", SetLastError = true)] public static extern bool LockWorkStation(); ‘@ Add-Type -MemberDefinition $signature -Name CustomName -Namespace CustomNamespace -PassThru [CustomNamespace.CustomName]::Lockworkstation() [/code] Notes Keyword PUBLIC is a must, or else wont be able to call the native function. Unlike variables, Add-Type cant be changed once created in a powershell session. …

SetDefaultPrinter using Pinvoke and Powershell

[code language=”powershell” wraplines=”false” collapse=”false”] $code = @’ using System; using System.Runtime.InteropServices; namespace ChangeDefaultPrinter { public class Program { [DllImport("Winspool.drv", CharSet=CharSet.Auto, SetLastError=true)] private static extern bool SetDefaultPrinter(string printerName); public static bool Run(string PrinterName) { bool default_result = SetDefaultPrinter(PrinterName); return default_result; } } } ‘@ cls Add-Type -TypeDefinition $code -Language CSharp #Set Default Printer – for network …

WordPress Appliance - Powered by TurnKey Linux