?
Solved

PowerShell function

Posted on 2011-10-19
3
Medium Priority
?
466 Views
Last Modified: 2012-05-12
Experts:
Good afternoon.  Have a task I'd like to examine automating.  I'm kinda new to powershell, and the below code is just a start, but I can't figure out why the call (in the foreach-object) doesn't see the function.  Is it possible to do this?
$part = gwmi win32_logicaldisk
$part|foreach {. chkacl($_.DeviceID)}

function chkacl {
	param($drive)
	if ($drive.DriveType -eq '3') {
Get-Acl $drive|fl *
	}
}

Open in new window

0
Comment
Question by:Just_RC
  • 2
3 Comments
 
LVL 2

Author Comment

by:Just_RC
ID: 36995904
Wait..was pokin' and hopin...here's my 'offical' not working...

$part = gwmi win32_logicaldisk
$part|foreach {chkacl($_.DeviceID)}

function chkacl {
      param($drive)
      if ($drive.DriveType -eq '3') {
Get-Acl $drive|fl *
      }
}
0
 
LVL 9

Accepted Solution

by:
chrismerritt earned 2000 total points
ID: 36995926
Your function needs to go above your script code, powershell processes all lines in order, for example this is right:

Function Badger
{
	Some Function Here
}

#Call Badger Function
Badger

Open in new window


This is not right:

#Call Badger Function
Badger

Function Badger
{
	Some Function Here
}

In VBS the compiler meant it would take care of the Function no matter where it was.

Open in new window

0
 
LVL 2

Author Closing Comment

by:Just_RC
ID: 36996061
Thanks...I knew it had to be "pilot error".

Thanks a ton.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The following article is intended as a guide to using PowerShell as a more versatile and reliable form of application detection in SCCM.
There are times when we need to generate a report on the inbox rules, where users have set up forwarding externally in their mailbox. In this article, I will be sharing a script I wrote to generate the report in CSV format.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Screencast - Getting to Know the Pipeline

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question