PoshCode Archive  Artifact [2332374bce]

Artifact 2332374bce427237c0825b2faf46f39136c7984b4f8a827ba820b9a97df920a0:

  • File Set-PrimaryDnsSuffix.ps1 — part of check-in [ffb5041480] at 2018-06-10 13:53:55 on branch trunk — Sets the system primary DNS suffix by p-invoking the Win32 API. Returns true for success, false for failure. (user: Andy Arismendi size: 1236)

# 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: Andy Arismendi
# license: CC0
# function: Set-PrimaryDnsSuffix
# x-poshcode-id: 5358
# x-archived: 2016-06-29T10:20:15
# x-published: 2016-08-07T13:18: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($Suffix)
}