![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| 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. |
| |||||||
![]() |
| |
| | #1 (permalink) |
| | 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 Specs![]() |
| | #2 (permalink) |
| | 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 Specs![]() |
| | #3 (permalink) |
| | 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. 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 Specs![]() |
| | #4 (permalink) |
| | 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 Specs![]() |
| | #5 (permalink) |
| | 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 Specs![]() |
| | #6 (permalink) |
| | 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: 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 Specs![]() |
| | #7 (permalink) |
| | 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? PS> [array]$script:my_array+="foo" PS> [array]$script:my_array+="bar" PS> $script:my_array foo bar |
My System Specs![]() |
| | #8 (permalink) |
| | 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 Specs![]() |
![]() |
| 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 | |||