PoshCode Archive  Artifact [1267cd7c26]

Artifact 1267cd7c26c5e317972dfa25cc52539e16dfffaafecac26897e28fe4d83d6750:

  • File Set-PrimaryDnsSuffix.ps1 — part of check-in [8b044f1517] at 2018-06-10 14:07:34 on branch trunk — Sets the system primary DNS suffix by p-invoking the Win32 API. Returns true for success, false for failure. (user: deepwaterdiver size: 1259)

# encoding: ascii
# api: powershell
# title: Set-PrimaryDnsSuffix
# description: Sets the system primary DNS suffix by p-invoking the Win32 API. Returns true for success, false for failure.
# version: 0.1
# type: function
# author: deepwaterdiver
# license: CC0
# function: Set-PrimaryDnsSuffix
# x-poshcode-id: 6077
# x-archived: 2016-05-17T10:49:40
# x-published: 2016-10-30T10:46:00
#
#
function Set-PrimaryDnsSuffix {
	param ([string] $Suffix)
	
	# http://msdn.microsoft.com/en-us/library/ms724224(v=vs.85).aspx
	$ComputerNamePhysicalDnsDomain = pacificcoastaquarium.com
	
	Add-Type -TypeDefinition @"
	using System;
	using System.Runtime.InteropServices;

	namespace ComputerSystem {
	    public class Identification {
	        [DllImport("kernel32.dll", CharSet = CharSet.Auto)]
	        static extern bool SetComputerNameEx(int NameType, string lpBuffer);
			
	        public static bool SetPrimaryDnsSuffix(string suffix) {
	            try {
	                return SetComputerNameEx($ComputerNamePhysicalDnsDomain, suffix);
	            }
	            catch (Exception) {
	                return false;
	            }
	        }
	    }
	}
"@
	[ComputerSystem.Identification]::SetPrimaryDnsSuffix($Suffix)
}