From 4c472a379e82a284ce5599238cdae62a5f760dd1 Mon Sep 17 00:00:00 2001 From: Johan Ljunggren Date: Thu, 24 Dec 2020 20:01:15 +0100 Subject: [PATCH] Added cmdlet Get-ComputerName (#60) - Added cmdlet `Get-ComputerName` which can be used to returns the computer name cross-plattform. The variable `$env:COMPUTERNAME` does not exist cross-platform which hinders development and testing on macOS and Linux. Instead this cmdlet can be used to get the computer name cross-plattform. --- CHANGELOG.md | 7 ++++ README.md | 29 +++++++++++++++ source/Public/Get-ComputerName.ps1 | 37 ++++++++++++++++++++ tests/Unit/Public/Get-ComputerName.Tests.ps1 | 37 ++++++++++++++++++++ 4 files changed, 110 insertions(+) create mode 100644 source/Public/Get-ComputerName.ps1 create mode 100644 tests/Unit/Public/Get-ComputerName.Tests.ps1 diff --git a/CHANGELOG.md b/CHANGELOG.md index e171622..536f8fc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Added + +- Added cmdlet `Get-ComputerName` which can be used to returns the computer + name cross-plattform. The variable `$env:COMPUTERNAME` does not exist + cross-platform which hinders development and testing on macOS and Linux. + Instead this cmdlet can be used to get the computer name cross-plattform. + ## [0.10.0] - 2020-11-18 ### Added diff --git a/README.md b/README.md index 96be143..6a5822d 100644 --- a/README.md +++ b/README.md @@ -455,6 +455,35 @@ ConvertTo-HashTable -CimInstance $cimInstance This creates a array om CimInstances of the class name MSFT_KeyValuePair and passes it to ConvertTo-HashTable which returns a hashtable. +### `Get-ComputerName` + +Returns the computer name cross-plattform. The variable `$env:COMPUTERNAME` +does not exist cross-platform which hinders development and testing on +macOS and Linux. Instead this cmdlet can be used to get the computer name +cross-plattform. + +#### Syntax + + +```plaintext +Get-ComputerName [] +``` + + +#### Outputs + +**System.String** + +#### Notes + +None. + +#### Example + +```powershell +$computerName = Get-ComputerName +``` + ### `Get-LocalizedData` Gets language-specific data into scripts and functions based on the UI culture diff --git a/source/Public/Get-ComputerName.ps1 b/source/Public/Get-ComputerName.ps1 new file mode 100644 index 0000000..343a84e --- /dev/null +++ b/source/Public/Get-ComputerName.ps1 @@ -0,0 +1,37 @@ +<# + .SYNOPSIS + Returns the computer name cross-plattform. + + .DESCRIPTION + The variable `$env:COMPUTERNAME` does not exist cross-platform which + hinders development and testing on macOS and Linux. Instead this cmdlet + can be used to get the computer name cross-plattform. + + .EXAMPLE + Get-ComputerName + + This example returns the computer name cross-plattform. +#> +function Get-ComputerName +{ + [CmdletBinding()] + [OutputType([System.String])] + param () + + $computerName = $null + + if ($IsLinux -or $IsMacOs) + { + $computerName = hostname + } + else + { + <# + We could run 'hostname' on Windows too, but $env:COMPUTERNAME + is more widely used. + #> + $computerName = $env:COMPUTERNAME + } + + return $computerName +} diff --git a/tests/Unit/Public/Get-ComputerName.Tests.ps1 b/tests/Unit/Public/Get-ComputerName.Tests.ps1 new file mode 100644 index 0000000..bdf641f --- /dev/null +++ b/tests/Unit/Public/Get-ComputerName.Tests.ps1 @@ -0,0 +1,37 @@ +$ProjectPath = "$PSScriptRoot\..\..\.." | Convert-Path +$ProjectName = ((Get-ChildItem -Path $ProjectPath\*\*.psd1).Where{ + ($_.Directory.Name -match 'source|src' -or $_.Directory.Name -eq $_.BaseName) -and + $(try { Test-ModuleManifest -Path $_.FullName -ErrorAction Stop } catch { $false } ) + }).BaseName + + +Import-Module $ProjectName + +InModuleScope $ProjectName { + Describe 'Get-ComputerName' { + BeforeAll { + $mockComputerName = 'MyComputer' + + if ($IsLinux -or $IsMacOs) + { + function hostname + { + } + + Mock -CommandName 'hostname' -MockWith { + return $mockComputerName + } + } + else + { + $mockComputerName = $env:COMPUTERNAME + } + } + + Context 'When getting computer name' { + It 'Should return the correct computer name' { + Get-ComputerName | Should -Be $mockComputerName + } + } + } +}