Powershell 函数不处理每个步骤

Powershell 函数不处理每个步骤,powershell,Powershell,下面的代码片段将跳转到正确的函数“ORD_LOG_PROCESS”,它将CD转到路径,但之后它不会存储变量$ORD文件及其后的每个变量都不存储。$ordlogpath目录中有一个文件,如果我在shell中有(gci$ordlogpath |%{$\ux.name}),它会工作,但由于某种原因,它不会通过脚本存储 $ordlogpath = "C:\test_environment\ORD_REPO\ORD_LOGS\" $ordlogexist = gci "C:\test_environmen

下面的代码片段将跳转到正确的函数“ORD_LOG_PROCESS”,它将CD转到路径,但之后它不会存储变量$ORD文件及其后的每个变量都不存储。$ordlogpath目录中有一个文件,如果我在shell中有(gci$ordlogpath |%{$\ux.name}),它会工作,但由于某种原因,它不会通过脚本存储

$ordlogpath = "C:\test_environment\ORD_REPO\ORD_LOGS\"
$ordlogexist = gci "C:\test_environment\ORD_REPO\ORD_LOGS\*.log"


FUNCTION ORD_LOG_PROCESS
{
cd $ordlogpath
$ordfiles = (gci $ordlogpath |% {$_.name})
FOREACH ($ordfile in $ordfiles)
{
$ordlogimport = Import-Csv $ordfile
$ordloggrep = $ordfile
exit
}
}

FUNCTION NO_FILES

{
write-host "NO FILES TO PROCESS"
EXIT
}

IF (!$ordlogexist)

{
NO_FILES
}

else

{
ORD_LOG_PROCESS
}

如果在函数内声明变量,则它们将是该函数的局部变量。这意味着变量不存在于函数之外

但是。。为什么要使用这样的函数?
你不能简单地做下面这样的事情吗

$ordlogpath  = "C:\test_environment\ORD_REPO\ORD_LOGS\*.log"

if (!(Test-Path -Path $ordlogpath)) {
    Write-Host "NO FILES TO PROCESS"
}
else {
    Get-ChildItem -Path $ordlogpath -File | ForEach-Object {
        $ordlogimport = Import-Csv $_.FullName
        # now do something with the $ordlogimport object, 
        # otherwise you are simply overwriting it with the next file that gets imported..
        # perhaps store it in an array?

        # it is totally unclear to me what you intend to do with this variable..
        $ordloggrep = $_.Name
    }

    Write-Host "The log name is: $ordloggrep"
    Write-Host
    Write-Host 'The imported variable ordlogimport contains:'
    Write-Host

    $ordlogimport | Format-Table -AutoSize

}

运行时,
$ordfiles
在脚本中保留什么值?您的
函数ORD\u LOG\u进程
(为什么要编写所有CAP?)没有返回任何内容,它只是在每次迭代中覆盖两个变量。顺便说一句,您的格式不太理想……我已多次清除该变量,当脚本启动时它不会保存任何内容,如果我从cli运行完全相同的命令,它将返回日志name@LotPings,出于某种原因,我总是用大写字母写函数,但显然没有完成,我只是想知道为什么每次runRead
Get help about\u scopes
时这个变量都是空的,或者这个函数是因为当它完成时,它都将捕捉到一个更大的脚本中。这部分应该根据.csv的“文件”列中的字符串移动文件,@200mg您从未提到过任何.csv,或者我忽略了这一点?无论如何,如果您真的觉得需要使用函数,请查看。声明您希望在脚本函数中可用的变量,方法是将它们声明为
$script:ordlogimport
甚至
$global:ordlogimport
这一切都很好,这让我克服了困难,就像@LotPings指出的,它只是对函数了解不够,谢谢您的帮助,当我进入堆栈时,我总是很惊讶,你们可以在5行中的10行中完成我的工作…=)