$var = @{ colour = 'red'; size = 'large' }
$var += @ { location = 'there' }
write-host $var['location']
Often you will see building custom objects with hash tables:$var = [PSCustomObject] @{ property1 ='value1'; property2 = 'value2' }
$var.property1
Import-Module ActiveDirectory
# Filter by single user and export to a CSV file.
$User = 'Username'
$schemaIDGUID = @{}
$schemaIDGUID[([guid]'00000000-0000-0000-0000-000000000000')] = 'All'
Get-ADObject -SearchBase (Get-ADRootDSE).schemaNamingContext -LDAPFilter '(schemaIDGUID=*)' -Properties name, schemaIDGUID |
ForEach-Object {$schemaIDGUID[([System.Guid]$_.schemaIDGUID)] = $_.name}
Get-ADObject -SearchBase "CN=Extended-Rights, $((Get-ADRootDSE).configurationNamingContext)" -LDAPFilter '(objectClass=controlAccessRight)' -Properties name, rightsGUID |
ForEach-Object {$schemaIDGUID[([System.GUID]$_.rightsGUID)] = $_.name}
#Get a list of AD objects.
$AOs = @()
$AOs += Get-ADOrganizationalUnit -Filter * # | Select-Object -ExpandProperty DistinguishedName
$AOs += Get-ADObject -SearchBase (Get-ADDomain).DistinguishedName -SearchScope Subtree -LDAPFilter '(ObjectClass=*)' # | Select-Object -ExpandProperty DistinguishedName
#Loop through each of the AD objects and retrieve their permissions.
$AOs | ForEach-Object {
$ao = $_
Get-Acl -Path "AD:\$($ao.DistinguishedName)" |
Select-Object -ExpandProperty Access |
Select-Object -Property `
@{n='DistinguishedName'; e={$ao.DistinguishedName}},
@{n='objectTypeName'; e={$schemaIDGUID[$_.objectType]}},
@{n='inheritedObjectyTypeName'; e={$schemaIDGUID[$_.inheritedObjectType]}},
*
} |
Where-Object {$_.IdentityReference -like "*$($User)*"} |
Select-Object DistinguishedName, IdentityReference, ActiveDirectoryRights, IsInherited -Unique |
Export-Csv -Path "C:\temp\explicit_permission.csv" -NoTypeInformation
This is frequently used to pass parameters.
For example, if I wanted to execute a series of commands on a remote computer, I would need to provide the name of the computer, together with a script block (the series of commands to execute). I could use a hash table to do that, and it may look something like this:
Open in new window
I don't have to use the hash table - I could put all the parameters on the Invoke-Command. Using the hash table makes the code a bit neater.