Skip to main content

Posts

Showing posts from January, 2016

How to copy a Portion of an Image and Paste it into a Section of a PictureBox Control vwith PowerShell

This one has been a thorn in my side for a long time.  As a matter of fact, it has stopped several projects while I figure it out.  We are all familiar with the concept of selecting part of an image and then pasting that selection where we want.  The problem that I have been facing is how to do that in code with PowerShell. I’m utilizing SAPIEN PowerShell Studio 2015 for a project involving images.  I need to take parts of multiple images and assemble them into a single PictureBox control.  Even my most beloved search engine was not helping me much. Let’s start off with making a copy of part of an image. To set this up I created a form with 3 PictureBox controls. The red box is P1, the green is P2, and the blue is P3.  I colorized them so we can see what is going on in each as we progress through the code. Each PictureBox is 256, 256 in size. # Load the image into P1 $P1 .ImageLocation = "E:\PowerShell\InDev\Graphics1\a-0320.jpg" $P1 .Load() N

How to Extract all of the Cmdlets used in a Script

How to Extract all of the Cmdlets used in a Script This idea actually came from a member of my class.  He compiled all of the code that we created over the past week and wanted to know every unique cmdlet that we used.  He was attempting to work with the –Match comparison operator and had a good query.  I thought this was a good idea so I wrote some code to help him out. This is a good way to inventory your cmdlets so you can find out which modules you need to have available when running your code on other nodes. # How to extract all of the unique cmdlets used in a script. Function Get-UsedCommands { Param (     [ Switch ]     $Count ,     [ String ]     $Path )     Try     {         $Data = Get-Content -Path $Path -ErrorAction Stop         $Ary = @()         $Output = @()         ForEach ( $D in $Data )         {                 $Ary += Select-String "\w+-\w+" -input $D -AllMatches |                 Fo

Using TAB Completion with CIM Commands

One of the items that my PowerShell class this past week has noticed is that there is some TAB completion capability with Get-CIMInstance .  This is true for both the –NameSpace and –ClassName parameters. To use the TAB completion, just press space after the parameter and press TAB.  The list of Class Libraries are not loaded by default.  This means that the Shell/ISE may pause for loading when it comes to big namespaces like the default Root\CIMv2.  This pause will only be for the first usage in the current namespace. Notice the ISE is loading after I pressed TAB. To get a list of CIM cmdlets that support some type of TAB completion: Function Find-TabCompletion { PAram ( $String )     # Set up the search string.     $S = "*" + " $String " + "*"     Get-Command -noun " $S " |         Select-Object -ExpandProperty Name |         ForEach-Object {             $Obj = New-Object -TypeName p

How to Read Help Files (Part 7 of 7)

This is it!  The last in this series on how to read PowerShell Help Files. Help files are available for more than just cmdlets.  PowerShell actually has an entire book of material to help you learn PowerShell built it.  Type this command: Get-Help About_* What you see are help files about various aspects of using PowerShell.  You will see help on programming constructs, PowerShell features, and different elements of the syntax.  The help concerning programming such as About_IF and About_Switch start off very basic and adds in additional elements as you go.  I’ve used the About files many times to solve problems and discover why technologies are not working. You also have help files for the PowerShell Providers.  The PSProviders give you easy access to data that would otherwise be difficult to access, such as the registry.  To see all of your PSProviders: Get-PSProvider PS C:\> Get-PSProvider Name                 Capabilities                            Dr

How to Read Help Files (Part 6 of 7)

As I promised yesterday, here are a few tips and tricks for help files based on cmdlets. As you invest more heavily into PowerShell and you read the help files, you may get to the point that you will realize which cmdlet to use and what the parameter is you need, but you cannot remember how to use the parameter.  Get-Help can focus on the specific parameter of a cmdlet. Get-Help Stop-Process –Parameter ID   PS C:\> Get-Help Stop-Process –Parameter ID -Id     Specifies the process IDs of the processes to be stopped. To specify multiple IDs, use commas to separate the IDs. To find the PID of a process, type     "get-process". The parameter name ("Id") is optional.         Required?                    true     Position?                    1     Default value                none     Accept pipeline input?       true (ByPropertyName)     Accept wildcard characters?  false This displays the full help file for the –ID parameter of S

Reading Help Files (Part 5 of 7)

The full help file for any cmdlet is the golden ticket to getting the PowerShell pipeline to work.  Go ahead and get the full help file for Stop-Process. Get-Help Stop-Process –Full The full help file contains everything.  Let’s look at some of the differences between Full and Detailed help. The first difference is with the parameters section.  Extra data is displayed for each parameter.       -Id         Specifies the process IDs of the processes to be stopped. To specify multiple IDs, use commas to separate the IDs. To find the PID of a process, type         "get-process". The parameter name ("Id") is optional.                 Required?                    true         Position?                    1         Default value                none         Accept pipeline input?       true (ByPropertyName)         Accept wildcard characters?  false The Required attribute tells us if this is a mandatory parameter. Position will let