Skip to main content

Posts

Showing posts with the label MOC 10962

Find the index of an Item in an Enumeration

Well, I’m back in the warmth of Phoenix.  We had a lot of great questions from our PowerShell class in Toronto last week.  While we were discussion .NET, a question popped up about finding the index number of a value of an enumeration.  Sure, no problem. Function Get-EnumIndex { Param (     [ parameter (Mandatory= $true )]     $Enum ,         [ parameter (Mandatory= $true )]     [ String ]     $Value ) [ enum ]::GetValues( $Enum )|         foreach { [ PSCustomObject ]@{ValueName = $_ ; IntValue = [ int ] $_   }   } |         Where ValueName -eq $Value <# .SYNOPSIS Searches and enumeration for the index number of the provided value. .DESCRIPTION Searches and enumeration for the index number of the provided value. .PARAMETER Enum The enu...

What Order do you need to list Functions?

I am actually correcting myself on this one.  I’m at 35,000 feet right now enjoying a ride in the flying tin can while looking over my notes from this past weeks Advanced PowerShell class in Fort Wayne.  One of the questions that came up is the order that you need to list functions.  I gave a bad answer.  I had always been taught to declare a function before I call it.  It makes sense.  How do you know how to execute instructions before you are told what the instructions are?  Take a look at this code: Function Show-A {     Write-Host "Function A" -ForegroundColor Green } Function Show-B {     Write-Host "Function B" -ForegroundColor Cyan     Show-A } Show-B Here I am declaring function Show-A before it is called by function Show-B.  Here is the result: Function B Function A Here is my surprise.  I changed the code an placed Function Show-B1 before F...

Basic Debugging in SAPIEN PowerShell Studio 2015

I wanted to share a little debugging trick with SAPIEN PowerShell Studio 2015.  Logic bugs are not a fun.  Essentially, your script is doing exactly what you told it to do, but it is not what you wanted it to do.  Most likely, the contents of memory are not what you expected.  SAPIEN PowerShell Studio 2015 has both a Variable and a Watch window to help you in your debugging efforts.  Take a look at this code: There is nothing wrong with it.  I just want to use very basic code.  By pressing Ctrl – F5 you will run this code normally.  Pressing F5 runs the code in Debug Mode.  In Debug Mode, nothing special happens.  You have to set a break point. Above, I clicked on line 16 and pressed F9 .  This is where execution of the code will stop and allow you to examine the contents of memory.  Press F5 to see what happens next. You can see the arrow beside line 16.  This is where we currently...

Reading and Resolving PowerShell Errors - Part 10

This is part 10 of my series of the most common PowerShell errors that are made in my PowerShell classes.  This is the last one! Today’s error: My data file does not have my data. Here is our starting code: Get-Process |     Measure-Object -Property CPU -sum |     Select-Object -Property Name , CPU |     Export-CSV -Path c:\temp\data.csv And here is the full error: No error, just the wrong data.  Here is the contents of the CSV. PS C:\temp> import-csv -Path C:\temp\Data.csv Name CPU ---- --- Resolution: The problem here is that the programmer did not keep track of what is in the PowerShell Pipeline. I see this very often.  When you are constructing a pipeline, you need to keep track of what is in the pipeline.  After each statement, pipe to object to Get-Member and verify that you are still working with the object type that you expect. PS C:\temp> Get-Process | Get-M...

Reading and Resolving PowerShell Errors - Part 9

This is part 9 of my series of the most common PowerShell errors that are made in my PowerShell classes.  I will be posting one a day to help you understand why an error occurred and what the error’s meaning is. Today’s error:  Get-CimInstance : A positional parameter cannot be found that accepts argument '='. Here is our starting code: Get-CimInstance -ClassName Win32_LogicalDisk -Filter DeviceID = 'C:' And here is the full error: Get-CimInstance : A positional parameter cannot be found that accepts argument '='. At line:1 char:1 + Get-CimInstance -ClassName Win32_LogicalDisk -Filter DeviceID = 3 + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~     + CategoryInfo          : InvalidArgument: (:) [Get-CimInstance], ParameterBindingException     + FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.Management.Infrastructure.CimCmdlets.GetCimIns...

Reading and Resolving PowerShell Errors - Part 8

This is part 8 of my series of the most common PowerShell errors that are made in my PowerShell classes.  I will be posting one a day to help you understand why an error occurred and what the error’s meaning is. Today’s error: No data returned from a remote command Here is our starting code: Invoke-Command -ComputerName DC , SVR1 -ScriptBlock {         Param ( $ID , $MT )         Get-CimInstance -ClassName Win32_Logicaldisk |         Where { $_ . DeviceID -eq $ID -and $_ . MediaType -eq $MT }     } -ArgumentList 12 , "C:" And here is the full error: There is no error except nothing comes back. Resolution: Change the order of the arguments. In this example, we are passing information from the local device to the remote device.  The problem is in order of information being passed from the –ArgumentList to the PARAM...

Reading and Resolving PowerShell Errors - Part 7

This is part 7 of my series of the most common PowerShell errors that are made in my PowerShell classes.  I will be posting one a day to help you understand why an error occurred and what the error’s meaning is. Today’s error: The "Expression" key has a type, System.Int32, that is not valid Here is our starting code: Get-CimInstance -ClassName Win32_LogicalDisk |     Select-Object -Property @{         Name = "FreeSpaceGB"         Expression = $_ . FreeSpace / 1GB         } And here is the full error: Select-Object : The "Expression" key has a type, System.Int32, that is not valid; expected types are {System.String, System.Management.Automation.ScriptBlock}. At line:2 char:5 +     Select-Object -Property @{ +     ~~~~~~~~~~~~~~~~~~~~~~~~~~     + CategoryInfo ...

Reading and Resolving PowerShell Errors - Part 6

This is part 6 of my series of the most common PowerShell errors that are made in my PowerShell classes.  I will be posting one a day to help you understand why an error occurred and what the error’s meaning is. Today’s error: Table columns are empty of property values Here is our starting code: Get-EventLog -LogName Security -Newest 5 |     Select-Object -Property Name , InstanceID , Timewritten And here is the full error: Name                                                 InstanceId TimeWritten                    ----              ...