The Wiert Corner – irregular stream of stuff

Jeroen W. Pluimers on .NET, C#, Delphi, databases, and personal interests

  • My badges

  • Twitter Updates

  • My Flickr Stream

  • Pages

  • All categories

  • Enter your email address to subscribe to this blog and receive notifications of new posts by email.

    Join 1,685 other followers

Archive for the ‘PowerShell’ Category

scripting – Run Multiple Powershell Scripts Sequentially – on a Folder – Combine Scripts into a Master Script – Stack Overflow

Posted by jpluimers on 2019/07/30

Cool tip by mjolinor to execute the scripts 1.ps1, 2.ps1 and 3.ps1 from a master.ps1 script in the same directory:

&"$PSScriptroot\1.ps1"
&"$PSScriptroot\2.ps1"
&"$PSScriptroot\3.ps1"

Source: [WayBackscripting – Run Multiple Powershell Scripts Sequentially – on a Folder – Combine Scripts into a Master Script – Stack Overflow.

It uses $PSScriptroot which got introduced in PowerShell 2 in modules and extended in PowerShell 3 to be available in all scripts. More information in [WayBack] about_Automatic_Variables | Microsoft Docs

–jeroen

Posted in CommandLine, Development, PowerShell, PowerShell, Scripting, Software Development | Leave a Comment »

powershell – How do I use join-path to combine more than two strings into a file path? – Stack Overflow

Posted by jpluimers on 2019/07/25

I love the solution with piped Join-Path constructs answered by David Keaveny in [WayBackpowershell – How do I use join-path to combine more than two strings into a file path? – Stack Overflow:

Since Join-Path can be piped its path value, you can pipe multiple Join-Path statements together:

Join-Path "C:" -ChildPath "Windows" | Join-Path -ChildPath "system32" | Join-Path -ChildPath "drivers"

Of course you could replace the built-in [WayBack] Join-Path by using using the .NET Framework [WayBack] Path.Combine Method (System.IO), but then you loose code completion.

If you do like that, here is how:

[System.IO.Path]::Combine("C:", "Windows", "system32", "drivers")

–jeroen

Posted in CommandLine, Development, PowerShell, PowerShell, Scripting, Software Development | Leave a Comment »

PowerShell script header to be as strict as possible and always throw exceptions

Posted by jpluimers on 2019/07/24

This is my default PowerShell script header from now on:

Set-StrictMode -Version Latest
$ErrorActionPreference = 'Stop'
$PSDefaultParameterValues['*:ErrorAction']='Stop'

This might sound like overkill, but it solves these problems:

The above helped me tremendously with for instance [WayBack] powershell – Why does the exception not get me in the catch block? – Stack Overflow:

I’m trying to interrogate some service information. Sometimes the installer of the application fails to correctly install, so the registry does not contain a service entry. I want to find out which installer steps did get executed correctly, even on systems that do not have proper logging in the installer.

If MyService does not exist, the script below does not go to the catch block even though the exception handling documentation suggests a bare catch should be enough:

try {
    $path = 'hklm:\SYSTEM\CurrentControlSet\services\MyService'
    $key = Get-Item $path
    $namevalues = $key | Select-Object -ExpandProperty Property |
        ForEach-Object {
        [PSCustomObject] @{
            Name = $_;
            Value = $key.GetValue($_)
        }
    }
    $namevalues | Format-Table
}
catch {
    $ProgramFilesX86 = [System.Environment]::GetFolderPath("ProgramFilesX86");
    $ProgramFiles = [System.Environment]::GetFolderPath("ProgramFiles");
    Write-Host $ProgramFilesX86
    Write-Host $ProgramFiles
}

Why is that and how should I force it to end up in the catch?

This is what PowerShell outputs:

Get-Item : Cannot find path 'HKLM:\SYSTEM\CurrentControlSet\services\MyService' because it does not exist.
At C:\Users\Developer\...\GetMyServiceInfo.ps1:17 char:12
+     $key = Get-Item $path
+            ~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (HKLM:\SYSTEM\Cu...vices\MyService:String) [Get-Item], ItemNotFoundException
    + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetItemCommand

For which I edited the answer to become this:

Force the error to be terminating:

$key = Get-Item $path -ErrorAction Stop

That way it will throw and catch will get it.

Explanation and links to the official Microsoft documentation:

What is missing there is the link to the $PSDefaultParameterValues documentation at [WayBack] about_Parameters_Default_Values | Microsoft Docs

–jeroen

Posted in CommandLine, Development, PowerShell, PowerShell, Scripting, Software Development | Leave a Comment »

DTW-DanWard/PowerShell-Beautifier: A whitespace reformatter and code cleaner for Windows PowerShell and PowerShell Core

Posted by jpluimers on 2019/07/23

Interesting tool: DTW-DanWard/PowerShell-Beautifier: A whitespace reformatter and code cleaner for Windows PowerShell and PowerShell Core

–jeroen

via: [WayBack] Is there a PowerShell code formatter / pretty printer? – Stack Overflow

Posted in CommandLine, Development, PowerShell, PowerShell, Scripting, Software Development | Leave a Comment »

PowerShell: Format-Table to show all columns/members

Posted by jpluimers on 2019/07/18

I’m not even sure if I’ve posted this before, but I always forget how to show all members (or columns) using Format-Table.

It’s dead easy: -Property *

Get-ChildItem | Format-Table -Property *

Later I found out this is equivalent with the shorter version where you omit the -Property part which I wrote about in [WayBackPowerShell: when Format-Table -AutoSize displays only 10 columns and uses the width of the console when redirecting to file.

So you can shorten the above to:

Get-ChildItem | Format-Table *

It has way more columns than this:

Get-ChildItem | Format-Table

The extra members in both marked with *:

  • PSPath
  • PSParentPath
  • PSChildName
  • PSDrive
  • PSProvider
  • PSIsContainer
  • BaseName
  • Mode *
  • Name *
  • FullName
  • Parent
  • Exists
  • Root
  • Extension
  • CreationTime
  • CreationTimeUtc
  • LastAccessTime
  • LastAccessTimeUtc
  • LastWriteTime *
  • LastWriteTimeUtc
  • Attributes

The odd thing: one property fails in the -Property * table:

  • Length

I tracked this down to how -Property * works: it takes the first entry in the list. If that is not a file, then it has no Length property: [WayBackpowershell – Measure-Object : The property “length” cannot be found in the input for any objects – Stack Overflow.

Note that for a GUI version, you can replace Format-Table with Out-GridView. See

-jeroen.

Posted in CommandLine, Development, PowerShell, PowerShell, Scripting, Software Development | Leave a Comment »

 
%d bloggers like this: