PowerShell:函数没有正确的返回值 [英] PowerShell: Function doesn't have proper return value

查看:721
本文介绍了PowerShell:函数没有正确的返回值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个powershell脚本来比较两个文件夹的内容:

  $ Dir1 =d:\ TEMP \\ Dir1
$ Dir2 =d:\ TEMP \Dir2

函数测试差异($ Dir1,$ Dir2){
$ fileList1 = Get-ChildItem $ Dir1 -Recurse | Where-Object {!$ _。PsIsContainer} | Get-Item | Sort-Object -Property Name
$ fileList2 = Get-ChildItem $ Dir2 -Recurse | Where-Object {!$ _。PsIsContainer} | Get-Item | ($ fileList1.Count -ne $ fileList2.Count){
Write-Host以下文件不同:
比较对象 - 属性名称

如果ReferenceObject $ fileList1 -DifferenceObject $ fileList2 - 属性名称-PassThru |格式表FullName
返回$ false
}

返回$ true
}

$ i =测试比较$ Dir1 $ Dir2

如果($ i){
写输出Test OK
} else {
写主机测试失败-BackgroundColor Red
}

如果我在 Compare-Object ,然后我在控制台中运行这个命令,我得到了不同的列表。如果我运行整个脚本,我不会得到任何输出。为什么?



我正在使用PowerGUI脚本编辑器,但我也尝试了普通的ps控制台。

编辑:



问题是检查脚本的结尾。



<$ p $如果($ i){
写输出Test OK
...
= $ $ $ $ $ $ = Test-Diff $ Dir1 $ Dir2

...

如果我调用 Test-Diff code> $ i = 检查,它的工作原理!


$ b

Test-Diff 返回一个对象数组,而不是预期的bool值:

  [DBG]:PS D:\>> $ i | ForEach-Object {$ _。GetType()} |格式 - 表 - 属性名称
名称
----
FormatStartData
GroupStartData
FormatEntryData
GroupEndData
FormatEndData
布尔

如果我用比较对象注释掉了这一行,那么返回值就是一个布尔值,正如预期的那样。



<问题是:为什么?

解决方案

我在这里找到了答案: http://martinzugec.blogspot.hu/2008/08/returning-values-from-fuctions-

 功能栏{
[System.Collections.ArrayList] $ MyVariable = @()
$ MyVariable.Add(a)
$ MyVariable.Add(b)
返回$ MyVariable
}

使用PowerShell方法返回对象:@(0,1 ,a,b)而不是@(a,b)

要使该函数按预期工作,您需要重定向输出为空:

 功能栏{
[System.Collections.ArrayList] $ MyVariable = @()
$ MyVariable.Add(a)| Out-Null
$ MyVariable.Add(b)| Out-Null
返回$ MyVariable
}

在我们的例子中,函数必须按照Koliat的建议重构。


I wrote a powershell script to compare the content of two folders:

$Dir1 ="d:\TEMP\Dir1"
$Dir2 ="d:\TEMP\Dir2"

function Test-Diff($Dir1, $Dir2) {
    $fileList1 = Get-ChildItem $Dir1 -Recurse | Where-Object {!$_.PsIsContainer} | Get-Item | Sort-Object -Property Name
    $fileList2 = Get-ChildItem $Dir2 -Recurse | Where-Object {!$_.PsIsContainer} | Get-Item | Sort-Object -Property Name

    if($fileList1.Count -ne $fileList2.Count) {
        Write-Host "Following files are different:"
        Compare-Object -ReferenceObject $fileList1 -DifferenceObject $fileList2 -Property Name -PassThru | Format-Table FullName
        return $false
    }

    return $true
}

$i = Test-Diff $Dir1 $Dir2

if($i) { 
    Write-Output "Test OK" 
} else { 
    Write-Host "Test FAILED" -BackgroundColor Red
}

If I set a break point on Compare-Object, and I run this command in console, I get the list of differences. If I run the whole script, I don't get any output. Why?

I'm working in PowerGUI Script Editor, but I tried the normal ps console too.

EDIT:

The problem is the check on the end of the script.

$i = Test-Diff $Dir1 $Dir2

if($i) { 
   Write-Output "Test OK" 
...

If I call Test-Diff without $i = check, it works!

Test-Diff returns with an array of objects and not with an expected bool value:

[DBG]: PS D:\>> $i | ForEach-Object { $_.GetType() } | Format-Table -Property Name
 Name                                                                                                                        
 ----                                                                                                                        
 FormatStartData                                                                                                             
 GroupStartData                                                                                                              
 FormatEntryData                                                                                                             
 GroupEndData                                                                                                                
 FormatEndData                                                                                                               
 Boolean         

If I comment out the line with Compare-Object, the return value is a boolean value, as expected.

The question is: why?

解决方案

I've found the answer here: http://martinzugec.blogspot.hu/2008/08/returning-values-from-fuctions-in.html

Functions like this:

Function bar {
 [System.Collections.ArrayList]$MyVariable = @()
 $MyVariable.Add("a")
 $MyVariable.Add("b")
 Return $MyVariable
}

uses a PowerShell way of returning objects: @(0,1,"a","b") and not @("a","b")

To make this function work as expected, you will need to redirect output to null:

Function bar {
 [System.Collections.ArrayList]$MyVariable = @()
 $MyVariable.Add("a") | Out-Null
 $MyVariable.Add("b") | Out-Null
 Return $MyVariable
}

In our case, the function has to be refactored as suggested by Koliat.

这篇关于PowerShell:函数没有正确的返回值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆