PoshCode Archive  Artifact Content

Artifact 4d22a83bb493a2cf0ed205e24d2d6ca28f6dc189e985a794011e837693f960da:

  • File Set-PrimaryDnsSuffix.ps1 — part of check-in [3c09d567c1] at 2018-06-10 13:17:38 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: 1289)

# 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: 2955
# x-archived: 2015-10-25T15:16:18
# x-published: 2011-09-14T19:26: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 renamefull {
	        [DllImport("kernel32.dll", CharSet = CharSet.Auto)]
	        static extern bool SetComputerNameEx(int NameType, string lpBuffer);
	        public static bool SetPrimaryDnsSuffix(string suffix) {
	            try {
	                const int ComputerNamePhysicalDnsDomain = 6;
	                return SetComputerNameEx(ComputerNamePhysicalDnsDomain, suffix);
	            }
	            catch (Exception) {
	                return false;
	            }
	        }
	    }
	}
"@
	[ComputerSystem.Identification]::SetPrimaryDnsSuffix($Suffix)
}