Monday, August 22, 2011

Some addendum on modules

If you add modules like  PSUserTools from Microsoft you receive some enhanced functionality. You also receive some well conceived script.  After you have imported your modules ('import-module'), you can use a function like that below to list all the exported commands:


function Global:get-module_exports {

[CmdletBinding()]
Param(
[Parameter(Mandatory=$true)]
$ModName
          )
$commands=(get-module $ModName).ExportedCommands
[Array[]]$list=(($commands).Values) | %{$_.Name} | Sort
$list
}


PS C:\> get-module -list
ModuleType Name                      ExportedCommands
---------- ----                      ----------------
...
Manifest   PSUserTools               {}
....
PS C:\> import-module PSUserTools
PS C:\> get-module_exports PSUserTools
Get-CurrentUser
Get-Everyone
Start-ProcessAsAdministrator
Test-IsAdministrator

'Get-CurrentUser' queries WMI to find domain and local users. Here is a simplified function derived from that script that prints out local users and their SIDs:



function net_user
{
function netusers {$query = "Win32_UserAccount";$query+= " WHERE LocalAccount='True'";Get-WmiObject $query }
netusers | % {($_.name)+" "+($_.SID)}
}

PS C:\ net_user  [SIDs deleted for privacy]
Administrator S-1-*
Guest S-1-*
rferrisx S-1-*



No comments: