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

Linux Adventures With Debian

1 – Install your distro, I am using Debian NetInst and then adding in some XFCE. A nice Terminal command for some more info:

uname -a && cat /etc/*release

2 – Patch!:

apt-get update && apt-get upgrade -y

3 – Customize your Terminal (bash) prompt:

a. Yours:

/home/<username>/.bashrc

(Hidden Path) a.k.a.

~/.bashrc

b. The “root” account’s is located here:

/root/.bashrc

I used this: http://ezprompt.net/

4. Install some themes:

/usr/share/themes

5. More visuals: conky :

apt-get install conky-all

a. Custom conky files –

Your conky preferences are stored here:

/home/<username>/.conkyrc

Additional add-on files/scripts can be placed here (?):

/home/<username>/.conky

6. Adding/editing repos:

/etc/apt/sources.list

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

PowerShell – Fun With Loading $Profile

If you run $profile, you will get a path returned similar to the following:
C:\Users\<USERNAME>\Documents\WindowsPowerShell\Microsoft.PowerShellISE_profile.ps1

If you have a .ps1 with the file name, you can pre-load or execute scripts upon launching PowerShell.

For example, check out the following. PowerShell can greet you based on the time of day:

Add-Type -AssemblyName System.Speech
$name = [Environment]::UserName
$theTime = (Get-Date).Hour

if ($theTime -le 12)
   {
       [string]$theGreeting = "Good morning "
   }
if (($theTime -ge 12) -and ($theTime -le 18))
   {
     [string] $theGreeting = "Good afternoon "
   }
if (($theTime -ge 19) -and ($theTime -le 24))
   {
      [string]$theGreeting = "Good evening "
   }

$theVoice = New-Object -TypeName System.Speech.Synthesis.SpeechSynthesizer 
$theVoice.Speak("$theGreeting $name")

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

PowerShell Looping

function HateOnProcesses()
{
 $A = Get-Service
 foreach ($B in $A)
 {
  $C = $B.DisplayName 
  $D = "I hate you $C" + " so much!" 
  Write-Host $D
 }
}

Then, call:

HateOnProcesses

 

 

You can assign whatever you want to the variables for your own clarity. Example:

function HateOnProcesses2()
{
 $gs = Get-Service
 foreach ($name in $gs)
  {
  $service = $name.DisplayName
  $hatemessage = "I hate you $service" + " so much!"
  Write-Host $hatemessage
  }
}

Then, call:

HateOnProcesses2

HardwareQuery.vbs

Couldn’t find the original post/author! Will edit link once I find it again:

Copy/save the below with a *.VBS extension:

Dim strComputer '. is the local PC
 Dim WshShell 'the windows script shell
 Dim strQuery 'the WMI Query to run
 Dim colCompSys 'the win32_ComputerSystem collection
 DIM PCModel 'the trimmed string with the model name
 DIM strSerialNum
 Dim colSMBIOS
 strComputer = InputBox("Please enter a computer name," & vbcrlf & "or a '.' for localhost")
Set WshShell = WScript.CreateObject("WScript.Shell")
 strQuery="SELECT * from Win32_ComputerSystem"
 Set colCompSys=GetObject("WinMgmts://" & strComputer & "/root/cimv2").ExecQuery(strQuery)
For Each WMIProperty in colCompSys
 PCModel = TRIM(WMIProperty.Model)
 PCMake = TRIM(WMIProperty.Manufacturer)
 Next
strQuery="Select * from Win32_SystemEnclosure"
 Set colSMBIOS=GetObject("WinMgmts://" & strComputer & "/root/cimv2").ExecQuery (strQuery)
For Each objSMBIOS in colSMBIOS
 strSerialNum = objSMBIOS.SerialNumber
 Next
WScript.Echo "PC Details: " & VBCrLf & PCMake & VBCrLf & PCModel & VBCrLf & strSerialNum
WScript.Quit

PowerShell – Writing Your Own Scripts

Time to start learning PowerShell. By default, PowerShell disables running scripts. You will need to change the execution policy to run your own scripts in your test environment.

1 – Open PowerShell

2 – You can check the Policy currently applied with the following command:

Get-ExecutionPolicy

By Default, it should return: Restricted

Other Types that could be set are: AllSigned, RemoteSigned, Unrestricted, Bypass

3 – To update the Policy to run anything from anywhere, run the following from an Elevated Command Prompt:

Set-ExecutionPolicy Unrestricted

4 – Acknowledge the security change with “Y”

 

Copy Information To The Clipboard:

Run a command with “| clip” appended on the end.
Example: Get-Help Get-Help | clip

 

View Help Documentation Online:

Run the Command with “-online” appended on the end.
Examples: Get-Help Get-Help -Online
Get-Help Set-ExecutionPolicy -Online

Understanding The OSI Model

[Upper layers]
7. Application:
Provides interface between software applications and network for interpreting applications’ requests and requirements.
6. Presentation:
Allows hosts and applications to use a common language; performs data formatting, encryption, and compression. a.k.a. “syntax layer”.
5. Session:
Establishes, maintains, and terminates user connections.
[Lower layers]
4. Transport:
Ensures accurate delivery of data through flow control, sementation and reassembly, error correction and acknowledgement. (TCP)
3. Network:
Establishes network connections; translates network address into their physical counterparts and determines routing through logical addressing (IP addresses).
2. Data link:
Packages data into frames appropriate to network transmission method.
2a. Logical Link Control (LLC) Sublayer:
Addressing and channel access control mechanisms
2b. Media Access Control (MAC) Sublayer:
Node-to-node error control.
1. Physical:
Manages signalling to and from physical network connections.

Examples of devices/services:
Physical – Ethernet, Token Ring. A hub or repeater works here.
Data Link – Ethernet, ATM. A switch or bridge works here.
Network – ARP, RIP, IPv4, IPv6. A router works here.
Transport – TCP, UDP, SPX
Session – PPTP, H.245, NetBIOS, AppleTalk, WinSock
Presentation – JPEG, GIF, MPEG
Application – DHCP, DNS, FTP, HTTP, LDAP, SSH, SIP, SMTP

References –
http://support.microsoft.com/kb/103884
http://en.wikipedia.org/wiki/OSI_model
http://compnetworking.about.com/od/basicnetworkingconcepts/l/blbasics_osimod.htm