replace first character in a string

Starting a powershell script, where I have to connect to the HP server's ilo as part of the overall setup validation.  Our ilo naming standard is to replace first character of the server name with a "R", and I am having issues creating a $iloname from the $servername variable.

All server names / ilo IPs are being imported via a csv import.  Here is the code:

$SeverName = @()
$iloip = @()


# Import Servername & IP from CSV file
    Import-Csv .\ilo.csv
        ForEach-Object {
            $SeverName += $_.Server_Name
            $iloip += $_.ilo_ip
            $iloname = ($Servername.Insert(0,"r").Remove(1,1))
            Write-Host $iloname
        }

and the csv looks like this:

WPXXYYY0app01,192.168.1.1
WPXXYYY0XXapp01,192.168.1.2
WPXXYYY0Zapp01,192.168.1.3
WPXXYYY0pp01,192.168.1.4

Any help to make what I would think simple but I am totatly missing would be greatly apericated!
February 14th, 2014 5:26pm

First of all you have $SeverName and $Servername. That may be causing some problems, or at minimum, it probably will in the future. Here's a small piece of code that might be a helpful start. It takes a string and replaces the first letter with an 'R.'

$Var = 'WPXXYYY0app01'
$Var = $Var.Replace(($Var.Substring(0,1)),'R')

Free Windows Admin Tool Kit Click here and download it now
February 14th, 2014 5:50pm

This may be everything you need and it is based on my example above.

Import-Csv -Path .\ilo.csv |
    ForEach-Object {
        $ServerName = $_.Server_Name
        $iloip = $_.ilo_ip
        $iloname = $ServerName.Replace(($ServerName.Substring(0,1)),'R')
        Write-Output $iloname
    }

  • Proposed as answer by vdboots 11 hours 43 minutes ago
  • Marked as answer by Keith Templin 9 hours 27 minutes ago
February 14th, 2014 5:57pm

This may be everything you need and it is based on my example above.

Import-Csv -Path .\ilo.csv |
    ForEach-Object {
        $ServerName = $_.Server_Name
        $iloip = $_.ilo_ip
        $iloname = $ServerName.Replace(($ServerName.Substring(0,1)),'R')
        Write-Output $iloname
    }

  • Proposed as answer by vdboots 11 hours 37 minutes ago
  • Marked as answer by Keith Templin 9 hours 20 minutes ago
Free Windows Admin Tool Kit Click here and download it now
February 14th, 2014 6:01pm

Or, with a simple regular expression to match any first character

$Var = 'WPXXYYY0app01'
$Var = $Var -replace '^.','R'

February 14th, 2014 6:49pm

Failure here is the error:

You cannot call a method on a null-valued expression.
At P:\~Tasks\FY14-ilo_Config\ilo_configure.ps1:10 char:13
+             $iloname = $servername.Replace(($servername.Substring(0,1)),'R')
+             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

Free Windows Admin Tool Kit Click here and download it now
February 14th, 2014 8:22pm

No errors but it did not write the new name
February 14th, 2014 8:38pm

Found a way to make it work with your code

$severname = @()
$iloip = @()
$iloname = @()
$csv = Import-CSV P:\~Tasks\FY14-ilo_Config\ilo.csv

# Import Servername & IP from CSV file
   ForEach ($line in $csv) {
            $iloname = $line.Server_Name.Replace(($line.Server_Name.Substring(0,1)),'R')
            Write-Host $iloname
            $iloip = $line.ilo_ip
            Write-Host $iloip
        }

Free Windows Admin Tool Kit Click here and download it now
February 14th, 2014 9:09pm

This may be everything you need and it is based on my example above.

Import-Csv -Path .\ilo.csv |
    ForEach-Object {
        $ServerName = $_.Server_Name
        $iloip = $_.ilo_ip
        $iloname = $ServerName.Replace(($ServerName.Substring(0,1)),'R')
        Write-Output $iloname
    }

  • Proposed as answer by vdboots Friday, February 14, 2014 11:48 PM
  • Marked as answer by Keith Templin Saturday, February 15, 2014 2:05 AM
February 15th, 2014 1:56am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics