PoshCode Archive  Artifact Content

Artifact b20d6a67d94398fdaf8c3b77ac53616b986f82abb77281d85437c8bce1d79e3a:

  • File Set-Privilege.ps1 — part of check-in [1865d9fead] at 2018-06-10 14:13:56 on branch trunk — I thought that it’s impossible. Some guy who calls himself gregzakh wrote a demo module called Func which allows you to invoke some WinAPI functions without defining dynamic modules in memory. This module correctly works on PowerShell v2, 4 and 5 (I have not got PowerShell v3 to test it). The script below uses Func library to set SeShutwondPrivilege privilege up for current PowerShell host. (user: Dan Jones size: 1292)

# encoding: ascii
# api: powershell
# title: Set-Privilege
# description: I thought that it’s impossible. Some guy who calls himself gregzakh wrote a demo module called Func which allows you to invoke some WinAPI functions without defining dynamic modules in memory. This module correctly works on PowerShell v2, 4 and 5 (I have not got PowerShell v3 to test it). The script below uses Func library to set SeShutwondPrivilege privilege up for current PowerShell host.
# version: 0.1
# type: script
# author: Dan Jones
# license: CC0
# function: Set-Privilege
# x-poshcode-id: 6358
# x-archived: 2016-05-28T02:16:52
# x-published: 2016-05-25T07:33:00
#
#
# this script is a part of Func module (https://github.com/gregzakh/Func)
function Set-Privilege {
  param(
    [Parameter(Position=0)]
    [ValidateRange(2, 35)]
    [UInt32]$Privilege = 19, #SeShutdownPrivilege
    
    [Parameter(Position=1)]
    [Switch]$Enable = $true
  )
  
  begin {
    $ptr, $null = Get-ProcAddress ntdll RtlAdjustPrivilege
    $RtlAdjustPrivilege = Set-Delegate $ptr `
         '[Action[UInt32, Boolean, Boolean, Text.StringBuilder]]'
    $ret = New-Object Text.StringBuilder
  }
  process {
    $RtlAdjustPrivilege.Invoke($Privilege, $Enable, $false, $ret)
  }
  end {}
}