【问题标题】:Passing a path directory to a function in PowerShell将路径目录传递给 PowerShell 中的函数
【发布时间】:2016-12-23 14:03:25
【问题描述】:

我是 PS 新手,我正在尝试编写一个从全局变量中获取参数的函数。我想将从 .txt 文件中读取的路径名传递给同一脚本中的函数。

function GetCorrectChildren ([string] $homepath,$min,$max,$row)
{
    #Testpoint 2
    write-host "homepath = $homepath"
    $ColItem = (Get-ChildItem $homepath |? {$_.PSIsContainer} | sort-object)
}

foreach ($homepath in (Get-Content $PSScriptRoot\homepath_short.txt))
{
    $freeSpace = [win32api]::GetDiskFreeSpace("$homepath").FreeBytesAvailable / 1073741824
    $totalSpace = [win32api]::GetDiskFreeSpace("$homepath").TotalNumberOfBytes / 1073741824 
    $percentageFreeSpace = $freeSpace / $totalSpace * 100

    if($freeSpace -lt $threshold)
    {
    #Testpoint 1
    write-host "homepath = $homepath"
    GetCorrectChildren ("$homepath",$min,$max,$OriRow)
}

对于#Testpoint 1,它会正确返回路径名\\C:\test1\test_a。但是在#Testpoint 2 中,它返回\\C:\test1\test_a 20 30 System.Object
我不明白20 30 System.Object 是什么意思,它来自哪里?有人可以对此有所启发吗?谢谢

【问题讨论】:

    标签: powershell parameter-passing pathname


    【解决方案1】:

    修改最后一行

    GetCorrectChildren ("$homepath",$min,$max,$OriRow)
    

    GetCorrectChildren $homepath $min $max $OriRow
    

    ("$homepath",$min,$max,$OriRow) 创建一个包含四个值的数组,并将其作为第一个参数传递给函数GetCorrectChildren,以便其中的write-host "homepath = $homepath" 打印所有四个值

    【讨论】:

      猜你喜欢
      • 2020-09-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-10-22
      • 2011-02-26
      • 2017-04-20
      • 2021-12-02
      • 2018-12-26
      相关资源
      最近更新 更多