Skip to content

Commit

Permalink
Added cmdlet Get-ComputerName (#60)
Browse files Browse the repository at this point in the history
- 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.
  • Loading branch information
johlju committed Dec 24, 2020
1 parent 91caae0 commit 4c472a3
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 0 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
29 changes: 29 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

<!-- markdownlint-disable MD013 - Line length -->
```plaintext
Get-ComputerName [<CommonParameters>]
```
<!-- markdownlint-enable MD013 - Line length -->

#### 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
Expand Down
37 changes: 37 additions & 0 deletions source/Public/Get-ComputerName.ps1
Original file line number Diff line number Diff line change
@@ -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
}
37 changes: 37 additions & 0 deletions tests/Unit/Public/Get-ComputerName.Tests.ps1
Original file line number Diff line number Diff line change
@@ -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
}
}
}
}

0 comments on commit 4c472a3

Please sign in to comment.