Powershell hash table and variable not working as expected.

Shockey

2[H]4U
Joined
Nov 24, 2008
Messages
2,272
Hello Everyone,

Having difficulty getting below line of code to execute. It doesn't like the format of $iDracNode.RacIP. It only take "RacIP" and fails to connect/translate to IP held in the variable.

I've tried replacing $iDracNode.RacIP with xx.xxx.xxx.xxx and it works. Also works if I use variable such as $IP.

Is there a way to format the below to get the value of $iDracNode.RacIP instead of just "RacIP" when executing the script?

Code:
invoke-expression "plink.exe -ssh -batch -l $iDracNode.RacIP  -u $racuser -pw $racpass set system.servertopology.AisleName $iDracNode.PartNum"


$iDracData
Code:
PartNum   : 555
RackNum      : 4
RacIP        : xxx.xxx.xxx.xxx
RU           : 4
Serial    : serial
RackLocation : RDC

PartNum   : 77kl
RackNum      : 3
RacIP        : xxx.xxx.xxx.xxx
RU           : 676
Serial    : serial
RackLocation : RDC

Code Snippet as a whole.

Code:
$iDracData = Import-Csv $csvFileName
    
    foreach($iDracNode in $idracData)
    {
       write-host -ForegroundColor Green "Setting node information for iDrac node:"$iDracNode.RacIP
       invoke-expression "plink.exe -ssh -batch -l $iDracNode.RacIP  -u $racuser -pw $racpass set system.servertopology.AisleName $iDracNode.PartNum"
     
    }
 
Is the variable being expanded properly? Are variables in command lines supposed to be escaped/enclosed so they aren't misinterpreted?
 
Oh, looks like you're missing a closing double quote on the first line in your foreach loop. Edit: nvm, it's before the variable at the end of the line. why isn't it inside?

Edit: Found it, under variable substitution in MS's docs:

PowerShell allows you to do command execution inside the string with a special syntax. This allows us to get the properties of these objects and run any other command to get a value.
PowerShell

$message = "Time: $($directory.CreationTime)"

This works great for some situations but it can get just as crazy as concatenation if you have just a few variables.
https://docs.microsoft.com/en-us/po...bout-string-substitutions?view=powershell-7.2
 
Last edited:
Hey Nobu,

Thanks for help on this. With your suggestions I made the below change and put plink into function. Working as expected now.

Code:
invoke-expression "plink.exe -ssh -batch -l $iDracNode.RacIP  -u $racuser -pw $racpass set system.servertopology.AisleName $iDracNode.PartNum"

Changed the above line to below and added the function "plink"

Code:
plink $($iDracNode.RacIP) $racuser $racpass "set system.servertopology.AisleName $($iDracNode.PartNum)"

Code:
function plink
{
  [CmdletBinding()]
  PARAM
  (
    [Parameter(Mandatory=$True)]
    [ValidateNotNullOrEmpty()]
    [string] $racIP,
    [Parameter(Mandatory=$True)]
    [ValidateNotNullOrEmpty()]
    [string] $racuser,
    [Parameter(Mandatory=$True)]
    [ValidateNotNullOrEmpty()]
    [string] $racpass,
    [Parameter(Mandatory=$True)]
    [ValidateNotNullOrEmpty()]
    [string] $command)
  & echo y | plink.exe -ssh $racIP -l $racuser -pw $racpass $command
  return
}
 
  • Like
Reactions: Nobu
like this
Back
Top