Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > PowerShell

Vista - Set a variable in a function then pass it off?

Reply
 
Old 03-18-2008   #1 (permalink)
akcorr


 
 

Set a variable in a function then pass it off?

I'm using a function a build a list of directories to check and see if all
files were deleted. Problem is I'm stuck in the foreach loop and it will
email on every file it finds still sitting in the directory built by the
function. I just want one email stating success or failure with the
$loc.fullpath listed in the email. I know this is probably really easy but I
just need some input at this point.


Function Check
{
Foreach ($f in $DirList)
{
$first = $args[0]
$second = $args[1]
If (!(Test-Path -path $first$second$f))
{
Write-Output $first$second$f "does not exist!"
}Else
{
set-Location $first$second$f
$VarList = Get-ChildItem *.*
If ($Varlist -eq $Null)
{
**Email Success**
}Else
{
**Email Failure**
}

}

}

}


My System SpecsSystem Spec
Old 03-18-2008   #2 (permalink)
akcorr


 
 

RE: Set a variable in a function then pass it off?

I was thinking about having an empty array and building it after each pass
through all the directories but how would I make that array available outside
the function? I tried write-host'ing the content to the screen but it was
blank.

"akcorr" wrote:
Quote:

> I'm using a function a build a list of directories to check and see if all
> files were deleted. Problem is I'm stuck in the foreach loop and it will
> email on every file it finds still sitting in the directory built by the
> function. I just want one email stating success or failure with the
> $loc.fullpath listed in the email. I know this is probably really easy but I
> just need some input at this point.
>
>
> Function Check
> {
> Foreach ($f in $DirList)
> {
> $first = $args[0]
> $second = $args[1]
> If (!(Test-Path -path $first$second$f))
> {
> Write-Output $first$second$f "does not exist!"
> }Else
> {
> set-Location $first$second$f
> $VarList = Get-ChildItem *.*
> If ($Varlist -eq $Null)
> {
> **Email Success**
> }Else
> {
> **Email Failure**
> }
>
> }
>
> }
>
> }
>
My System SpecsSystem Spec
Old 03-18-2008   #3 (permalink)
Marco Shaw [MVP]


 
 

Re: Set a variable in a function then pass it off?

akcorr wrote:
Quote:

> I was thinking about having an empty array and building it after each pass
> through all the directories but how would I make that array available outside
> the function? I tried write-host'ing the content to the screen but it was
> blank.
This would be a PowerShell scope issue.

You can declare this at the beginning of your script:
[array]$script:my_array

Then each time you want to write or each the array:
$script:my_array

PSH>help about_scope

(I'm using the v2 CTP, that help topic should be available with v1.)

Marco

--
Microsoft MVP - Windows PowerShell
http://www.microsoft.com/mvp

PowerGadgets MVP
http://www.powergadgets.com/mvp

Blog:
http://marcoshaw.blogspot.com
My System SpecsSystem Spec
Old 03-18-2008   #4 (permalink)
akcorr


 
 

Re: Set a variable in a function then pass it off?

Thanks Marco I read up on it!

"Marco Shaw [MVP]" wrote:
Quote:

> akcorr wrote:
Quote:

> > I was thinking about having an empty array and building it after each pass
> > through all the directories but how would I make that array available outside
> > the function? I tried write-host'ing the content to the screen but it was
> > blank.
>
> This would be a PowerShell scope issue.
>
> You can declare this at the beginning of your script:
> [array]$script:my_array
>
> Then each time you want to write or each the array:
> $script:my_array
>
> PSH>help about_scope
>
> (I'm using the v2 CTP, that help topic should be available with v1.)
>
> Marco
>
> --
> Microsoft MVP - Windows PowerShell
> http://www.microsoft.com/mvp
>
> PowerGadgets MVP
> http://www.powergadgets.com/mvp
>
> Blog:
> http://marcoshaw.blogspot.com
>
My System SpecsSystem Spec
Old 03-18-2008   #5 (permalink)
akcorr


 
 

Re: Set a variable in a function then pass it off?

Since this in a foreach loop everytime it runs through it erases data from a
pervious loop. Is there a way to append to an array rather than dumping new
data in it?

"akcorr" wrote:
Quote:

> Thanks Marco I read up on it!
>
> "Marco Shaw [MVP]" wrote:
>
Quote:

> > akcorr wrote:
Quote:

> > > I was thinking about having an empty array and building it after each pass
> > > through all the directories but how would I make that array available outside
> > > the function? I tried write-host'ing the content to the screen but it was
> > > blank.
> >
> > This would be a PowerShell scope issue.
> >
> > You can declare this at the beginning of your script:
> > [array]$script:my_array
> >
> > Then each time you want to write or each the array:
> > $script:my_array
> >
> > PSH>help about_scope
> >
> > (I'm using the v2 CTP, that help topic should be available with v1.)
> >
> > Marco
> >
> > --
> > Microsoft MVP - Windows PowerShell
> > http://www.microsoft.com/mvp
> >
> > PowerGadgets MVP
> > http://www.powergadgets.com/mvp
> >
> > Blog:
> > http://marcoshaw.blogspot.com
> >
My System SpecsSystem Spec
Old 03-18-2008   #6 (permalink)
Keith Hill [MVP]


 
 

Re: Set a variable in a function then pass it off?

"akcorr" <akcorr@xxxxxx> wrote in message
news:38A5A722-F82E-4365-8CC9-54143E74AFE5@xxxxxx
Quote:

> Since this in a foreach loop everytime it runs through it erases data from
> a
> pervious loop. Is there a way to append to an array rather than dumping
> new
> data in it?
>
> "akcorr" wrote:
Why don't you just have the function "output" the item i.e don't capture it
in a variable and it will contribute to the function's output. The caller
of the function will receive either nothing (nothing output), a scalar (one
item found) or an array (multiple items found):

function GetFiles($path) {
get-childitem $path -recurse | where {!$_.PSIsContainer}
}

--
Keith

My System SpecsSystem Spec
Old 03-18-2008   #7 (permalink)
Marco Shaw [MVP]


 
 

Re: Set a variable in a function then pass it off?

akcorr wrote:
Quote:

> Since this in a foreach loop everytime it runs through it erases data from a
> pervious loop. Is there a way to append to an array rather than dumping new
> data in it?
Shortcut:
PS> [array]$script:my_array+="foo"
PS> [array]$script:my_array+="bar"
PS> $script:my_array
foo
bar
My System SpecsSystem Spec
Old 03-20-2008   #8 (permalink)
akcorr


 
 

Re: Set a variable in a function then pass it off?

Guys,

Thanks for you help! Everything looks good except one issue. If the job is
successful $script:List should be $null. Thats exactly what happens when I
type it at the prompt but when using it in a IF statement to check if it's
$null and email me the results System.Collections.Hashtable shows up? Any
ideas?


"Marco Shaw [MVP]" wrote:
Quote:

> akcorr wrote:
Quote:

> > Since this in a foreach loop everytime it runs through it erases data from a
> > pervious loop. Is there a way to append to an array rather than dumping new
> > data in it?
>
> Shortcut:
> PS> [array]$script:my_array+="foo"
> PS> [array]$script:my_array+="bar"
> PS> $script:my_array
> foo
> bar
>
My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
Get variable data from function to pass back to main for use. VB Script
Using a variable from within a function PowerShell
Why the Value not able to Pass to a Function PowerShell
how to pass *properties* to a function PowerShell
Howto: pass all args to a sub function? PowerShell


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46