PowerShell – Check For Matching Items In Seperate Arrays

$test1 = (1,2,3)
$test2 = (2,4,6)
$test3 = (1,3,5)
( ( $test1 + $test2 + $test3) | Group-Object |?{$_.Count -gt 1}).Values

Example outputs:

PS C:\> $test1 = (1,2,3)
$test2 = (2,4,6)
$test3 = (1,3,5)
( ( $test1 + $test2 + $test3) | Group-Object |?{$_.Count -gt 1}).Values
1
2
3
PS C:\>  

PowerShell Special Characters and Tokens (short-handing):
http://www.neolisk.com/techblog/powershell-specialcharactersandtokens

PowerShell – Registry & File System Access Control Lists (ACL) Creation

#
# File system:
# http://msdn.microsoft.com/en-us/library/system.security.accesscontrol.filesystemaccessrule(v=vs.110).aspx
#
# Name of the user or group:
$objIdentityReference = [System.Security.Principal.NTAccount]("HOSTNAME\username")
# Rights to assign. List: http://msdn.microsoft.com/en-us/library/system.security.accesscontrol.filesystemrights(v=vs.110).aspx
$objFileSystemRights = [System.Security.AccessControl.FileSystemRights]"Read,Write"
# Permission inheritance: None, ContainerInherit, Object Inherit
$objInheritanceFlags = [System.Security.AccessControl.InheritanceFlags]::None
# Propagation of permissions: None, InheritOnly, NoPropagateInherit
$objPropagationFlags = [System.Security.AccessControl.PropagationFlags]::None
# Allow or Deny:
$objAccessControlType = [System.Security.AccessControl.AccessControlType]::Allow
# Create new rule:
$objRule = New-Object System.Security.AccessControl.FileSystemAccessRule ($objIdentityReference, $objFileSystemRights, $objInheritanceFlags, $objPropagationFlags, $objAccessControlType)
$objACL = Get-ACL "C:\TestFolder"
# Add our new rule:
$objACL.AddAccessRule($objRule)
# Set the new permissions:
Set-ACL "C:\Path\To\Folder" $objACL

# Alternatively, create your rule list manually:
$AclRuleList = New-Object System.Security.AccessControl.DirectorySecurity
$AclRuleList.AddAccessRule($objRule)
$AclRuleList.SetOwner([System.Security.Principal.NTAccount]"Administrators")
# http://msdn.microsoft.com/en-us/library/system.security.accesscontrol.objectsecurity.setaccessruleprotection(v=vs.110).aspx
$AclRuleList.SetAccessRuleProtection($true,$false) #isProtected,preserveInheritance

#
# Registry:
# http://msdn.microsoft.com/en-us/library/system.security.accesscontrol.registryaccessrule(v=vs.110).aspx
#
# Name of the user or group:
$objIdentityReference = [System.Security.Principal.NTAccount]("HOSTNAME\username")
# Rights to assign. List: http://msdn.microsoft.com/en-us/library/system.security.accesscontrol.registryrights(v=vs.110).aspx
$objRegistryRights = [System.Security.AccessControl.RegistryRights]::FullControl
# Permission inheritance: None, ContainerInherit, Object Inherit
$objInheritanceFlags = [System.Security.AccessControl.InheritanceFlags]::None
# Propagation of permissions: None, InheritOnly, NoPropagateInherit
$objPropagationFlags = [System.Security.AccessControl.PropagationFlags]::None
# Allow or Deny:
$objAccessControlType = [System.Security.AccessControl.AccessControlType]::Allow

# Create your new rule:
$objRule = New-Object System.Security.AccessControl.RegistryAccessRule ($objIdentityReference, $objRegistryRights, $objInheritanceFlags, $objPropagationFlags, $objAccessControlType)
$objACL = Get-ACL "HKCU:\TEST"
# Add our new rule:
$objACL.AddAccessRule($objRule)
# Set the new permissions:
Set-ACL "HKCU:\TEST" $objACL

# Alternatively, create your rule list manually:
$AclRuleList = New-Object System.Security.AccessControl.RegistrySecurity
$AclRuleList.AddAccessRule($objRule)
$AclRuleList.SetOwner([System.Security.Principal.NTAccount]"Administrators")
$AclRuleList.SetAccessRuleProtection($true,$false)

WMIC, WmiObject, CimInstance Formatting

wmic bios get serialnumber

Reference: http://support.microsoft.com/kb/558124

New formats:

Get-WmiObject -Class "Win32_SystemEnclosure" | Select-Object SerialNumber
# OR
Get-CimInstance -Class "Win32_SystemEnclosure" | Select-Object SerialNumber

Another type of Query:

Get-WmiObject -Query "SELECT * FROM Win32_ComputerSystem" -ComputerName .
# OR
Get-CimInstance -Query "SELECT * FROM Win32_ComputerSystem" -ComputerName .

PowerShell – Dissecting Parameters and Enumeration

Get-Command Get-Mailbox | Get-Member Param*
(Get-Command Get-Mailbox).Parameters | Format-List
(Get-Command Get-Mailbox).ParameterSets | Format-List name # Returns: __AllParameterSets
$m = (Get-Command Get-Mailbox).ParameterSets[0].Parameters | ?{$_.Name -eq "Filter"}

References:

http://blogs.msdn.com/b/powershell/archive/2006/05/10/594175.aspx

 

 

Enumeration:

function Select-Color
{
  param(
  [System.ConsoleColor]
  $Color
  )

  "You chose $Color"
}

Then enter: Select-Color -Color
Snippet from:
http://powershell.com/cs/blogs/tips/archive/2012/11/16/using-enumeration-types-for-parameter-intellisense.aspx
References:
http://blogs.msdn.com/b/powershell/archive/2007/01/23/how-to-create-enum-in-powershell.aspx

PowerShell – Dispose()

You may be asking yourself “Why is that one script keeping all my RAMs?!”.

Maybe you should call the .Dispose() method on the variable to clean-up.

Another option is to use Remove-Variable .

References:
http://technet.microsoft.com/en-us/library/ff730962.aspx
http://msdn.microsoft.com/en-us/library/system.management.automation.powershell.dispose(v=vs.85).aspx

PowerShell – Workflows

Reference material:
http://technet.microsoft.com/en-us/library/jj134242.aspx
http://blogs.technet.com/b/heyscriptingguy/archive/2012/12/26/powershell-workflows-the-basics.aspx

Import-Module PSWorkflow

workflow Test-WorkflowInline{
    InlineScript{
        $object = New-Object -TypeName PSObject
        $object | Add-Member -MemberType NoteProperty `
                -Name ExampleProperty `
                -Value 'Hello!'
        $object | Get-Member
    }
    # The $object variable no longer exists at this point as it has left the InlineScript block.
    #The below line fails if uncommented.
    # $object |Get-Member
}

workflow Test-WorkflowParallel{
    "This line will always run first!"
    parallel {
        "Command One."
        "Command Two."

        sequence{
            "Command Alpha."
            "Command Beta."
        }
    }
}

# Possible output from Test-WorkflowParallel:
# This line will always run first!
# Command One.
# Command Alpha.
# Command Beta.
# Command Two.

PowerShell – ActiveDirectory Searches

Cool stuff. Get every AD User Object that has an E-mail message attribute assigned:

Import-Module ActiveDirectory
Get-ADObject -Filter {(mail -like "*") -and (ObjectClass -eq "user")} -Server CORP:3268


Import-Module ActiveDirectory
Get-ADObject -Filter {(mail -like "*") -and (ObjectClass -eq "user")} -Server CORP:3268


Import-Module ActiveDirectory
Get-ADUser -Filter {(SamAccountName -like "*" -and (mailNickName -like "*")} `
| Set-ADUser -Replace @{extensionAttribute1='FoundAttribute1';extensionAttribute2='FoundAttribute2';}

Reference URLs:
http://technet.microsoft.com/en-us/library/ee617198.aspx
http://technet.microsoft.com/en-us/library/dd772723(WS.10).aspx

Extra:

Get-Command -Module *
Get-Command -Module CimCmdlets
Get-Command -Module ActiveDirectory

PowerShell – Active Directory – Server Core Stuffs

Configure a Server Core Server: http://technet.microsoft.com/en-us/library/jj592692.aspx
(Also, there is “SCONFIG.CMD” when you first login to a Server Core installation that can be used.)

Install a New Windows Server 2012 Active Directory forest: http://technet.microsoft.com/en-us/library/jj574166.aspx
Import-Module ActiveDirectory
Get-ADForest domain.com | Format-Table SchemaMaster,DomainNamingMaster
Get-ADDomain domain.com | Format-Table PDCEmulator,RIDMaster,InfrastructureMaster Reference: http://support.microsoft.com/kb/223346

Turning the GUI on/off with either SCONFIG or PowerShell:
http://www.howtogeek.com/111967/how-to-turn-the-gui-off-and-on-in-windows-server-2012/

PowerShell Desired State Configuration – Install Exchange Server 2013 Prerequisites

A simple DesiredStateConfiguration file that is used to install Exchange 2013 Prerequisites for both the mailbox and Client Access Server (CAS) roles on Windows Server 2013 R2. Use at your own peril!

Configuration Exchange2013PrerequisitesForServer2013MailboxRoleCASRole
{ 
       Node "localhost"
       { 
           WindowsFeature HTTPActivation 
           { 
                  Ensure    = “Present” 
                  Name      = “AS-HTTP-Activation” 
           } 

            WindowsFeature DesktopExperience 
           { 
                  Ensure    = “Present” 
                  Name      = “Desktop-Experience” 
           }

            WindowsFeature NETFramework45Features
           { 
                  Ensure    = “Present” 
                  Name      = “NET-Framework-45-Features” 
           } 

           WindowsFeature RPCoverHTTPProxy
           { 
                  Ensure    = “Present” 
                  Name      = “RPC-over-HTTP-Proxy"
           }
           WindowsFeature RSATClustering
           { 
                  Ensure    = “Present” 
                  Name      = “RSAT-Clustering"
           }
           WindowsFeature RSATClusteringCmdInterface
           { 
                  Ensure    = “Present” 
                  Name      = “RSAT-Clustering-CmdInterface"
           }
           WindowsFeature WebMgmtConsole
           { 
                  Ensure    = “Present” 
                  Name      = “Web-Mgmt-Console"
           }
           WindowsFeature WASProcessModel
           { 
                  Ensure    = “Present” 
                  Name      = “WAS-Process-Model"
           }
           WindowsFeature WebAspNet45
           { 
                  Ensure    = “Present” 
                  Name      = “Web-Asp-Net45"
           }
           WindowsFeature WebBasicAuth
           { 
                  Ensure    = “Present” 
                  Name      = “Web-Basic-Auth"
           }
           WindowsFeature WebClientAuth
           { 
                  Ensure    = “Present” 
                  Name      = “Web-Client-Auth"
           }
           WindowsFeature WebDigestAuth
           { 
                  Ensure    = “Present” 
                  Name      = “Web-Digest-Auth"
           }
           WindowsFeature WebDirBrowsing
           { 
                  Ensure    = “Present” 
                  Name      = “Web-Dir-Browsing"
           }
           WindowsFeature WebDynCompression
           { 
                  Ensure    = “Present” 
                  Name      = “Web-Dyn-Compression"
           }
           WindowsFeature WebHttpErrors
           { 
                  Ensure    = “Present” 
                  Name      = “Web-Http-Errors"
           }
           WindowsFeature WebHttpLogging
           { 
                  Ensure    = “Present” 
                  Name      = “Web-Http-Logging"
           }
           WindowsFeature WebHttpRedirect
           { 
                  Ensure    = “Present” 
                  Name      = “Web-Http-Redirect"
           }
           WindowsFeature WebHttpTracing
           { 
                  Ensure    = “Present” 
                  Name      = “Web-Http-Tracing"
           }
           WindowsFeature WebISAPIExt
           { 
                  Ensure    = “Present” 
                  Name      = “Web-ISAPI-Ext"
           }
           WindowsFeature WebISAPIFilter
           { 
                  Ensure    = “Present” 
                  Name      = “Web-ISAPI-Filter"
           }
           WindowsFeature WebLgcyMgmtConsole
           { 
                  Ensure    = “Present” 
                  Name      = “Web-Lgcy-Mgmt-Console"
           }
           WindowsFeature WebMetabase
           { 
                  Ensure    = “Present” 
                  Name      = “Web-Metabase"
           }
           WindowsFeature WebMgmtService
           { 
                  Ensure    = “Present” 
                  Name      = “Web-Mgmt-Service"
           }
           WindowsFeature WebNetExt45
           { 
                  Ensure    = “Present” 
                  Name      = “Web-Net-Ext45"
           }
           WindowsFeature WebRequestMonitor
           { 
                  Ensure    = “Present” 
                  Name      = “Web-Request-Monitor"
           }
           WindowsFeature WebServer
           { 
                  Ensure    = “Present” 
                  Name      = “Web-Server"
           }
           WindowsFeature WebStatCompression
           { 
                  Ensure    = “Present” 
                  Name      = “Web-Stat-Compression"
           }
           WindowsFeature WebStaticContent
           { 
                  Ensure    = “Present” 
                  Name      = “Web-Static-Content"
           }
           WindowsFeature WebWindowsAuth
           { 
                  Ensure    = “Present” 
                  Name      = “Web-Windows-Auth"
           }
           WindowsFeature WebWMI
           { 
                  Ensure    = “Present” 
                  Name      = “Web-WMI"
           }
           WindowsFeature WindowsIdentityFoundation
           { 
                  Ensure    = “Present” 
                  Name      = “Windows-Identity-Foundation"
           }

    } 
}

Load up the Configuration script into memory by entering:

Exchange2013PrerequesitiesForServer2013MailboxRoleCASRole

It generates a folder in your working directory and generates a .MOF file.

Run it on the Server 2012 server to install the Windows Features:

Start-DscConfiguration -Wait -Verbose -Path .\Exchange2013PrerequisitesForServer2013MailboxRoleCASRole

 

Reference full installation of Server 2013:
http://exchangeserverpro.com/install-exchange-2013-pre-requisites-windows-server-2012/
Reference for Desired State Configuration:
http://technet.microsoft.com/library/dn249918.aspx