troubleshooting Question

Having trouble making a script work

Avatar of colby_watts
colby_wattsFlag for United States of America asked on
Powershell
12 Comments1 Solution469 ViewsLast Modified:
Experts -

Am I missing something?  I get the folllowing when I do this:

####### WITH THIS DATA (saved as .\etherchannel-summary-test.txt)::

Group  Port-channel  Protocol    Ports
------+-------------+-----------+-----------------------------------------------
1      Po1(SU)          -        Gi0/47(P)   Gi0/48(P)  

######## I RUN THIS SCRIPT:

$separator = "|"
$entete = "Group  PortChannel  Protocol    Ports"
$sourceFile = ".\etherchannel-summary-test.txt"
$targetFile = ".\etherchannel-summary-test.csv"
# FIRST ROW : THE FIELDS NAME
# WE REPLACE THE OLD FILE WITH ">" AND NOT ">>" TO ADD RECORD

echo "Group|PortChannel|Protocol|Ports" > $targetFile
Import-Csv $sourceFile | foreach {
        $line = $_.$entete
        $Group = $line.substring(0,7).trim().trimEnd('$')
        $PortChannel = $line.substring(7,14).trim().trimEnd('$')
        $Protocol =  $line.substring(21,12).trim().trimEnd('$')
        $Ports = $line.substring(33).trim().trimEnd('$')
        $newline = $Group + $separator + $PortChannel + $separator + $Protocol + $separator + $Ports
       
        echo $newline >> $targetFile
}

######## AND GET THESE ERRORS
PS C:\Documents and Settings\cwatts\Desktop\RHUBNA-ACCESS\ps-test> .\etherchannel-summary-test.ps1
You cannot call a method on a null-valued expression.
At C:\Documents and Settings\cwatts\Desktop\RHUBNA-ACCESS\ps-test\etherchannel-summary-test.ps1:11 char:33
+         $Group = $line.substring <<<< (0,7).trim().trimEnd('$')
You cannot call a method on a null-valued expression.
At C:\Documents and Settings\cwatts\Desktop\RHUBNA-ACCESS\ps-test\etherchannel-summary-test.ps1:12 char:39
+         $PortChannel = $line.substring <<<< (7,14).trim().trimEnd('$')
You cannot call a method on a null-valued expression.
At C:\Documents and Settings\cwatts\Desktop\RHUBNA-ACCESS\ps-test\etherchannel-summary-test.ps1:13 char:37
+         $Protocol =  $line.substring <<<< (21,12).trim().trimEnd('$')
You cannot call a method on a null-valued expression.
At C:\Documents and Settings\cwatts\Desktop\RHUBNA-ACCESS\ps-test\etherchannel-summary-test.ps1:14 char:33
+         $Ports = $line.substring <<<< (33).trim().trimEnd('$')

#######  I NEED TO FIGURE OUT WHAT I AM DOING WRONG BEFORE I ALTER ANOTHER TWO SCRIPTS TRYING TO #######  PULL SIMIILAR DATA

Thanks for your help.
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 12 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 12 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros