PowerShell foreach within function

RJC

I'm writing a function to collect logs for analysis.

I'm trying to use variables to keep it as tight as possible, but for some reason the function won't create, moans about brackets and variables.

Any assistance would be appreciated.

FUNCTION GetLogs ($CompName)
{
$LOGS    = "CcmExec.log,Scheduler.log,WUAHandler.log"
$CCMPath = "C$\Windows\CCM\Logs"
$Target  = "C:\Temp\Logs"

foreach (file$ IN $LOGS ) {file$ = Copy-Item \\$CompName\$CCMPath\$LOGS $Target\$CompName-$LOGS}    
}
30000MONKEYS

That code won't work.

I think you meant to define an array in $LOGS, but you are actually defining just one String.

Try this: $LOGS = "CcmExec.log", "Scheduler.log", "WUAHandler.log"

Also it looks like you are trying to copy those 3 Files somewhere, but what you are doing is setting the var $file each turn to something else. The statement $file In $LOGS means that the variable $file will be "CcmExec.log" the first time of the loop, then "Scheduler.log" and so on.

So what I think you want to do is this:

Function GetLogs ($CompName) {

$LOGS    = "CcmExec.log","Scheduler.log","WUAHandler.log"
$CCMPath = "C$\Windows\CCM\Logs"
$Target  = "C:\Temp\Logs"

foreach ($file In $LOGS ) {
       Copy-Item -Path \\$CompName\$CCMPath\$file -Destination $Target\$CompName-$file
    }    
}

I also removed some typos.

Is that about right?

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Explanation of PHP array foreach scope within function

How to properly set function scope within a powershell module?

Prepending text to only certain variables within a Powershell function

PowerShell: Disposing of a call result within a function - best practice

r - foreach unable to find object within function

Is there a way to call a variable that is been declared in within a foreach function block?

PowerShell string format different behaviour within function

Call Function within ForEach Loop

Pass arguments to a function called within a switch in PowerShell

Foreach loops within a foreach loop

Powershell New line within function

Printing to Powershell screen from within script function

Modifying array within forEach

concurrency modeling in PHP for looping through data within a foreach function

Using foreach within a foreach with php

Powershell: dynamic variables within foreach

Push value to multidimensional array within a foreach loop, inside a custom function

Knockout JS throws "not a function" error from within foreach

Increment variable in PowerShell from within if statement within a foreach loop. Seems broken

powershell use of where-object within script function not working

Running a foreach loop within a Curl function

How to call a function within a foreach parallel loop

Placing an if statement within a map or forEach function

Can't apply other const within a forEach function

TypeScript: Change of a function argument type is not picked up in forEach within the function

javascript how do i get .data info within a function with forEach function?

Nesting ForEach within If statement (PowerShell)

PowerShell - Provide Real Time Output to Console from within ForEach-Object or any Loop

Why I cannot change the arg's type within a PowerShell function?