-1

I'm trying to get the below part of the code done in a relatively condensed fashion (to plug in to a much bigger script). I don't have any problems sending multiple commands this way. However, there's something that's causing the multi-line command to eventually choke. Syntax for Cisco comnmands appear to be correct. I'm not sure if I'm running into some kind of character limit or if I need to escape specific characters in $showintstatusCommands, but nothing I tried seems to work.

This code:

$BGPInterface = "GigabitEthernet0/2"
$showintstatusCommands =  "`nterminal length 0`nsho int $BGPInterface | include reliability|errors`nsho log | include $Date.*LINK-3-UPDOWN.*$BGPInterface`nexit"
($Response = $showintstatusCommands | C:\Windows\plink.exe -ssh -2 -l $Credential.GetNetworkCredential().username -pw $($Credential.GetNetworkCredential().password) $DeviceName -batch) 2>$null | out-null

produces the below when I reveal the contents of the variables. $ShowIntstatusCommands appears to be correct when it echoes locally. Notice, the end of the 3rd line is cut off (number 2 character is missing at the end). Also the subsequent line is some weird residual of the previous line, which starts with $nclude.

PS C:\Users\MKANET\Desktop\test> $Response
CISCO-ROUTER#
CISCO-ROUTER#terminal length 0
CISCO-ROUTER#sho int GigabitEthernet0/2 | include reliability|errors

   reliability 255/255, txload 1/255, rxload 1/255

0 input errors, 0 CRC, 0 frame, 0 overrun, 0 ignored

0 output errors, 0 collisions, 3 interface resets

CISCO-ROUTER#sho log | include Jul 17.*LINK-3-UPDOWN.*GigabitEthernet0/

$nclude Jul 17.*LINK-3-UPDOWN.*GigabitEthernet0/2

CISCO-ROUTER#
CISCO-ROUTER#exit
PS C:\Users\MKANET\Desktop\test> $showintstatusCommands
terminal length 0
sho int GigabitEthernet0/2 | include reliability|errors
sho log | include Jul 17.*LINK-3-UPDOWN.*GigabitEthernet0/2
exit
MKANET
  • 573
  • 6
  • 27
  • 51
  • 1
    Which version of PowerShell are you on? Does it help any if you put your variables in the string you assign to $showintstatusCommands in `{}` e.g. `"...${BGPInterface}...${Date}...${BGPInterface}.."`? – Keith Hill Jul 18 '15 at 05:52
  • I'm using Powershell 4.0; however, whatever I do; I'd like it to work under Powershell 2. I haven't tried bracket's. Thanks for the suggestion! I'll be able to test this on Monday. BTW: I've already tried using $($BGPInterface) without any success. I wonder if there's some kind of character limit. The last character of the variable $BGPInterface isn't visible; and, the next line output becomes mangled. – MKANET Jul 18 '15 at 16:17
  • Hmm, if `$($BGPInterface)` didn't fix then `${BGPInterface}` won't either. It don't believe it is a problem with a character limit. I can take that string fully expanded with the date and BGPInterface, pass it into a little test exe that I wrote that takes its stdin and spits it back out to the console. The whole string comes through. – Keith Hill Jul 18 '15 at 16:33
  • I was actually referring to the remote device's terminal session vertical column character limit. I searched online; but couldn't find anything. – MKANET Jul 18 '15 at 16:50

2 Answers2

0

When you write to a variable in a non-global scope (like script or function scope), PowerShell will not modify any higher level scoped variables of the same name. IOW $Response = 'foo' will create a local copy of the $Response variable and assign 'foo' to it. If your intent is to modify the global variable $Response then change the line to $global:Response = ... What you see in $global:Response is residual, probably from previous tinkerings.

Keith Hill
  • 194,368
  • 42
  • 353
  • 369
  • Thanks, but that's not it. I only temporarily utilized a global variable there to quickly dump the contents of the variable at my command prompt after the script ran (for demonstration purposes). In in my original script there are no global variables defined. – MKANET Jul 18 '15 at 05:04
  • Edit: I just edited the question so it doesn't have "global" on it (to avoid any confusion) – MKANET Jul 18 '15 at 16:08
0

I finally verified that the same behavior was happening on all the remote devices if I typed in the command below manually. The line get's jumbled up if too many characters were used.

I changed:

sho log | include $Date.*LINK-3-UPDOWN.*$BGPInterface

to the below command (removing just a couple of characters); and, it worked fine. It looks like it was a Cisco IOS CLI limit.

sho log | include $Date.*LINK-3-UPDO.*$BGPInterface
MKANET
  • 573
  • 6
  • 27
  • 51