Skip to content

Instantly share code, notes, and snippets.

@AspenForester
Created June 11, 2018 19:29
Show Gist options
  • Save AspenForester/d2ea7faf18fb939ff46886760a0e66e3 to your computer and use it in GitHub Desktop.
Save AspenForester/d2ea7faf18fb939ff46886760a0e66e3 to your computer and use it in GitHub Desktop.
function Register-DNSVMGuest
{
#Requires -modules VMware.VimAutomation.Core,DNSClient
[CmdletBinding()]
param (
# Parameter help description
[Parameter(Mandatory = $true)]
[ValidateNotNullorEmpty]
[String]
$VIServer,
# Credential
[ValidateNotNull()]
[System.Management.Automation.PSCredential]
[System.Management.Automation.Credential()]
$Credential = [System.Management.Automation.PSCredential]::Empty,
# Parameter help description
[Parameter()]
[String[]]
$ComputerName
)
begin
{
function Compare-Things
{
[CmdletBinding()]
[OutputType([bool])]
Param
(
# The singular thing that want to see if it matches
[Parameter(Mandatory = $true,
ValueFromPipelineByPropertyName = $true,
Position = 0)]
[String]
$ThingA,
# Any of these things
[String[]]
$Things
)
Process
{
foreach ($thing in $Things)
{
if ($ThingA -like $thing)
{
return $true
}
}
return $False
}
}
$VISess = Connect-VIServer -Server $VIServer
}
process
{
foreach ($Computer in $ComputerName)
{
try
{
$VM = Get-VM -Name $Computer -Server $VISess -ErrorAction Stop
$ip = $($vm.Guest.IPAddress)[0]
# Might want to see if trusted hosts is going to allow this
$TrustedHosts = (get-item WSMan:\localhost\Client\TrustedHosts).value -split (',')
if (Compare-Things -ThingA $ip -Things $TrustedHosts)
{
Try
{
$session = New-CimSession -ComputerName $ip -Credential $prodcred -ErrorAction Stop
Register-DnsClient -CimSession $session
$session | Remove-CimSession
}
catch
{
Write-Error "Unable to connect to $Computer via WSMAN"
}
}
else
{
Write-Error "$ip doesn't match any of the WinRM/WSMAN trusted host filters. You won't be able to connect"
}
}
catch
{
Write-Error "Unable to find $Computername hosted with $Viserver"
}
}
}
end
{
$VISess | Disconnect-VIServer -Force -Confirm:$False -ErrorAction SilentlyContinue
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment