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

When Powershell function won’t work: you define them with commas and parentheses, but call them with spaces and no parentheses

Posted by jpluimers on 2019/08/15

The function or command was called as if it were a method. 
Parameters should be separated by spaces. For information about 
parameters, see the about_Parameters Help topic.

Every now and then I bump into the above error. The reason is this:

  1. Functions are defined with commas between parameters and parentheses around them
  2. One-parameter functions can be called with one parameter surrounded by parentheses
  3. Multi-parameter functions need to be called with spaces between parameters and no parentheses surrounding them

Confused? #MeToo

The problem: [WayBackabout_Parameters_Default_Values | Microsoft Docs

Based on [WayBack] Powershell function won’t work.

–jeroen

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

PowerShell: be careful using `-ReadCount` on `Get-Content`

Posted by jpluimers on 2019/08/14

I learned this the hard way: [WayBackDifferent result when using -ReadCount with Get-Content: because -ReadCount delivers data in chunks, the filter after [WayBack] Get-Content (Microsoft.PowerShell.Management) it will only filter on those chunks. If the filter isn’t prepared for that, it might only filter the last chunk.

So do not use for instance [WayBack] Select-String (Microsoft.PowerShell.Utility) on it, but perform your own [WayBack] ForEach-Object (Microsoft.PowerShell.Core) aliased as foreach like in [WayBack] Get all lines containing a string in a huge text file – as fast as possible?:

Get-Content myfile.txt -ReadCount 1000 |
  foreach { $_ -match "my_string" }

A more elaborate example is at [WayBack] How can I make this PowerShell script parse large files faster?.

–jeroen

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

Powershell: what kind of data type is [string[]] and when would you use it?

Posted by jpluimers on 2019/08/13

[WayBackIn Powershell, what kind of data type is [string[]] and when would you use it? (thanks cignul9 and arco444!): basically it forces an array of string.

It defines an array of strings. Consider the following ways of initialising an array:

[PS] > [string[]]$s1 = "foo","bar","one","two",3,4
[PS] > $s2 = "foo","bar","one","two",3,4

[PS] > $s1.gettype()

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     True     String[]                                 System.Array

[PS] > $s2.gettype()

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     True     Object[]                                 System.Array

By default, a powershell array is an array of objects that it will cast to a particular type if necessary. Look at how it’s decided what types the 5th element of each of these are:

[PS] > $s1[4].gettype()

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     True     String                                   System.Object


[PS] > $s2[4].gettype()

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     True     Int32                                    System.ValueType


[PS] > $s1[4]
3
[PS] > $s2[4]
3

The use of [string[]] when creating $s1 has meant that a raw 3 passed to the array has been converted to a String type in contrast to an Int32 when stored in an Object array.

–jeroen

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

.NET and PowerShell: Getting proper version info from a PE file like EXE, DLL, assembly

Posted by jpluimers on 2019/08/01

I’ve learned the hard way that both .NET and PowerShell version information isn’t always accurate or usable for two reasons which I later found in various other blog and forum posts:

The easiest is to use these numbers to create a [WayBack] Version Class (System) instance using the [WayBack] Version Constructor (Int32, Int32, Int32, Int32) constructor. This has the added benefit that you directly compare versions with each other.

Sometimes it makes even sense to take the highest version from Product and File.

In PowerShell, this is the way to do that, assuming $imagePath points to a [WayBack] Portable Executable:

try {
  $VersionInfo = (Get-Item $imagePath).VersionInfo
  $FileVersion = [version]("{0}.{1}.{2}.{3}" -f $VersionInfo.FileMajorPart, $VersionInfo.FileMinorPart, $VersionInfo.FileBuildPart, $VersionInfo.FilePrivatePart)
  $ProductVersion = [version]("{0}.{1}.{2}.{3}" -f $VersionInfo.ProductMajorPart, $VersionInfo.ProductMinorPart, $VersionInfo.ProductBuildPart, $VersionInfo.ProductPrivatePart)
  $ActualVersion = $(if ($ProductVersion -gt $FileVersion) { $ProductVersion } else { $FileVersion })
}
catch {
  $ActualVersion = [version]("0.0.0.0")
}

Background information:

–jeroen

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

Ternary operator in PowerShell – Stack Overflow

Posted by jpluimers on 2019/07/31

I like this built-in construct by fbehrens most:

$result = If ($condition) {"true"} Else {"false"}

Everything else is incidental complexity and thus to be avoided.

For use in or as an expression, not just an assignment, wrap it in $(), thus:

write-host $(If ($condition) {"true"} Else {"false"})

There are even more elegant constructs, but those require setting up an alias before using them.

Source: [WayBackTernary operator in PowerShell – Stack Overflow

–jeroen

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

 
%d bloggers like this: