Function not Returning Data

╄→尐↘猪︶ㄣ 提交于 2019-12-11 14:45:15

问题


I am using PowerShell to compare the file count and size (per file extension) in 2 separate directories.

$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
    }
}

When using the ISE and calling dtop I get the correct return:

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

Yet when run in the script, it does not return any value. I have attempted to call a function write $tst which runs dtop and that does the same (writes null).

Furthermore, I have removed the { so it does not run as a function, and it operates without an issue. My concern is perhaps the -Path file cannot be parsed at the same time as the input - meaning: when I call dtop from ISE it already has the $Directory variable stored in memory.

Are there any obvious errors here? I am rather new to PowerShell and am unsure where the mistake lies.

来源:https://stackoverflow.com/questions/45838782/function-not-returning-data

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!