Skip to content

Instantly share code, notes, and snippets.

@rohnedwards
rohnedwards / QueryServiceObjectSecurity.ps1
Last active August 29, 2015 14:26
Demo code for using QueryServiceObjectSecurity() Win32 call from PowerShell. Created in response to a comment from here: https://rohnspowershellblog.wordpress.com/2013/03/19/viewing-service-acls/
<#
NOTE: I think you have to run this as an admin b/c ServiceHandle seems to
be null when not elevated. Also, this can't be used to get the SACL
(ServiceHandle probably isn't opened with that access requested, and
this code doesn't do privilege modifications). If you really want to
get around these problems, you can probably use reflection to get at
the GetServiceHandle() private method on the ServiceController
instance. That might work as a non-elevated user...
#>
/*
This is meant to be saved as a .cs file, and called with Add-Type inside PowerShell:
Add-Type -Path c:\path\to\this\gist.cs
*/
using System;
using System.Security.Principal;
using System.Collections.Generic;
using System.Linq;
@rohnedwards
rohnedwards / DetectPreviousCommandArgCompleter.ps1
Created May 18, 2018 00:34
Example of ArgumentCompleter looking back in Command AST
# See here for question: https://rohnspowershellblog.wordpress.com/2017/01/17/completing-parameter-values-with-other-parameter-values/#comment-437
function Test-DetectProperty {
<#
.SYNOPSIS
Proof of concept to show that inspecting preceding command elements is possible
from within ArgumentCompleters, at least in a limited way.
.EXAMPLE
Get-Service | Test-DetectProperty -Property <PressTab>
@rohnedwards
rohnedwards / Test-DynamicParamCreation.ps1
Created May 18, 2018 17:33
Example of peeking into unbound parameters inside DynamicParam block. This is a bad idea, and should only be used for playing around with the PowerShell engine.
# Link to original question: https://powershell.org/forums/topic/retrieve-the-input-value-of-the-dynamic-parameter/
function Test-DynamicParamCreation {
<#
.SYNOPSIS
Example of how to make dynamic param decisions based on other dynamic params.
NOTE: This probably isn't a good idea, as reflection is used to get at private
members of the PowerShell engine.
.DESCRIPTION
This function has a dynamic parameter named '-OtherDynamicParameterName' that