help with IF statement

$cred = Get-Credential
$getinfo = Import-Csv C:\Users\Soma\Desktop\COMPUTERS.csv
$getinfo | foreach {
$OldServer = $.Oldserver
$NewServer = $
.NewServer

Write-Output “############# PING #############”
PING $OldServer

PING $NewServer

Write-Output “############# IP_CONFIG #############”
Get-WmiObject Win32_NetworkAdapterConfiguration -cn $oldServer -Cr $cred | ft -AutoSize

Write-Output “############# NS_LOOKUP #############”
nslookup $NewServer
nslookup $oldServer

Write-Output “############# COMPUTER_NAME_INFO ##############”
Get-WmiObject win32_computersystem -cn $oldServer -Cr $cred | ft -AutoSize

Write-Output “############# COMPUTER_SERVICES #############”
get-wmiobject win32_Service -cn $oldserver -Cr $cred | ft -AutoSize

Write-Output “############# COMPUTER_PROCESS ##############”
Get-WmiObject win32_process -cn $oldServer -Cr $cred | ft -AutoSize

Write-Output “############# COMPUTER_DISKINFO ##############”
Get-WmiObject win32_logicaldisk -cn $oldServer -Cr $cred | ft -AutoSize

Write-Output “############# COMPUTER_OU ##############”
Get-ADOrganizationalunit -filter ‘name -like “*”’ -properties canonicalname

Write-Host -ForegroundColor Red -BackgroundColor Yellow “<<<<<<<<<<>>>>>>>>>>>>>”

Rename-Computer -ComputerName $OldServer -NewName $NewServer -DomainCredential $cred -Restart -Confirm

Write-host -ForegroundColor Red -BackgroundColor Yellow **************** PLEASE WAIT WHILE YOUR COMPUTER/S RESTART ****************

Write-Output “###################################### POST-RENAME DETAILS ########################################################”

( if Test-Connection -Computername $NewServer -BufferSize 32 -Count 10 -ea 1 -quiet)

Write-Output “############# FLUSH_DNS #############”

ipconfig /flushdns
ipconfig /registerdns

Write-Output “############# PING #############”

PING $NewServer

PING $OldServer

Write-Output “############# IP_CONFIG #############”
Get-WmiObject Win32_NetworkAdapterConfiguration -cn $NewServer -Cr $cred | ft -AutoSize

Write-Output “############# NS_LOOKUP #############”
nslookup $NewServer
nslookup $oldServer

Write-Output “############# COMPUTER_NAME_INFO ##############”
Get-WmiObject win32_computersystem -cn $NewServer -Cr $cred | ft -AutoSize

Write-Output “############# COMPUTER_SERVICES #############”
get-wmiobject win32_Service -cn $NewServer -Cr $cred | ft -AutoSize

Write-Output “############# COMPUTER_PROCESS ##############”
Get-WmiObject win32_process -cn $NewServer -Cr $cred | ft -AutoSize

Write-Output “############# COMPUTER_DISKINFO ##############”
Get-WmiObject win32_logicaldisk -cn $NewServer -Cr $cred | ft -AutoSize

Write-Output “############# COMPUTER_OU ##############”
Get-ADOrganizationalunit -filter ‘name -like “*”’ -properties canonicalname

Write-host -ForegroundColor Red -BackgroundColor Yellow **************** RENAME COMPLETED ON SELECTED SERVERS **************** } | Out-File C:\Users\Soma\Desktop\RENAMETEST1.txt

if statement doesn’t seem to work in the above script, I’m unable to ascertain where im doing wrong, all im trying to do is run the #### POST-RENAME DETAILS ### commands if " ( if Test-Connection -Computername $NewServer -BufferSize 32 -Count 10 -ea 1 -quiet)" newserver name which got renamed gets at least one successful ping.

Any help please :slight_smile: TIA

If statements always need an opening curly bracket { and closing } as you can see in the examples of the Microsoft documentation for If here https://technet.microsoft.com/en-us/library/Hh847876.aspx and below:


$a = 3
if ($a -gt 2)
{
Write-Host “The value $a is greater than 2.”
}

If modified to your code to use opening and closing curly brackets for your If statement:

$cred = Get-Credential
$getinfo = Import-Csv C:\Users\Soma\Desktop\COMPUTERS.csv
$getinfo | foreach {
$OldServer = $.Oldserver
$NewServer = $
.NewServer

Write-Output ‘############# PING #############’
PING.EXE $OldServer

PING.EXE $NewServer

Write-Output ‘############# IP_CONFIG #############’
Get-WmiObject Win32_NetworkAdapterConfiguration -cn $oldServer -Cr $cred | Format-Table -AutoSize

Write-Output ‘############# NS_LOOKUP #############’
nslookup.exe $NewServer
nslookup.exe $oldServer

Write-Output ‘############# COMPUTER_NAME_INFO ##############’
Get-WmiObject win32_computersystem -cn $oldServer -Cr $cred | Format-Table -AutoSize

Write-Output ‘############# COMPUTER_SERVICES #############’
get-wmiobject win32_Service -cn $oldserver -Cr $cred | Format-Table -AutoSize

Write-Output ‘############# COMPUTER_PROCESS ##############’
Get-WmiObject win32_process -cn $oldServer -Cr $cred | Format-Table -AutoSize

Write-Output ‘############# COMPUTER_DISKINFO ##############’
Get-WmiObject win32_logicaldisk -cn $oldServer -Cr $cred | Format-Table -AutoSize

Write-Output ‘############# COMPUTER_OU ##############’
Get-ADOrganizationalunit -filter ‘name -like “*”’ -properties canonicalname

Write-Host -ForegroundColor Red -BackgroundColor Yellow ‘< <<<<<<<>>>>>>>>>>>’

Rename-Computer -ComputerName $OldServer -NewName $NewServer -DomainCredential $cred -Restart -Confirm

Write-host -ForegroundColor Red -BackgroundColor Yellow **************** PLEASE WAIT WHILE YOUR COMPUTER/S RESTART ****************

Write-Output ‘###################################### POST-RENAME DETAILS ########################################################’

if (Test-Connection -Computername $NewServer -BufferSize 32 -Count 10 -Quiet)
{

Write-Output '############# FLUSH_DNS #############'

ipconfig.exe /flushdns
ipconfig.exe /registerdns

Write-Output '############# PING #############'

PING.EXE $NewServer

PING.EXE $OldServer

Write-Output '############# IP_CONFIG #############'
Get-WmiObject Win32_NetworkAdapterConfiguration -cn $NewServer -Cr $cred | Format-Table -AutoSize

Write-Output '############# NS_LOOKUP #############'
nslookup.exe $NewServer
nslookup.exe $oldServer

Write-Output '############# COMPUTER_NAME_INFO ##############'
Get-WmiObject win32_computersystem -cn $NewServer -Cr $cred | Format-Table -AutoSize

Write-Output '############# COMPUTER_SERVICES #############'
get-wmiobject win32_Service -cn $NewServer -Cr $cred | Format-Table -AutoSize

Write-Output '############# COMPUTER_PROCESS ##############'
Get-WmiObject win32_process -cn $NewServer -Cr $cred | Format-Table -AutoSize

Write-Output '############# COMPUTER_DISKINFO ##############'
Get-WmiObject win32_logicaldisk -cn $NewServer -Cr $cred | Format-Table -AutoSize

Write-Output '############# COMPUTER_OU ##############'
Get-ADOrganizationalunit -filter 'name -like "*"' -properties canonicalname

}

Write-host -ForegroundColor Red -BackgroundColor Yellow **************** RENAME COMPLETED ON SELECTED SERVERS **************** } | Out-File C:\Users\Soma\Desktop\RENAMETEST1.txt

I hope above helps.

Thank you Daniel, good catch I missed the curly, however even after placing the curly the code is not giving the desired out put.

I dont know why it is not collecting the new server name details. at this point " Rename-Computer -ComputerName $OldServer -NewName $NewServer -DomainCredential $cred -Restart -Confirm " the computer will be restarted.

and this " if (Test-Connection -Computername $NewServer -BufferSize 32 -Count 10 -Quiet) " should ping or test the connection connection and if responds atleast to one ping, it should proceed with " {

Write-Output ‘############# FLUSH_DNS #############’

ipconfig.exe /flushdns … }

it should ping the new computer name -Count 10 or wait until its up and collect the

" {

Write-Output ‘############# FLUSH_DNS #############’

ipconfig.exe /flushdns … }

but its not happening :frowning:

Your computer may not restart in the time taken by Test-Connection to send 10 ‘pings’

run the code manually and see how long it takes to restart. You could swap your original version of the if statement for this

while ($true){
if (Test-Connection -Computername $NewServer -Count 1 -Quiet) {

    break
}

}

which keeps testing until it gets a reply