简体   繁体   中英

Extracting desired field in powershell from a command output

I am in the process of writing a powershell script, as part of which, I need to find out the flavour of Linux of the target machine, and based on which flavour I get, I need to execute command subsequently.

To know the linux type, I have executed the command:

cat /etc/os-release

An example output of which looks like:

NAME="Red Hat Enterprise Linux Server" VERSION="7.6 (Maipo)" ID="rhel" ID_LIKE="fedora" VARIANT="Server" VARIANT_ID="server" VERSION_I D="7.6" PRETTY_NAME="Red Hat Enterprise Linux Server 7.6 (Maipo)" ANSI_COLOR="0;31" CPE_NAME="cpe:/o:redhat:enterprise_linux:7.6:GA:se rver" HOME_URL="https://www.redhat.com/" BUG_REPORT_URL="https://bugzilla.redhat.com/" REDHAT_BUGZILLA_PRODUCT="Red Hat Enterprise Li nux 7" REDHAT_BUGZILLA_PRODUCT_VERSION=7.6 REDHAT_SUPPORT_PRODUCT="Red Hat Enterprise Linux" REDHAT_SUPPORT_PRODUCT_VERSION="7.6"

I need to get the NAME="Red Hat Enterprise Linux Server" or more specifically Red Hat Enterprise Linux Server portion of the output. Similarly the ouput might be Debian or CentOS or SUSE in the NAME= portion.

I tried the following if $output is the whole of the above output:

  1. $output.NAME
  2. $abc = -split $Output; Write-Host $abc[0] + $abc[1]
  3. findstr
  4. Select-String and a couple of more things

But none of them work. Can someone help?

Regex one way to do this, another way is to use ConvertFrom-String which has the option to supply a template. Eg.

$Template = 'NAME="{Name:Red Hat Enterprise Linux Server}" VERSION="7.6 (Maipo)" ID="rhel" ID_LIKE="fedora" VARIANT="Server" VARIANT_ID="server" VERSION_ID="7.6" PRETTY_NAME="Red Hat Enterprise Linux Server 7.6 (Maipo)" ANSI_COLOR="0;31" CPE_NAME="cpe:/o:redhat:enterprise_linux:7.6:GA:server" HOME_URL="https://www.redhat.com/" BUG_REPORT_URL="https://bugzilla.redhat.com/"  REDHAT_BUGZILLA_PRODUCT="Red Hat Enterprise Linux 7" REDHAT_BUGZILLA_PRODUCT_VERSION=7.6 REDHAT_SUPPORT_PRODUCT="Red Hat Eterprise Linux" REDHAT_SUPPORT_PRODUCT_VERSION="7.6"'

$TestData = 'NAME="Red Hat Enterprise Linux Server" VERSION="7.6 (Maipo)" ID="rhel" ID_LIKE="fedora" VARIANT="Server" VARIANT_ID="server" VERSION_ID="7.6" PRETTY_NAME="Red Hat Enterprise Linux Server 7.6 (Maipo)" ANSI_COLOR="0;31" CPE_NAME="cpe:/o:redhat:enterprise_linux:7.6:GA:server" HOME_URL="https://www.redhat.com/" BUG_REPORT_URL="https://bugzilla.redhat.com/"  REDHAT_BUGZILLA_PRODUCT="Red Hat Enterprise Linux 7" REDHAT_BUGZILLA_PRODUCT_VERSION=7.6 REDHAT_SUPPORT_PRODUCT="Red Hat Eterprise Linux" REDHAT_SUPPORT_PRODUCT_VERSION="7.6"'
$TestData2 = 'NAME="Ubuntu" VERSION="20.04 (Maipo)" ID="debian" ID_LIKE="fedora" VARIANT="Server" VARIANT_ID="server" VERSION_ID="7.6" PRETTY_NAME="Red Hat Enterprise Linux Server 7.6 (Maipo)" ANSI_COLOR="0;31" CPE_NAME="cpe:/o:redhat:enterprise_linux:7.6:GA:server" HOME_URL="https://www.redhat.com/" BUG_REPORT_URL="https://bugzilla.redhat.com/"  REDHAT_BUGZILLA_PRODUCT="Red Hat Enterprise Linux 7" REDHAT_BUGZILLA_PRODUCT_VERSION=7.6 REDHAT_SUPPORT_PRODUCT="Red Hat Eterprise Linux" REDHAT_SUPPORT_PRODUCT_VERSION="7.6"'
$TestData3 = 'NAME="Centos" VERSION="7.654 (Maipo)" ID="rhel" ID_LIKE="fedora" VARIANT="Server" VARIANT_ID="server" VERSION_ID="7.6" PRETTY_NAME="Red Hat Enterprise Linux Server 7.6 (Maipo)" ANSI_COLOR="0;31" CPE_NAME="cpe:/o:redhat:enterprise_linux:7.6:GA:server" HOME_URL="https://www.redhat.com/" BUG_REPORT_URL="https://bugzilla.redhat.com/"  REDHAT_BUGZILLA_PRODUCT="Red Hat Enterprise Linux 7" REDHAT_BUGZILLA_PRODUCT_VERSION=7.6 REDHAT_SUPPORT_PRODUCT="Red Hat Eterprise Linux" REDHAT_SUPPORT_PRODUCT_VERSION="7.6"'
$TestData4 = 'NAME="Arch Linux" VERSION="24.6 (Maipo)" ID="rhel" ID_LIKE="fedora" VARIANT="Server" VARIANT_ID="server" VERSION_ID="7.6" PRETTY_NAME="Red Hat Enterprise Linux Server 7.6 (Maipo)" ANSI_COLOR="0;31" CPE_NAME="cpe:/o:redhat:enterprise_linux:7.6:GA:server" HOME_URL="https://www.redhat.com/" BUG_REPORT_URL="https://bugzilla.redhat.com/"  REDHAT_BUGZILLA_PRODUCT="Red Hat Enterprise Linux 7" REDHAT_BUGZILLA_PRODUCT_VERSION=7.6 REDHAT_SUPPORT_PRODUCT="Red Hat Eterprise Linux" REDHAT_SUPPORT_PRODUCT_VERSION="7.6"'
$TestData,$TestData2,$TestData3,$TestData4 | 
    ForEach-Object { 
        $_ | 
        ConvertFrom-String -TemplateContent $Template
    }

Output:

Name
----
Red Hat Enterprise Linux Server
Ubuntu
Centos
Arch Linux

EDIT: Here's a regex variant:

$Regex = '.*="(.*)"\sVERSION='
$TestData,$TestData2,$TestData3,$TestData4 |
    ForEach-Object {
        [void]($_ -match $Regex)
        $Matches.1
    }

Output:

Red Hat Enterprise Linux Server
Ubuntu
Centos
Arch Linux

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM