简体   繁体   English

如何处理 Powershell Object 调用 function 的结果

[英]How to handle the result of a Powershell Object to call a function

Here is my code:这是我的代码:

function setParameterb () {
    $Name = $args[0]  

 "Arg1: $Name"
 
}
 
Write-Output "### Starte AlertlogRotate ###" 

$folders = @('d:', 'e:', 'f:', 'g:', 'h:', 'i:', 'j:', 'k:', 'l:')

foreach ($i in $folders) {
  $filenames = Get-ChildItem -Path $i\*\log -ErrorAction SilentlyContinue -Recurse -Filter alert_*.log | Select-Object FullName 
  
}

The setParametersb function is just for test now and should only print the result. setParametersb function 现在只是为了测试,应该只打印结果。 Later, I will use it to zip logfiles which get too big.稍后,我会用它来处理 zip 个太大的日志文件。

I need to get the result of this powershell object into a string to call a function for every line.我需要将这个 powershell object 的结果放入一个字符串中,以便为每一行调用 function。 The Object looks like this: Object 看起来像这样:

FullName                                                             
--------                                                             
D:\AREA\log\diag\rdbms\area\area\trace\alert_area.log        
D:\CONS\log\diag\rdbms\cons\cons\trace\alert_cons.log    
D:\DEV01\log\diag\rdbms\dev01\dev01\trace\alert_dev01.log            
G:\TEST01\LOG\diag\rdbms\test01\test01\trace\alert_test01.log        
G:\TEST02\log\diag\rdbms\test02\test02\trace\alert_test02.log

I know, that I have to crop the headline "FullName", the row"--------" and some empty lines, but this is not my problem now.我知道,我必须裁剪标题“FullName”、行“--------”和一些空行,但这不是我现在的问题。 My problem is to transfer the object $filenames into an array to be able to call the function setParameterb with every single line from the output.我的问题是将 object $filenames 转移到一个数组中,以便能够使用 output 中的每一行调用 function setParameterb。

tried lots of other stuff, but finally this solved my problem:尝试了很多其他的东西,但最终这解决了我的问题:

$filenames |ForEach-Object { setParameterb $_.FullName } 

try stuff like尝试类似的东西

function setParameterb () {
param (
    [Parameter(Mandatory=$true)]
    $names,
)
    
foreach($name in $names){
  "Arg1: $name"
}
 
 
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM