简体   繁体   中英

How Do I Determine the Distribution and Version of Linux Using PowerShell 6.x or Newer?

Using a PowerShell script in PowerShell 6.x or newer, running on an unknown Linux distribution, how do I programmatically and reliably determine the distribution and version number of the operating system?

Edit: Maybe something like this answer, plus some string-parsing?: https://unix.stackexchange.com/a/6348

Using a PowerShell script in PowerShell 6.x or newer, running on an unknown Linux distribution, how do I programmatically and reliably determine the distribution and version number of the operating system?

Edit: Maybe something like this answer, plus some string-parsing?: https://unix.stackexchange.com/a/6348

I needed to do this as well inside a powershell script. I use the following solution:

$distribution = Invoke-Expression "lsb_release -cs"

This makes $distribution contain the, for example, following value:

bionic

You can do the same for the version:

$version = Invoke-Expression "lsb_release -rs"

This makes $version contain the, for example, following value:

18.04

This should get you the distro and version number.

$release = Invoke-Expression "(lsb_release -ds || cat /etc/*release || uname -om) 2>/dev/null | head -n1"

$release will contain something like Ubuntu 18.04.5 LTS or CentOS Linux release 7.9.2009 (Core) .

If you're like me and just needed the distro, you can then use Regex to capture.

$release | grep -Po '^[^\s]+'

$release will contain something like Ubuntu or CentOS .

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