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 – 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 – Various Annoyances

Terminating errors vs. Non-terminating errors.
This will “fail” (a.k.a. worked as designed) and skip over the catch block because it is a non-terminating error.

try
{
Import-Module BogusModule
}
catch
{
Write-Warning "BogusModule could not be loaded."
Write-Output $Error[0]
}

This will work:

try
{
Import-Module BogusModule -ErrorAction 'Stop'
}
catch
{
Write-Warning "BogusModule could not be loaded."
Write-Output $Error[0]
}

Reference function:
http://stackoverflow.com/questions/10498433/how-to-correctly-ignore-import-module-errors-in-powershell

Annoyance:
PowerShell deciding how it will format (“rolling” and “unrolling” objects) an item on output.
An intermediary function for CSV as an example:
http://learn-powershell.net/2014/01/24/avoiding-system-object-or-similar-output-when-using-export-csv/

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

Desired State Configuration

Desired state configuration (requires PowerShell 4.0) is awesome!
Let’s you build a machine with required specs enforced.
http://blogs.msdn.com/b/powershell/archive/2013/11/01/configuration-in-a-devops-world-windows-powershell-desired-state-configuration.aspx

Demo video:
http://channel9.msdn.com/Events/TechEd/NorthAmerica/2013/MDC-B302#fbid=

Windows Server 2012 – PowerShell WebAccess

Import-Module ServerManager
Install-WindowsFeature WindowsPowerShellWebAccess
Get-Help *pswa*

#For testing only:
Install-PswaWebApplication -UseTestCertificate
Add-PswaAuthorizationRule * * *

# In reality, buy a certification and configure some security:
# Add-PswaAuthorizationRule -ComputerName SERVER-NAME-001
# Add-PswaAuthorizationRule -UserGroupName SECURITY-GROUP-001
# Add-PswaAuthorizationRule -ConfigurationName WHAT-CMDLETS-POLICY

 


Reference:
http://technet.microsoft.com/en-us/library/hh831611.aspx

Exchange – Device Related Cmdlets

Exchange 2010 – Get-ActiveSyncDevice:
http://technet.microsoft.com/en-us/library/dd335068(v=exchg.150).aspx

Exchange 2013 – Get-MobileDevice:
http://technet.microsoft.com/en-us/library/jj218706(v=exchg.150).aspx

Differences:
http://exchangeserverpro.com/mobile-device-management-cmdlets-exchange-2013/

Get-ActiveSyncDevice is deprecated for Get-MobileDevice.
A Task-Based Guide to Windows PowerShell Cmdlets:
http://technet.microsoft.com/en-us/scriptcenter/dd772285.aspx

Powershell – Disable Windows Firewall

Older netsh advfirewall set allprofiles state off is to be deprecated in favor of PowerShell.

For testing, you can disable all Windows Firewall Profiles with the following PowerShell commands:

Import-Module NetSecurity
Get-NetFirewallProfile | Set-NetFirewallProfile -Enabled False

To re-enable:

Get-NetFirewallProfile | Set-NetFirewallProfile -Enabled True

PowerShell Menu – Template .PS1 File

Hi.

Couldn’t find a batch menu demo that contained more than two layers in their example.
The template includes a main menu window, a sub-menu, and options to expand the console as deep as you want to go.

The menu and sub-menu loops until you select a valid entry from the displayed options and exit the script gracefully from the main menu.

<# # ==================================================================================================== # # Script:      PoSHMenuTemplate.ps1                                                                    #  # Version:     1.0.0                                                                                   #    # Author:      Calahan, Cory                                                                           # # Create date: 2013-07-17                                                                              # # Description: PowerShell "batch" menu template.                                                       # # Comments:    https://stealthfield.wordpress.com                                                       # # ==================================================================================================== # #>

Clear-Host
$Host.UI.RawUI.WindowTitle = "PoSH Menu Template"

function loadMainMenu()
{
    [bool]$loopMainMenu = $true
    while ($loopMainMenu)
    {
    Clear-Host  # Clear the screen.
    Write-Host -BackgroundColor Black -ForegroundColor White  “`n`tTeam Name – PowerShell Tasks – Version 1.0`t`n”
    Write-Host -BackgroundColor Black -ForegroundColor White  “`t`tMain Menu`t`t`n”
    $runasAlias = [Environment]::UserName
    Write-Host -BackgroundColor Black -ForegroundColor White "Running as: $runasAlias`n"
    Write-Host “`t`t`t1 - Select Sub-Menu 1”
    Write-Host “`t`t`t2 - Select Sub-Menu 2”
    Write-Host “`t`t`t3 - Select Sub-Menu 3”
    Write-Host “`t`t`t4 - Select Sub-Menu 4”
    Write-Host “`t`t`tQ --- Quit And Exit`n”
    Write-Host -BackgroundColor DarkCyan -ForegroundColor Yellow "`NOTICE:`t"
    Write-Host -BackgroundColor DarkCyan -ForegroundColor White  "`An extra warning.`t`n"
    $mainMenu = Read-Host “`t`tEnter Sub-Menu Option Number” # Get user's entry.
    switch ($mainMenu)
        {
        1{mainMenuOption1} # Calls function mainMenuOption1()
        2{} #To-do: Add.
        3{} #To-do: Add.
        4{} #To-do: Add.
        "q" {
                $loopMainMenu = $false
                Clear-Host
                Write-Host -BackgroundColor DarkCyan -ForegroundColor Yellow "`t`t`t`t`t"
                Write-Host -BackgroundColor DarkCyan -ForegroundColor Yellow "`tGoodbye!`t`t`t"
                Write-Host -BackgroundColor DarkCyan -ForegroundColor Yellow "`t`t`t`t`t"
                sleep -Seconds 1
                $Host.UI.RawUI.WindowTitle = "Windows PowerShell" # Set back to standard.
                Clear-Host
                Exit-PSSession
            }
        default {
            Write-Host -BackgroundColor Red -ForegroundColor White "You did not enter a valid sub-menu selection. Please enter a valid selection."
            sleep -Seconds 1
                }
        }
    }
return
}

function mainMenuOption1()
# This section is used for Loading Main Menu Option 1, .
{
    [bool]$loopSubMenu = $true
    while ($loopSubMenu)
    {
    $Host.UI.RawUI.WindowTitle = "PoSH Sub Menu 1"
    Clear-Host  # Clear the screen.
    Write-Host -BackgroundColor Black -ForegroundColor White  “`n`tTeam Name – PowerShell Tasks – Version 1.0`t`n”
    Write-Host -BackgroundColor Black -ForegroundColor White  “`t`tSub Menu 1`t`t`n”
    $runasAlias = [Environment]::UserName
    Write-Host -BackgroundColor Black -ForegroundColor White "Running as: $runasAlias`n"
    Write-Host “`t`t`t1 - Sub Menu 1 - Option 1”
    Write-Host “`t`t`t2 - Sub Menu 2 - Option 2”
    Write-Host “`t`t`t3 - Sub Menu 3 - Option 3”
    Write-Host “`t`t`tQ --- Quit And Return To Main Menu`n”
    $subMenu = Read-Host “`t`tEnter Sub-Menu 1 Option Number”
        switch ($subMenu)
        {
        1
            {
            loadSubMenu1Option1
            }
        2
            {
            #loadSubMenu2
            }
        3
            {
            #loadSubMenu3
            }
        q
            {
                $loopSubMenu = $false
            }
    default
            {
            Write-Host -BackgroundColor Red -ForegroundColor White "You did not enter a valid sub-menu selection. Please enter a valid selection."
            sleep -Seconds 1
            }
        }
    }
}

function loadSubMenu1Option1()
{
# Repeat Code Block from mainMenuOption1. Continue nesting based on features/selections you want.
}

# Start the Menu once loaded:
loadMainMenu

# Extras:
if ($clearHost) {Clear-Host}
if ($exitSession) {Exit-PSSession};