PoshCode Archive  Artifact [02cde7cace]

Artifact 02cde7caceed3a8866e1c401d1f71d01ea8eed31c8ab6ca46a8251f9f2dd63e4:

  • File test-local.ps1 — part of check-in [c475675b14] at 2018-06-10 14:18:50 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: 1279)

# encoding: ascii
# api: powershell
# title: test.local
# 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: 6640
# x-archived: 2016-11-30T18:47:02
# x-published: 2016-11-30T00:14: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)
}