功能不返回数据

时间:2017-08-23 11:43:42

标签: function powershell powershell-v3.0

我使用PowerShell比较两个不同目录中的文件数量和大小(每个文件扩展名)。

$User = $env:username
$pwd = pwd

clear
write-host "`n"
write-host "`n"
write-host "`n"
write "The current user is: $User"
write-host "`n"
write "The current path is: $pwd"
write-host "`n"
write-host "`n"
write-host "`n"
write "We need to know the following information:"
write "`n"
write "`n"

$UserDesktopPath = Read-Host "New PC User Desktop Path"          # This should be the new PC Desktop Path
$UserDocumentPath = Read-Host "New PC User Document Path"        # This should be the new PC Document Path
$USBDesktopPathServer = Read-Host "USB User Desktop Path"       # This should be the USB User Desktop Path
$USBDocumentPathServer = Read-Host "USB User Document Path"      # This should be the USB User Document Path

clear
write-host "`n"
write-host "`n"
write-host "`n"
write "This is the results for your Desktop Folder Paths:"
write-host "`n"
    $folder_new = Get-ChildItem -Recurse -path "$USBDesktopPathServer"                                                         # Recurses the New PC Desktop
    $folder_old = Get-ChildItem -Recurse -path "$UserDesktopPath"                                                              # Recurses the USB Backup Desktop
    Compare-Object -ReferenceObject "$folder_new" -DifferenceObject "$folder_old"                                              # Compares the two folders for the path to identify discrepancies


write-host "`n"
write "This is the results for your Documents Folder Paths:"
write-host "`n"
write-host "`n"
write-host "`n"
    $folder_new1 = Get-ChildItem -Recurse -path "$UserDocumentPath"                                                               # Recurses the New PC Documents
    $folder_old1 = Get-ChildItem -Recurse -path "$USBDocumentPathServer"                                                          # Recurses the USB Backup Documents
    Compare-Object -ReferenceObject "$folder_new1" -DifferenceObject "$folder_old1"                                               # Compares the two folders for the path to identify discrepancies


write-host "`n"
write-host "`n"
write-host "`n"
write "Now we shall compare file sizes of your Documents:"
write-host "`n"
write-host "`n"
write-host "`n"
write-host "`n"

function doc{
    $DirectoryDocuments = "$USBDocumentPathServer", "$UserDocumentPath"
    foreach ($Directory in $DirectoryDocuments) {
        Get-ChildItem -Path $Directory -Recurse | 
        Where-Object {-not $_.PSIsContainer} |
        Tee-Object -Variable Files |
        Group-Object -Property Extension |
        Select-Object -Property @{
            n = "Directory"
            e = {$Directory}
            },
        @{
          n = "Extension"
                e = { $_.Name -replace '^\.' }
                },
            @{
                n = "Size (MB)"
                e={ [math]::Round( ( ( $_.Group | Measure-Object Length -Sum ).Sum / 1MB ), 2 ) }
                },
            Count

    $Files |
        Measure-Object -Sum -Property Length |
        Select-Object -Property @{
                n = 'Extension'
                e = { 'Total' }
                },
            @{
                n = 'Size (MB)'
                e = { [math]::Round( ( $_.Sum / 1MB ), 2 ) }
                },
            Count
    }
}

使用ISE并调用dtop时,我得到了正确的回复:

PS C:\Users\Michael Nancarrow> dtop

Directory                       Extension Size (MB) Count
---------                       --------- --------- -----
D:\Deployment Kit\Test\Desktop2 txt               0     1
                                Total             0     1
D:\Deployment Kit\Test\Desktop1 txt               0    11
                                Total             0    11

然而,当在脚本中运行时,它不会返回任何值。我试图调用运行write $tst的函数dtop并执行相同的操作(写入null)。

此外,我删除了{,因此它不会作为一个函数运行,并且它运行时没有问题。我担心的是,-Path文件可能无法与输入同时解析 - 这意味着:当我从ISE调用dtop时,它已经将$Directory变量存储在内存中。

这里有明显的错误吗?我对PowerShell很新,不确定错误在哪里。

0 个答案:

没有答案