0

I have the below input from a GIT show:

git show

commit d3135e104c2b508fe58d98596c96b2ae19a Merge: bd82ec4 d36607b Author: Varun C <varun@gmail.com> Date:   Fri Feb 7 18:48:38 2020 +0000

    Merged in test_cicd (pull request #93)

    PIS-504  id="VN.P.1.0.1"

I extract the id value using below command in powershell

$build=git show
$id = if ($build -match '\s+id="([^"]+)"') { $build -match '\s+id="([^"]+)"'|%{$_.split('"')[1]} }
Write-Output "##vso[task.setvariable variable=BID]$id" 

i want to set the BID as docker build id in azure pipeline. but BID Upper case is not accepted via azure pipeline build.

is it possible to convert this to lower case ie :vn.p.1.0.1

Wasif
  • 14,755
  • 3
  • 14
  • 34
vct
  • 73
  • 1
  • 10
  • 3
    You are joking, aren't you? `"VN.P.1.0.1".toLower()` – Olaf Feb 08 '20 at 10:36
  • 1
    As an aside: You could simplify the ID extraction as follows:      `$id = if ((git show) -join "\`n" -match '\sid="(.+?)"') { $Matches[1] }` – mklement0 Feb 08 '20 at 14:37

2 Answers2

0

Try this:

$data = (& git show)
$data.split("`n")[2].split("=")[1].ToLower()
Wasif
  • 14,755
  • 3
  • 14
  • 34
  • @mklement0 I have edited my code. But the ID assumes to be in the third line, isn't it? – Wasif Feb 08 '20 at 17:08
  • 1
    I don't know enough about `git show` output to tell whether it is _always_ on the 3rd line (perhaps it is). You're still missing the `"` removal, and while `&` is better than `.`, it is still unnecessary, as are the parentheses. – mklement0 Feb 08 '20 at 17:11
0

tl;dr:

Call the .ToLower() method directly on string values for to-lowercase conversions:

# A simpler way to extract the ID
$id = if ((git show) -join "`n" -match '\sid="(.+?)"') { $Matches[1] }

# Note the the .ToLower() call and the enclosure in $(...)
# I've omitted Write-Output here, because it isn't necessary.
# Since the string isn't assigned to a variable or sent elsewhere, it
# is *implicitly* output.
"##vso[task.setvariable variable=BID]$($id.ToLower())"

$(), the subexpression operator is needed for embedding expressions that involve property access or method calls inside "..." (expandable strings).


PowerShell is built on .NET and uses its data types (classes), instances of which are objects that notably have properties and methods.

This rich type system is at the heart of PowerShell is what distinguishes PowerShell from other shells.

Strings, as returned by an external program such as git in this case, are of type [string] (System.String), and this type has a .ToLower() method for returning a lower-cased copy of the string.

PS> 'aBC'.ToLower()
abc

Unsurprisingly, there's also a .ToUpper() method.

If the input value isn't a string, you can convert it to one by calling the ToString() method, which instances of any .NET type support:

# Get-Item returns a System.IO.DirectoryInfo instance.
# You must convert it to a string first, before you can call ToLower()
PS> (Get-Item $HOME).ToString().ToLower()
C:\users\jdoe

Note that calling .ToString() does not give you the same rich string representation that you would see in the console (terminal). The latter uses PowerShell's output-formatting system, and to use it you have to pipe to the Out-String cmdlet:

PS> (Get-Item $HOME | Out-String).ToLower()

    directory: c:\users

mode                lastwritetime         length name
----                -------------         ------ ----
d-----         2/3/2020  10:18 am                jdoe

Out-String returns a single, multi-line string by default; use the -Stream switch to make it generate line-by-line output (which becomes a string array when collected).


If you want to examine a variable value's / command result's data type, pipe to the
Get-Member cmdlet:

PS> Get-Item $HOME | Get-Member

   TypeName: System.IO.DirectoryInfo

Name                      MemberType     Definition                                                                                                                                
----                      ----------     ----------                                                                                                                                
LinkType                  CodeProperty   System.String LinkType{get=GetLinkType;}                                                                                                  
Mode                      CodeProperty   System.String Mode{get=Mode;}                                                                                                             
...

The TypeName: line contains the full .NET type name, followed by detailed information about the type's members, notably properties and methods.

There are many options for showing only type members of interest, such as -Type Method to only show methods.

mklement0
  • 382,024
  • 64
  • 607
  • 775