Some pieces of code which seems right... but which will definitively fail in some situation. PowerShell is not VBS !

Provide honest answers. The goal of this test is that you can detect and evaluate your own doubts.

 


Powershell  1. This function, CountProcesses, counts how many executing processes, filtering by a particular name.  "CMD", for example.

 

  Function CountProcesses ([string]$Name)

  {

    return (Get-Process -Name $Name).Count

  }

 

Is it correct ?  
  Always      Never       It depends on PowerShell version

Is it a good implementation ?
  SI         NO, it could sometimes work... but could also fail.

Answer :

 

In PowerShell v2 this will fail in many occasions.

In v3 it seems to work, but with a performance and clarity penalty. The syntax is wrong. 

 


Powershell   2. These 2 lines look for a specific domain name ("domain.com") inside the Netlogon.Log file

 

  $File = Get-Content NETLOGON.LOG

  $File | Select-String "domain.com"

 

Is it correct ?  
  Always      Never       It depends on PowerShell version

Hint  What could happen if the file has 1 TB ? 

Answer :

 

The syntax is WRONG. It will sometimes return bad results.

For example, a domain called DomainSCOM.NET will also be found. Try it.

Besides, if it is a large file, it will run out of memory. There are better ways !

 


Powershell   3. This function (UserExists) uses the .Count property to decide if a particular user is present in Active Directory.

 

  Function UserExists ($Name)

  {

    return ((Get-AdUser -Identity $Name).Count -eq 1)

  }

 

Is it correct ?  
  Always        Never                It depends on PowerShell version
   
No matter which PowerShell version you use, it will fail. Why ?

Answer :
Try this. It does not work this way, even with v4. The .Count property is evil ! Wrong syntax, man.

Powershell   4. This simple function (LogFiles) should display all *.LOG files present in the Windows folder.

 

  Function LogFiles

  {

    Get-ChildItem -Include *.LOG -Path $env:windir

  }

 

Is it correct ?  
  Always        Never                 It depends on PowerShell version
   
No matter which PowerShell version you use, it will fail. Why ?

Answer :
Try this... this does not work this way.

Powershell   5. This function displays Windows services, ordered by status.

       The order is important. We'd like to get first the "Stopped" ones, and the "Running" ones at the end. So we force Sort-Object to be -DESCENDING

 

  Function DisplayServices

  {

    Get-Service | Sort-Object -Property Status -Descending

  }

 

Is it correct ?  
  Always        Never                 It depends on PowerShell version
   

Strangely enough, we get the wrong order.

We get first "Running" services, and then "Stopped". This is exactly the opposite as expected by -Descending.

Why ?

Answer : TBD

Powershell    6.  Can I define variables with these names ?

      

 

  $Cañón = 3

 

  $This Variable Contains Spaces = 5

  

Is it correct ?  
  Always         Never                It depends on PowerShell version
Answer :
The first variable is perfectly right...
The variable with spaces could be right, but requires a slightly different syntax...

 


Powershell    7.  Can I start my profile with this line ?

      

 

  $TestNumbers = 1..10000000

 

   

Is it correct ?  
  Always        Never                It depends on PowerShell version
Answer :
Of course you can...

 But how much memory are you going to waste ? 



Powershell    8.  I'm writing a loop for each month of the year, in French. However, I'm getting 13 months per year !!! Thirteen ! Why ?

 

  $Region = "fr-FR"

 

  $Months = [globalization.cultureinfo]::CreateSpecificCulture($Region).DateTimeFormat.MonthNames

 

  foreach ($Month in $Months) { Write-Host $Month }  

 

 $Months.Count    # 13

   

Now I'm trying this with my own culture ("en-US", "uk-GB" ... or any other). Again, 13

Is it correct ?  
  Always         Never                 It depends on PowerShell version
Answer :

  This loop is wrong. You cannot loop this way, as Windows defines 13...

  Yes, 13 (and it is not an error).

  Using Write-Host you get an extra empty line...

  In a real example, like processing files, you'll get a problem with the 13th month name...


 


(TBD - Adding more examples soon)



Get-JC © December 2015
All rights reserved.
 
Get-PowerShell | Use-PowerShell -Force