PoshCode Archive  Artifact [1adb163a6b]

Artifact 1adb163a6b41421451e3f4b3e7c7bcf88d3828aa37876bc312320051fba0ab21:

  • File Set-PrimaryDnsSuffix.ps1 — part of check-in [4f4675b796] at 2018-06-10 14:13:45 on branch trunk — Sets the system primary DNS suffix by p-invoking the Win32 API. Returns true for success, false for failure. (user: Natedog size: 1232)

# 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: Natedog
# license: CC0
# function: Set-PrimaryDnsSuffix
# x-poshcode-id: 6350
# x-archived: 2016-05-20T15:23:49
# x-published: 2016-05-17T23:47:00
#
#
function Set-PrimaryDnsSuffix {
	param ([string] $Suffix)
	
	# http://msdn.microsoft.com/en-us/library/ms724224(v=vs.85).aspx
	$ComputerNamePhysicalDnsDomain = 6
	
	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($happy.com)
}