1

I have read Read a Csv file with powershell and capture corresponding data and have been able to put together some code but it is not giving me what I wanted it to, I am trying to get it to read a CSV, find a server in column 1 and open the ip address in column 2 in Internet explorer.

function Open-RAC
{
param
    (
    [Parameter(Mandatory = $true)]
    [string]$ComputerName
)

$ServerName = @()
$IMM = @()

Import-Csv C:\Scripts\ComputerList\IMMs.CSV |
ForEach-Object {
    $ServerName += $_.ServerName
    $IMM += $_.IMM
}

$inputNumber = $ComputerName#Read-Host -Prompt "Short Server Name"

if ($ServerName -contains $inputNumber)
{
    Write-Host "DRAC Exist"
    Write-Host "Servername : $inputNumber"
    $Where = [array]::IndexOf($ServerName, $inputNumber)
    Write-Host "Where : $Where"
    Write-Host "DRAC IP Address: " $IMM[$Where]
    $URL = $IMM[$Where]
    Write-Host "$URL"
    $IE = new-object -com internetexplorer.application
    $IE.navigate2("http:\\$URL")
    $IE.visible = $true
}
Else
{
    Write-Host " That server does not have a DRAC or you mistyped the server name"
}

however this is not giving my the correct IP address back but whatever the last one in the CSV is.

Any help is appreciated.

Luke
  • 647
  • 1
  • 8
  • 22
  • What is the $ComputerName variable doing? If I change that line to `$inputNumber = "server1"` with a test csv it works fine from me, so your logic in the IF statement is good. Are you sure you are getting the servername into $inputNumber as expected? Before the IF statement, add a line that just has `$inputNumber` so you can see what is in that variable before it gets to the IF statement. – Tony Hinkle May 06 '15 at 14:43
  • I forget part of my code, it is a function and the $Computername gets set when you call the function. and I am getting the correct name all the way up to the end, but it doesn't return the value I expected – Luke May 06 '15 at 15:01
  • Not sure what you're wanting to get on the line that assigns to $inputNumber--user input or the name of the computer the script is running on. To get user input: $inputNumber = Read-Host "Enter the server name:" To get the computer name: $inputNumber = $env:COMPUTERNAME These work for me with the caveat of being case sensitive. It appears that the IndexOf fuction is case sensitive, so if you enter "Win7-Laptop" for the input, but the .CSV file has "WIN7-LAPTOP", then IndexOf returns a -1, which ends up getting the last IP address. – Tony Hinkle May 06 '15 at 15:03
  • so 123Server is not the same as 123SERVER.... wow I completely missed that. Thanks for your help on that, any ideas on how to make prevent a user from entering it in the wrong case? I could make it transfer to all caps correct – Luke May 06 '15 at 15:06
  • So I think maybe the only problem you have is that [array]::IndexOf is case sensitive. If that is the case, you might want to rewrite the question or delete it and write a new one. – Tony Hinkle May 06 '15 at 15:07
  • I am looking for a way to have IndexOf be case insensitive, but haven't found anything yet. If everything in the .csv is upper case, then you can simply convert the input to uppercase with `$inputNumber = $inputNumber.ToUpper()` – Tony Hinkle May 06 '15 at 15:08
  • I changed my CSV to all lowercase and it worked like a charm. Thanks again – Luke May 06 '15 at 15:09

2 Answers2

1

Build a hashtable from the CSV:

$drac = @{}
Import-Csv C:\Scripts\ComputerList\IMMs.CSV | ForEach-Object {
  $drac[$_.ServerName] = $_.IMM
}

Then you can connect to the DRAC like this:

if ($drac.Contains($ServerName)) {
  Write-Host "Servername : $ServerName"
  $URL = $drac[$ServerName]
  $IE = New-Object -COM 'InternetExplorer.Application'
  $IE.Navigate2("http:\\$URL")
  $IE.Visible = $true
}
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
0

The [array]::IndexOf function is case sensitive, and it is returning a -1 if the input is not the same case as the servername that was found in the .csv. You'll need to find a way to make IndexOf case insensitive (I'm looking and will update if I find it) or normalize the input so that it matches what is in the .csv.

Tony Hinkle
  • 4,706
  • 7
  • 23
  • 35