VMware Cloud Community
ganapa2000
Hot Shot
Hot Shot
Jump to solution

Error : ip : The term 'ip' is not recognized as the name of a cmdlet, function, script file

Hi,

I am getting the error while using invoke-vmscript as I am getting the below error.

Please help!!

$code = @'
"iface=$(ip -o link | grep ether | awk -F': ' '{print $2}' | head -1)"
ifdown $iface; ifup $iface
'@

$sInvoke = @{
VM = $vm
ScriptType = 'bash'
ScriptText = $ExecutionContext.InvokeCommand.ExpandString($code)
GuestCredential = $Creds
}
Invoke-VMScript @sInvoke
}

 

Error

ip : The term 'ip' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. At line:1 char:12 + """iface=$(ip -o link | grep ether |awk -F': ' '{print $2}' | head -1 ... + ~~ + CategoryInfo : ObjectNotFound: (ip:String) [], CommandNotFoundException + FullyQualifiedErrorId : CommandNotFoundException

 

Reply
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Shouldn't you use an export for that local variable?
Not sure if the double quotes are needed on that first line (not a Linux specialist).

$code = @'
export iface=`$(ip -o link | grep ether | awk -F': ' '{print `$2}' | head -1)
ifdown `$iface; ifup `$iface
'@@


 


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

Reply
0 Kudos
6 Replies
LucD
Leadership
Leadership
Jump to solution

You have to escape the variables that you don't want to be substituted by the ExpandString method.
Like this

$code = @'
"iface=`$(ip -o link | grep ether | awk -F': ' '{print `$2}' | head -1)"
ifdown `$iface; ifup `$iface
'@@

In fact why do you call the ExpandString, there are no variables in your code that need to be substituted


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
ganapa2000
Hot Shot
Hot Shot
Jump to solution

LucD,

Now I am getting the below error as variable substitution is not working 

VM : poclqapemytest

ExitCode : 1 ScriptOutput : bash: iface=ens192: command not found

usage: ifdown <configuration>

Usage: ifup <configuration>

 

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

What variable substitution?
What needs to be substituted on the caller's side?
In fact, where did you get that code from, I have no clue what you are trying to do.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
ganapa2000
Hot Shot
Hot Shot
Jump to solution

LucD,

I am trying to disable and enable the network interface on the RHEL VM. Few VMs has a different network interface names.

From the below, I am trying to get the network interface name and store the value in iface variable 

"iface=`$(ip -o link | grep ether | awk -F': ' '{print `$2}' | head -1)"

and Pass the network interface variable to below command to disable and enable

ifdown `$iface; ifup `$iface

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Shouldn't you use an export for that local variable?
Not sure if the double quotes are needed on that first line (not a Linux specialist).

$code = @'
export iface=`$(ip -o link | grep ether | awk -F': ' '{print `$2}' | head -1)
ifdown `$iface; ifup `$iface
'@@


 


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
ganapa2000
Hot Shot
Hot Shot
Jump to solution

Perfect... Thank you LucD. That worked perfectly 🙂

 

Reply
0 Kudos