I was using below powershell script to loop thru servers in the list to perform particular tasks depending upon the server's hostname. In the below script if $server = 'server1' I want to perform different set of tasks in contrast to other servers, But this loop is directly executing 'else' loop. I'm I missing anything here?
$servernames = @("server1", "server2", "server3")
$user = '**********'
$Password = '*******'
$SecurePassword = $Password | ConvertTo-SecureString -AsPlainText -Force
$cred = New-Object System.Management.Automation.PSCredential -ArgumentList $user, $SecurePassword
foreach($server in $servernames)
{
Invoke-Command -ComputerName $server -Credential $cred -ScriptBlock {
if ($server -eq 'server1')
{
#do some task on server1
}
else
{
#do tasks for remaining servers
}
}
if ($error) { exit 1 }
}