如果我使用cmd行与函数,则输出字符串不同

我正在尝试将日期格式从"PT10H24M30S"转换为"10:24:30"

我创建了以下函数:

function PTTime([string] $time){
        $pattern = "(\d{2})+"
        $matches = $time | Select-String -Pattern $pattern -AllMatches
        $newFormat += $matches.Matches | ForEach {$_.Value}
        $newFormat = $newFormat -Replace " ",":"
        return $newFormat
}

但是,如果我调用该函数,则会得到输出 10 24 30

10
24
30

但是,另一方面,如果我一次在命令行上执行功能命令1,则在打印$newFormat "10:24:30"时得到正确的输出

tangnande 回答:如果我使用cmd行与函数,则输出字符串不同

我意识到$newFormat$returnValue的类型不同

PS在这里有点领先,因为当我打电话给$newFormat | Get-Member$returnValue | Get-Memeber时 输出类型始终为TypeName: System.String。 但是,如果做$newFormat.GetType(),我得到System.String,而如果我打电话给$returnValue.GetType(),我得到System.Array[]

我更新了功能,现在可以按预期运行


function PTTime([string] $time){
        $pattern = "(\d{2})+"
        $matches = $time | Select-String -Pattern $pattern -AllMatches
        $newFormat = $matches.Matches.Value 
        return $newFormat -Join ":"
}

本文链接:https://www.f2er.com/3116275.html

大家都在问