Welcome to Powergui.org - an open source community for Windows Powershell

PowerGUI.org PowerGUI.org and blogs

Forums » Request a Script

Thread: ForEach Statement in PowerShell

This question is answered. Helpful answers available: 2. Answered answers available: 1.


Permlink Replies: 13 - Pages: 1 - Last Post: Mar 21, 2013 1:57 PM by: Arnab
Arnab

Posts: 26
Registered: 3/3/13
ForEach Statement in PowerShell
Posted: Mar 3, 2013 8:20 AM
 
  Click to reply to this thread Reply

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!



Martin9700


Posts: 482
Registered: 7/19/12
Re: ForEach Statement in PowerShell
Posted: Mar 3, 2013 11:56 AM   in response to: Arnab
 
  Click to reply to this thread Reply

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.

Martin
www.thesurlyadmin.com
Arnab

Posts: 26
Registered: 3/3/13
Re: ForEach Statement in PowerShell
Posted: Mar 4, 2013 7:05 AM   in response to: Martin9700
 
  Click to reply to this thread Reply

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
 




velaskec


Posts: 52
Registered: 4/15/10
Re: ForEach Statement in PowerShell
Posted: Mar 4, 2013 7:14 AM   in response to: Arnab
 
  Click to reply to this thread Reply

iex "snmpget -c xyz -p 8001 $IP 1.3.6.1.4.1.1569.7142.2.1.28.0"


Martin9700


Posts: 482
Registered: 7/19/12
Re: ForEach Statement in PowerShell
Posted: Mar 4, 2013 8:00 AM   in response to: Arnab
 
  Click to reply to this thread Reply

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:

$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
}


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.

Martin
www.thesurlyadmin.com
Arnab

Posts: 26
Registered: 3/3/13
Re: ForEach Statement in PowerShell
Posted: Mar 4, 2013 1:42 PM   in response to: Martin9700
 
  Click to reply to this thread Reply

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



Martin9700


Posts: 482
Registered: 7/19/12
Re: ForEach Statement in PowerShell
Posted: Mar 4, 2013 2:04 PM   in response to: Arnab
 
  Click to reply to this thread Reply

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]).

$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


Martin
www.thesurlyadmin.com
Arnab

Posts: 26
Registered: 3/3/13
Re: ForEach Statement in PowerShell
Posted: Mar 5, 2013 12:00 PM   in response to: Martin9700
 
  Click to reply to this thread Reply

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





Art Beane

Posts: 760
Registered: 4/23/09
Re: ForEach Statement in PowerShell
Posted: Mar 5, 2013 1:12 PM   in response to: Arnab
 
  Click to reply to this thread Reply

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.


Arnab

Posts: 26
Registered: 3/3/13
Re: ForEach Statement in PowerShell
Posted: Mar 5, 2013 2:21 PM   in response to: Art Beane
 
  Click to reply to this thread Reply

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






Art Beane

Posts: 760
Registered: 4/23/09
Re: ForEach Statement in PowerShell
Posted: Mar 6, 2013 6:15 AM   in response to: Arnab
 
  Click to reply to this thread Reply

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.


Martin9700


Posts: 482
Registered: 7/19/12
Re: ForEach Statement in PowerShell
Posted: Mar 7, 2013 7:01 AM   in response to: Art Beane
 
  Click to reply to this thread Reply

Thank's Art, PowerGUI stopped notifying me of new messages again! Just got the message about this thread just now!

Martin
www.thesurlyadmin.com
Martin9700


Posts: 482
Registered: 7/19/12
Re: ForEach Statement in PowerShell
Posted: Mar 14, 2013 11:03 AM   in response to: Martin9700
 
  Click to reply to this thread Reply

There, just got the email notification for this thread. 7 days later. Outstanding.

Martin
www.thesurlyadmin.com
Arnab

Posts: 26
Registered: 3/3/13
Re: ForEach Statement in PowerShell
Posted: Mar 21, 2013 1:57 PM   in response to: Martin9700
 
  Click to reply to this thread Reply

HI Guys,

Thank you for your help. It works great.

Regards,

Arnab


Legend
MVP: 2501 + pts
Guru: 2001 - 2500 pts
Expert: 751 - 2000 pts
Enthusiast: 31 - 750 pts
Novice: 0 - 30 pts
Moderators
Helpful answer (5 pts)
Answered (10 pts)

Point your RSS reader here for a feed of the latest messages in all forums