Your PowerGUI Community: Simplify your PowerShell related GUI and script editing needs.


ForEach Statement in PowerShell

This question is not answered
Powershell ForEach Statement?
Hi,

I need to run a powershell script which will read the ips from a csv file and run a snmpget command

$File = Import-Csv C:\IP.csv

foreach ($IP in $File)
{
$IP = $P.IPAddress; #IP is IP address (IPaddress is column name in csv file)

foreach ($IP in $File)
{
snmpget -c fdot -p 8001 $IP 1.3.6.1.4.1.1569.7142.2.1.28.0
}
}

However this down not seem to work. Any help will be greatly appreciated!
All Replies
  • You have 2 ForEach loops running in your script when you only need one. Remove the second one entirely (don't forget to remove the closing curly bracket) and it should work fine.
  • Hi,

    Thanks for your reply, however that does not resolve the issue.

    $RCUFile = Import-Csv C:\RCU.csv

    foreach ($IP in $RCUFile)
    {
    $HN = $P.DNSName; #DNS Name
    $IP = $P.IPAddress; #IP is IP address
    snmpget -c xyz -p 8001 $IP 1.3.6.1.4.1.1569.7142.2.1.28.0
    }

    SNMPGET.EXE :
    At line:12 char:8
    + snmpget <<<<  -c fdot -p 8001 $IP 1.3.6.1.4.1.1569.7142.2.1.28.0
        + CategoryInfo          : NotSpecified: (:String) [], RemoteException
        + FullyQualifiedErrorId : NativeCommandError
     

  • iex "snmpget -c xyz -p 8001 $IP 1.3.6.1.4.1.1569.7142.2.1.28.0"
  • Yeah, looks like you have some variable confusion here:

    You're setting $IP to $P.IPAddress, but never defined $P. Just need to flip some things around:

    Beer$RCUFile = Import-Csv C:\RCU.csv

    foreach ($IP in $RCUFile)
    {
    $HN = $IP.DNSName; #DNS Name
    $P = $IP.IPAddress; #IP is IP address
    snmpget -c xyz -p 8001 $P 1.3.6.1.4.1.1569.7142.2.1.28.0
    }[/b]

    Also, you're defining $HN as the DNS name, but never use in the loop. So you're either intending to add that functionality later, or you can get rid of it.
  • Hi Martin,

    Thank you for your awesome help, that worked like a charm. Based on the snmpget output,

    The output gets displayed as shown below on the screen.

    1.3.6.1.4.1.1569.7142.2.1.28.0 : OCTET STRING- (ascii): 19:41:57 3/4/2013

    Is there a way, I can export the DNS Name, Time(19:41:57) Date(3/4/2013) to a csv fle?

    Thanks,

    Arnab
  • Sure. The cmdlet we're interested in is Export-CSV which takes a Powershell object and converts it to CSV. So we have to parse the output from snmpget to get the data we want then load up a PSObject with the data. When we're done with our loop we can then export the results to a CSV file using Export-CSV.

    Now, I'm making an assumption that snmpget will only ever return 1 line of text in the format you posted. If that's not the case we'll have to change things a bit to take that into account. As it is I noticed that the data fields are separated by a colon ":", so I just use a String split on that character which creates an array out of your output, and the third element happens to be our date/time (you reference it using [2], because the first element is [0], second is [1] and the one we want will be [2]).

    Beer$RCUFile = Import-Csv C:\RCU.csv

    $Results = @()
    foreach ($IP in $RCUFile)
    {
    $HN = $IP.DNSName; #DNS Name
    $P = $IP.IPAddress; #IP is IP address
    $SNMP = snmpget -c xyz -p 8001 $P 1.3.6.1.4.1.1569.7142.2.1.28.0
    $TimeDate = $SNMP.Split(":")[2]
    $Results += New-Object PSObject -Property @{
    DNSName = $HN
    TimeStamp = $TimeDate
    }
    }
    $Results | Export-Csv c:\snmpgetlog.csv -NoTypeInformation[/b]
  • Hi Martin,

    I am getting the following error

    $RCUFile = Import-Csv C:\RCU.csv
    $Result = @()
    foreach ($P in $RCUFile)
    {
    $HN = $P.DNSName; #DNS Name of RCU
    $IP = $P.IPAddress; #IP is IP address
    $SNMP = snmpget -c fdot -p 8001 "$IP" 1.3.6.1.4.1.1569.7142.2.1.28.0
    $TimeDate = $SNMP.split(":")[2]
    $Results += New-Object PSObject -Property @{
    DNSName = $HN
    TimeStamp = $TimeDate
    }
    }
    $Results | Export-Csv c:\snmpgetlog.csv -NoTypeInformation

    ----------------------------
    $Results += New-Object PSObject -Property @{ - complaining about an error in this line of the code.

    Method invocation failed because [System.Management.Automation.PSObject] doesn't contain a method named 'op_Addition'.
    At line:15 char:12
    + $Results += <<<<  New-Object PSObject -Property @{
        + CategoryInfo          : InvalidOperation: (op_Addition:String) [], RuntimeException
        + FullyQualifiedErrorId : MethodNotFound


  • It's just a simple spelling error like the ones we all get all the time. You defined an empty array to collect data called $Result and missed the "s" at the end that you use everywhere else. Just change the second line from $Result = @() to $Results = @() and you'll be fine.
  • Hi,

    That worked great, also the time and date when I run the snmpget command is  in this format 19:51:15 3/5/2013,

    However when I run this script only 19 gets exported in the csv. Please assist.

    "DNSName","TimeStamp"
    "RCU1"," 19"
    "RCU2"," 19"
    "RCU3"," 22"

    Thanks,

    Arnab



  • change this line:

    $TimeDate = $SNMP.split(":")[2]

    to this:

    $TimeDate = ($SNMP -Split ":",3)[2].Trim()


    This stops the split after three elements so that the ":" dividers in the time stamp don't get tested. Since the resulting array is 0-based, the [2] returns the third element. The trim method removes the extra space at the beginning of the returned string.
  • Thank's Art, PowerGUI stopped notifying me of new messages again! Just got the message about this thread just now!
  • There, just got the email notification for this thread. 7 days later. Outstanding.
  • HI Guys,

    Thank you for your help. It works great.

    Regards,

    Arnab