Search This Blog

Tuesday, June 19, 2012

Powershell: Get SID of all logged on users

This function will list store the SIDs of all logged on users in an array:


Function Get-LoggedOnUsersSID
{
    #Create empty array
    $CurrentUser = @()
    #Store all processes named 'explorer.exe' in $proc
    $proc = Get-WmiObject win32_process -computer $ENV:COMPUTERNAME `
    -Filter "Name = 'explorer.exe'"
    #Go through collection of processes and save information in
    #$temp, extract username and store in $CurrentUser
    ForEach ($p in $proc) { 
        $temp = "" | Select Computer, Domain, User 
        $temp.computer = $c
        $temp.user = ($p.GetOwner()).User 
        $CurrentUser += $temp.User
    }

        #Create empty array
    $SID = @()


    #Translates all usernames stored into SID
    ForEach ($element in $CurrentUser) {
        $objUser = New-Object `
        System.Security.Principal.NTAccount ` 
        ("DOMAIN", $Element)
        $strID = $objUser.Translate `
        ([System.Security.Principal.SecurityIdentifier])
        $SID += $strID.Value
    }
    
    #Set return value to $SID, array containing all SIDs
    Return $SID
}

Function call:
$AllSID = Get-LoggedOnUsersSID

NO GUARANTEE THAT THIS FUNCTION WORKS WITHOUT ANY ERRORS IN YOUR ENVIRONMENT - PLEASE TEST IT FIRST!

Sources
http://learn-powershell.net/2010/11/01/quick-hit-find-currently-logged-on-users/
http://community.spiceworks.com/how_to/show/2776

Monday, June 18, 2012

Powershell: Set registry key

The New-Item CmdLet is used to create a registry key normally:
New-Item -Type String "HKLM:\Software\Example"

This works fine but that can be a roundabout way if you need to create a key whose subkeys are not existing:
New-Item -Type String "HKLM:\Software\Example"
New-Item -Type String "HKLM:\Software\Example\Sub1"
New-Item -Type String "HKLM:\Software\Example\Sub1\NewKey"


Last but not least, you have to set the value:
Set-ItemProperty "HKLM:\Software\Example\Sub1\NewKey" "Name" -value "John Doe" -type String


With the following function you are able to set an registry key and its value:

Function New-RegistryKey([string]$key,[string]$Name,[string]$type,[string]$value)
{
    #Split the registry path into its single keys and save
    #them in an array, use \ as delimiter:
    $subkeys = $key.split("\")
    
      #Do this for all elements in the array:
    foreach ($subkey in $subkeys)
    {
        #Extend $currentkey with the current element of
        #the array:
        $currentkey += ($subkey + '\')

        #Check if $currentkey already exists in the registry
        if (!(Test-Path $currentkey))
        {
            #If no, create it and send Powershell output
            #to null (don't show it)
            New-Item -Type String $currentkey | Out-Null
        }
     }
     #Set (or change if alreday exists) the value for $currentkey
      Set-ItemProperty $CurrentKey $Name -value $Value -type $type 
}

Function Call:
New-RegistryKey "<PATH>" "<NAME>" "<DATATYPE>" "<VALUE>"

Datatypes:
  • String
  • ExpandString (for use with environment variables)
  • Binary
  • DWord
  • Multistring
  • QWord

Example:
New-RegistryKey "HKLM:\Software\Vendor\MySoftware\2012\Example\Another\Key\StopHere" "Name" "STRING" "John Doe"

NO GUARANTEE THAT THIS FUNCTION WORKS WITHOUT ANY ERRORS IN YOUR ENVIRONMENT - PLEASE TEST IT FIRST!

Friday, June 15, 2012

Welcome!

Hi @all!

This is my first post in my new Powershell and SCCM blog. Here you will find both Powershell scripts (and tipps) and worthing knowing about Microsoft's SCCM 2012. Mostly I will use it as my own 'knowledge base', but feel free to find answers about your Powershell and SCCM questions! ;-)

Best regards, JeeCay