I am creating some tools for active directory management and have run into an issue where IP Addresses are not being converted to hostnames when searching AD through powershell. As a workaround I created this Resolve-Hostname function and if I run it by itself it works just fine. As soon as I put it in my overall script and try calling upon it, it does not convert the IP to a Hostname...
Below is the function I created
function Resolve-Hostname {
If ($Workstation -like "*.*.*.*") #Convert IP Address to Hostname
{
$NameHost = resolve-dnsname $Workstation | Select-Object -ExpandProperty NameHost -First 1
$Workstation = ($NameHost -Split {$_ -eq "."})[0]
}
} #close of create the Resolve-Hostname function
Anyone that has any idea how to implement this better feel free to chip in as well, I am by no means an expert.
I am not sure where my issue lies so I will start from the top, any recommendations for optimizing my script would be greatly appreciated.
Sets the size of the window
$pshost = get-host
$pswindow = $pshost.ui.rawui
$newsize = $pswindow.windowsize
$newsize.height= 30
$newsize.width = 60
$pswindow.windowsize = $newsize
Declares the Show-Menu and resolve-host function
#Create functions
function Show-Menu { #Create the Show-Menu function
param ([string]$Title = 'Admin Tools Menu') #Sets title
Clear-Host
Write-Host " "
Write-Host " "
Write-Host "`t$Title" -foregroundcolor Blue #Display title of menu
Write-Host "`t1: Add Admin."
Write-Host "`t2: Boot Time."
Write-Host "`t3: User 'Shr_' Groups."
Write-Host "`t4: Computer Management."
Write-Host "`t5: Active Directory."
Write-Host "`t6: Remove Admin."
Write-Host "`t7: Disable Workstation in AD"
Write-Host
Write-Host "`tQ: Enter 'Q' to quit."
Write-Host
Write-Host
} #close of create show menu function
function Resolve-Hostname {
If ($Workstation -like "*.*.*.*") #Convert IP Address to Hostname
{
$NameHost = resolve-dnsname $Workstation | Select-Object -ExpandProperty NameHost -First 1
$Workstation = ($NameHost -Split {$_ -eq "."})[0]
}
} #close of create the Resolve-Hostname function
#End of create functions
Implementation of the script below
#Begin Main Menu
do
{
Show-Menu #Displays created menu above
$Selection = $(Write-Host "`tMake your selection: " -foregroundcolor Red -nonewline; Read-Host)
switch ($selection)
{
'1' { #Add Admin
Clear-Host
$Workstation = $(Write-Host "Workstation\IP Address" -nonewline -foregroundcolor DarkGreen) + $(Write-Host "?: " -NoNewline; Read-Host) #Declare Workstation/IP Address
Resolve-Hostname
$Workstation
pause
After the pause the script goes through adding the workstation to the admin group but I am fairly certain that portion is not relevant to the problem at hand. There are no errors that pop up, just that the $workstation outputs the IP address instead of resolving into the Hostname as per the resolve-hostname function.