نحوه به روزرسانی آدرس آی پی DNS ماشین مجازی VMware Windows





در این پست نحوه به روزرسانی آدرس آی پی DNS ماشین مجازی VMware Windows را با استفاده از PowerCLI به شما آموزش خواهیم داد.

در این جا چند شبکه سازی و تغییر در AD و DNS را انجام خواهیم داد. برای انجام این کار آدرس آی پی DNS ما تغییر کرده است. تغییر تنظیمات DNS برای ایستگاه های کاریمان بسیار ساده بود و تنها کافی است محدوده DHCP را تغییر داد.
از طرف دیگر تمام سرورها دارای آی پی استاتیک و DNS IP استاتیک بودند که هر یک از آن ها نیز باید آپدیت می شدند.
درست مثل تمام افرادی که از PowerCLI استفاده می کنند اولین کاری که انجام دادیم این بود که در گوگل کدهای موجود را جستجو کردیم.
پس از چند ویرایش جزئی کد را برای اجرا آماده کردیم.

اولین تلاش برای به روزرسانی DNS ماشین مجازی VMware Windows با استفاده از PowerCLI

Connect-VIServer vcenter.jasemccarty.com
$HostCred = $Host.UI.PromptForCredential(“Please enter credentials”, “Enter ESX host credential”, “”, “”)
$GuestCred = $Host.UI.PromptForCredential(“Please enter credentials”, “Enter Guest credentials”, “”, “”)
$vmlist = Get-Content C:\Scripts\serverlist.txt
foreach ($item in $vmlist) {
# I like to map out my variables
#$vmname = $item.vmname
$pdnswins = “192.168.1.1”
$sdnswins = “192.168.1.2”
#Get the current interface info
     $GuestInterface = Get-VMGuestNetworkInterface -VM $vmlist -HostCredential
$HostCred -GuestCredential $GuestCred
    #If the IP in the VM matches, then I don’t need to update
   Set-VMGuestNetworkInterface -VMGuestNetworkInterface $GuestInterface -HostCredential $HostCred -GuestCredential $GuestCred -DNS $pdnswins,$sdnswins
}

 
به هر نحوی که بود این اسکریپت با خطا مواجه شد. متن های قرمز رنگی در مورد دستورات مفقود به ما نشان داده شد.
در ابتدا مجدداً کلمه «Get-VMGuestNetworkInterface» را در گوگل جستجو کردیم و صفحه vSphere PowerCLI Reference را پیدا کردیم و همه چیز خوب به نظر می رسید.
از طریق توئیتر به Jase رسیدیم تا ببینیم آیا پیشنهادی برای ما دارد.
بلافاصله Jase نیز به ما اطلاع داد که این cmdlet ها اشکال دارند و لینک یک اطلاعیه و این پست وبلاگ مایکروسافت را در اختیار ما قرار داد.
حال تمام ملزومات به روزرسانی اسکریپت را در اختیار داشتیم.


دومین تلاش برای به روزرسانی DNS ماشین مجازی VMware Windows با استفاده از PowerCLI

Function Set-VMGuestWindowsIp {
        <#        .SYNOPSIS
        This function is a modification the Set-VMGuestNetworkInterface
        .DESCRIPTION
        This function is a modification the Set-VMGuestNetworkInterface
.PARAMETER VM
        The VM
        .PARAMETER Adapter
        The Name of the Windows Network Adapter
        .PARAMETER IP
        The IP of the Windows Network Adapter
        .PARAMETER Netmask
        The Netmask of the Windows Network Adapter
        .PARAMETER Gateway
        The Gateway of the Windows Network Adapter
        .PARAMETER GuestUser
        The Windows Guest User Account
        .PARAMETER GuestPass
        The Windows Guest User Account Password
        .PARAMETER SetDns
        True or False, set the DNS Address to the Hardcoded DNS
            .EXAMPLE
        PS C:\> UpdateIp.ps1 -VM VMNAME -Adapter “Local Network Connection” -GuestUser “admin”
-GuestPass “pass” -SetDns $true
        .EXAMPLE
        PS C:\> UpdateIp.ps1 -VM VMNAME -Adapter “Local Network Connection”
-GuestUser “admin” -GuestPass “pass” -SetDns $true
-IP “192.168.0.5” -Netmask “255.255.255.0” -Gateway “192.168.0.1”
        .NOTES
        Author                                    : Jase McCarty
       Version                                   : 0.1
        #>
     param(
        [Parameter(Mandatory=$true)][String]$VM,
        [Parameter(Mandatory=$true)][String]$Adapter,
        [Parameter(Mandatory=$false)][String]$IP,
        [Parameter(Mandatory=$false)][String]$Netmask,
        [Parameter(Mandatory=$false)][String]$Gateway
,        [Parameter(Mandatory=$true)][String]$GuestUser,
        [Parameter(Mandatory=$true)][String]$GuestPass,
        [Parameter(Mandatory=$true)][Boolean]$SetDns
    )
        # Windows netsh path
    $netshPath = “C:\Windows\System32\netsh.exe”
    # Set DNS Entries
    $pdns = “1.1.1.1”
    $sdns = “8.8.8.8”
     $netsh = “$netshPath interface ip set address $Adapter static $IP $NetMask $Gateway1”
    $netsh1 = “$netshPath interface ip set dnsserver $Adapter static $pdns”
    $netsh2 = “$netshPath interface ip set dnsserver $Adapter static $sdns Index=2”
     If ($IP) {
        # Set the IP Address
        Invoke-VMScript -VM (Get-VM -Name $VM) -GuestUser $GuestUser -GuestPassword $GuestPass -ScriptType bat -ScriptText $netsh
    }
        If ($SetDns -eq $true) {
        # Set the Primary DNS
        Invoke-VMScript -VM (Get-VM -Name $VM) -GuestUser $GuestUser -GuestPassword $GuestPass -ScriptType bat -ScriptText $netsh1
         # Set the Secondary DNS
        Invoke-VMScript -VM (Get-VM -Name $VM) -GuestUser $GuestUser -GuestPassword $GuestPass -ScriptType bat -ScriptText $netsh2
    } } # Windows Path
#$vmlist = Get-Content C:\Scripts\logs\serverlist.txt
# Path on my Mac
$vmList = Get-Content /Users/jase/PowerCLI/serverlist.txt
foreach ($VM in $vmlist) {
    Set-VMGuestWindowsIp -VM $VM -Adapter “Local Area Connection” -GuestUser “admin” -GuestPass “pass” -SetDns $true
}

 

ما شوک زده شده بودیم و انتظار بازخوردی به این دقیقی را نداشتیم. پوینتر در سمت راست قرار گرفت و اسکریپتی کاملاً عملی را بدست آوردیم.
حال وقت دیباگ کردن فرا رسیده بود
.
 
نحوه به روزرسانی عملی DNS ماشین مجازی VMware Windows با استفاده از PowerCLI

سه چیزی که در کد بالا تغییر دادیم بدین شرح اند:
  1. ما دریافتیم که نیازی به فراهم سازی مسیر کامل به netsh.exe نیست و این متغیر را حذف کردیم و به جای آن netsh قرار دادیم.
  2. متغیر $Adapter در netsh1 و netsh2 (خطوط 51 و 52) باید میان دو نقل قول قرار گیرند.
  3. در $netsh2 باید از “netsh interface ip add dns “”$Adapter”” $sdns index=2” (خط 52) استفاده می کردیم تا DNS IP دومین به درستی پیکربندی شود.


در ادامه نسخه نهایی این اسکریپت نشان داده شده است:

Function Set-VMGuestWindowsIp {
        <#
        .SYNOPSIS
        This function is a modification the Set-VMGuestNetworkInterface
        .DESCRIPTION
        This function is a modification the Set-VMGuestNetworkInterface
    .PARAMETER VM
        The VM
        .PARAMETER Adapter
        The Name of the Windows Network Adapter
        .PARAMETER IP
        The IP of the Windows Network Adapter
        .PARAMETER Netmask
        The Netmask of the Windows Network Adapter
        .PARAMETER Gateway
        The Gateway of the Windows Network Adapter
        .PARAMETER GuestUser
        The Windows Guest User Account
        .PARAMETER GuestPass
        The Windows Guest User Account Password
        .PARAMETER SetDns
        True or False, set the DNS Address to the Hardcoded DNS
            .EXAMPLE
        PS C:\> UpdateIp.ps1 -VM VMNAME -Adapter “Local Network Connection” -GuestUser “admin” -GuestPass “pass” -SetDns $true
        .EXAMPLE
        PS C:\> UpdateIp.ps1 -VM VMNAME -Adapter “Local Network Connection” -GuestUser “admin” -GuestPass “pass” -SetDns $true -IP “192.168.0.5” -Netmask “255.255.255.0” -Gateway “192.168.0.1”
         .NOTES
        Author                                    : Jase McCarty
        Version                                   : 0.1
        #>
     param(
        [Parameter(Mandatory=$true)][String]$VM,
        [Parameter(Mandatory=$true)][String]$Adapter,
       [Parameter(Mandatory=$false)][String]$IP,
        [Parameter(Mandatory=$false)][String]$Netmask,
        [Parameter(Mandatory=$false)][String]$Gateway,
        [Parameter(Mandatory=$true)][String]$GuestUser,
        [Parameter(Mandatory=$true)][String]$GuestPass,
        [Parameter(Mandatory=$true)][Boolean]$SetDns
    )
       # Windows netsh path
    $netshPath = “C:\Windows\System32\netsh.exe”
    # Set DNS Entries
     $pdns = “1.1.1.1”
    $sdns = “8.8.8.8”
    $netsh = “$netshPath interface ip set address $Adapter static $IP $NetMask $Gateway1”
    $netsh1 = “netsh interface ip set dns “”$Adapter”” static $pdns”
    $netsh2 = “netsh interface ip add dns “”$Adapter”” $sdns index=2″
     If ($IP) {
         # Set the IP Address
        Invoke-VMScript -VM (Get-VM -Name $VM) -GuestUser $GuestUser -GuestPassword $GuestPass -ScriptType Bat -ScriptText $netsh
    }
        If ($SetDns -eq $true) {
         # Set the Primary DNS
        Invoke-VMScript -VM (Get-VM -Name $VM) -GuestUser $GuestUser -GuestPassword $GuestPass -ScriptType Bat -ScriptText $netsh1
         # Set the Secondary DNS
        Invoke-VMScript -VM (Get-VM -Name $VM) -GuestUser $GuestUser -GuestPassword $GuestPass -ScriptType Bat -ScriptText $netsh2
    }
}
# Windows path to server list
$vmlist = Get-Content C:\Scripts\logs\serverlist.txt
# Mac path to server list# $vmList = Get-Content /Users/jase/PowerCLI/serverlist.txt
foreach ($VM in $vmlist) {
    Set-VMGuestWindowsIp -VM $VM -Adapter “Ethernet0” -GuestUser “domain\admin” -GuestPass “Password123” -SetDns $true
}
 
نویسنده مقاله : منبع مقاله :
  • 12 آذر 1398
  • ادمین سایت
  • 1432
طراحی سایت : رسانه گستر © 2002 - 2025