Change_vNIC.ps1 (zpäť na zoznam)
Zmena VMWare E1000 sieťovej karty na VMXNet3 na virtuálnych servroch. Skript nie je plne funkčný, kedže je dosť zložité vierohodne zálohovať a následne obnoviť sieťovú konfiguráciu a taktiež je takmer nemožné debugovať skript, keďže výstupy nie sú vypísané do konzoly.
#vCenter name
$vCenter = ""
#admin account and password
$adminAccount = ""
$adminPWD = ""
#list of VMs
$VMlist = Get-Content "C:\temp\vmlist.txt"
#error settings
$ErrorActionPreference= 'SilentlyContinue'
#log folder
$log_folder = "C:\temp\vNIC_Change"
#log file
$log_file = $log_folder+"\"
if (-not (Test-Path -Path $log_folder)) {
if (New-Item -Path $log_folder -ItemType Directory) {
Write-Host "Log folder `"$log_folder`" created."
}
else {
Write-Host "Unable to create log folder."
}
}
else {
Write-Host "Log folder already exists."
}
#vmware snapin
Add-PSSnapin VMware.VimAutomation.Core
#connection to vCenter
$connect = Connect-VIServer -server $vCenter
#get CLI version, different cmdlets used in version 6
$cli = (get-powercliversion).major
if ($connect) {
foreach ($vm in $VMlist) {
Write-Host "Working on $vm"
#getting guest operating system
$guestOS = (get-vmguest $vm).OSFullName
#checking if VM has E1000 adapter
$adapter = get-vm $vm | Get-NetworkAdapter | Where-Object {$_.type -eq "e1000"}
#performing only on 2012 and only for e1000 adapter, vmtools have to be also installed, if these conditions aren't met, skipping to next server
if ((get-vmguest $vm).toolsversion) {
if ($guestOS -match "2012") {
if ($adapter) {
#save configuration for adapters
$script = '
netsh interface ip dump > c:\temp\ifconfig_raw.txt
'
$output = Invoke-VMScript -VM $vm -ScriptText $script -GuestUser $adminAccount -GuestPassword $adminPWD
$log_file += $vm+"_IPdump.txt"
if ($output) { #not working with -GuestCredentials
$output | Out-File $log_file
Write-Host "Command to dump network configuration executed, check log for output $log_file."
}
else {
Write-Host -ForegroundColor "Red" "Unable to execute `"netsh`" command to dump network configuration on $vm. Skipping to next server."
"Unable to execute `"netsh`" command to dump network configuration." | Out-File $log_file
continue
}
#deleting blank rows, restore will not work with them
$script = '
get-content c:\temp\ifconfig_raw.txt | where {$_ -ne ""} > c:\temp\ifconfig.txt
'
$log_file += $vm+"_blankrows.txt"
$output = Invoke-VMScript -VM $vm -ScriptText $script -GuestUser $adminAccount -GuestPassword $adminPWD
if ($output) {
$output | Out-File $log_file
Write-Host "Command to remove blank rows from netsh dump executed."
}
else {
Write-Host -ForegroundColor "Red" "Unable to execute command to remove blank rows from netsh dump on $vm. Skipping to next server."
"Unable to execute command to remove blank rows from netsh dump." | Out-File $log_file
continue
}
#settings of old adapter
$script = '
$old_adapter = get-wmiobject win32_networkAdapter | Where-Object {$_.Manufacturer -match "Intel"}
$old_adapter.NetConnectionID | Out-File C:\Temp\adapter_name.txt
$old_adapter.MACAddress | Out-File C:\Temp\adapter_mac.txt
$old_adapter.NetConnectionID = "E1000"
$old_adapter.Put()
'
$log_file += $vm+"_oldNIC.txt"
$output = Invoke-VMScript -VM $vm -ScriptText $script -GuestUser $adminAccount -GuestPassword $adminPWD
if ($output) {
Write-Host "Command to save name, MAC and to rename adapter executed, check log for output $log_file."
$output | Out-File $log_file
}
else {
Write-Host -ForegroundColor "Red" "Unable to execute commands to save name, MAC and to rename adapter."
"Unable to execute commands to save name, MAC and to rename adapter." | Out-File $log_file
continue
}
#change adapter type to vmxnet3
$log_file = $vm+"_vNICchange.txt"
if ($adapter | set-networkadapter -type vmxnet3 -confirm:$false) {
Write-Host "Adapter type changed to VMxnet3."
"Adapter type changed to VMxnet3." | Out-File $log_file
}
else {
Write-Host -ForegroundColor "Red" "Unable to change adapter type to VMxnet3 for $vm. Skipping to next server."
"Unable to change adapter type to VMxnet3. Skipping to next server." | Out-File $log_file
continue
}
## shutdown the server
$log_file = $vm+"_shutdown.txt"
if ($cli -eq 6) {
if (Stop-VMGuest $vm -Confirm:$false) {
Write-Host "Gracefull shutdown of $vm executed"
"Gracefull shutdown of $vm executed" | Out-File $log_file
}
else {
Write-Host -ForegroundColor "Red" "Unsuccessfull gracefull shutdown of $vm. Forcing...."
"Unsuccessfull gracefull shutdown of server. Forcing...." | Out-File $log_file
if (Stop-VM $VM -Confirm:$false) {
Write-Host -ForegroundColor "Yellow" "Successfull shutdown of $vm"
Add-Content "Successfull shutdown of VM" $log_file
}
}
}
else {
if (Shutdown-VMGuest $vm -Confirm:$false) {
Write-Host "Gracefull shutdown of $vm executed"
"Gracefull shutdown of $vm executed" | Out-File $log_file
}
else {
Write-Host -ForegroundColor "Red" "Unsuccessfull gracefull shutdown of $vm. Forcing...."
"Unsuccessfull gracefull shutdown of server. Forcing...." | Out-File $log_file
if (Stop-VM $VM -Confirm:$false) {
Write-Host -ForegroundColor "Yellow" "Successfull shutdown of $vm"
Add-Content "Successfull shutdown of VM" $log_file
}
}
}
#checking server status, if it is powered off
$log_file += $vm+"_shutdown.txt"
$i = 0
do {
start-sleep -s 10
$status = (Get-VM $vm).PowerState
Write-Host "Waiting for complete shutdown. $status"
$i++
if ($i -eq 18) {
Write-host -ForegroundColor "Red" "Server $vm is not off after 3 minutes. Check status manually"
"Server is not off after 3 minutes." | Out-File $log_file
}
} while ($status -ne "PoweredOff")
#VM start
Start-VM $vm -confirm:$false
#waiting for complete boot
$log_file += $vm+"_poweron.txt"
$i = 0
do {
Start-Sleep -s 10
$toolsStatus = (Get-VMGuest $vm).ExtensionData.ToolsStatus
Write-Host "Waiting for complete start. $toolsStatus"
$i++
if ($i -eq 18) {
Write-host -ForegroundColor "Red" "Server $vm is not on after 3 minutes. Check status manually"
"Server is not on after 3 minutes." | Out-File $log_file
}
} while ($toolsStatus -ne "toolsOK")
#configuring name of new adapter
$script = '
$adapterName = Get-Content C:\Temp\adapter_name.txt
$adapterMAC = Get-Content C:\Temp\adapter_mac.txt
$new_adapter = get-wmiobject win32_networkAdapter | Where-Object {$_.MACAddress -eq $adapterMAC}
$new_adapter.NetConnectionID = $adapterName
$new_adapter.Put()
'
$log_file += $vm+"_newNIC.txt"
$output = Invoke-VMScript -VM $vm -ScriptText $script -GuestUser $adminAccount -GuestPassword $adminPWD
if ($output) {
Write-Host "Command to rename new adapter executed. Check log for output $log_file"
$output | Out-File $log_file
}
else {
Write-Host -ForegroundColor "Red" "Unable to execute command to rename new adapter. Skipping to next server."
"Unable to execute command to rename new adapter." | Out-File $log_file
continue
}
#set IP configuration to new adapter
$script = '
netsh -c interface -f c:\temp\ifconfig.txt
'
$log_file += $vm+"_restoreIP.txt"
$output = Invoke-VMScript -VM $vm -ScriptText $script -GuestUser $adminAccount -GuestPassword $adminPWD
if ($output) {
Write-Host "Command to restore network configuration executed. Check log for output $log_file"
$output | Out-File $log_file
}
else {
Write-Host -ForegroundColor "Red" "Unable to execute command to restore network configuration. Skipping to next server."
"Unable to execute command to restore network configuration." | Out-File $log_file
continue
}
}
else {
Write-Host -ForegroundColor "Yellow" "$vm doesn't have E1000 adapter. Skipping to next server."
$log_file = $vm+"_E1000.txt"
"Server has no E1000 adapter." | Out-File $log_file
continue
}
}
else {
Write-Host -ForegroundColor "Yellow" "$vm is not Windows Server 2012. Skipping to next server."
$log_file = $vm+"_OSversion.txt"
"Server is not 2012 Windows Server." | Out-File $log_file
continue
}
}
else {
Write-Host -ForegroundColor "Red" "$vm has no VMTools installed, unable to change the vNIC via this script. Skipping to next server."
$log_file = $vm+"_vmtools.txt"
"No VMTools installed, unable to continue." | Out-File $log_file
continue
}
}
#disconnecting from vCenter server
disConnect-VIServer -Server $vCenter -Confirm:$false
}
else {
Write-Host -ForegroundColor "Red" "Unable to connect to vCenter $vCenter"
}