Skip to content

Commit

Permalink
added win-hostname plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
ciiqr committed Oct 16, 2023
1 parent adf7076 commit 5c969eb
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 0 deletions.
11 changes: 11 additions & 0 deletions win-hostname/plugin.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
name: win-hostname
when: os == "windows"

provision:
when: declaration == "hostname"

executable: win-hostname.ps1

schema:
$schema: https://json-schema.org/draft/2020-12/schema
type: string
55 changes: 55 additions & 0 deletions win-hostname/win-hostname.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
Param (
[Parameter(Mandatory)]
[ValidateSet("provision")]
[string]$command,

[Parameter(Mandatory)]
[string]$info_json,

[Parameter(Mandatory, ValueFromPipeline)]
[string]$stdin
)

function hostname_provision_computer_name() {
Param (
[Parameter(Mandatory)]
[string]$hostname
)

$result = @{
status = "success"
changed = $false
description = "hostname ${hostname}"
output = ""
}

if ($env:ComputerName -ne $hostname) {
$result.changed = $true
$result.output = (Rename-Computer -NewName $hostname -Force) -join "`n"
if (!$?) {
$result.status = "failed"
return $result
}
}

return $result
}

function hostname_provision() {
$states = ConvertFrom-Json $stdin

# provision hostname
foreach ($state in $states) {
hostname_provision_computer_name $state.state | ConvertTo-Json -Compress
}
}

switch ($command) {
"provision" {
hostname_provision
}
default {
Write-Output "hostname: unrecognized subcommand: ${command}"
exit 1
}
}

0 comments on commit 5c969eb

Please sign in to comment.