The PowerShell Scripting and Toolmaking Book - Error completing exercise

This topic contains 5 replies, has 2 voices, and was last updated by  Ciaran.Kelly 3 months, 2 weeks ago.

  • Author
    Posts
  • #74027

    Ciaran.Kelly
    Participant

    Hi All
    I am thoroughly enjoying working my way (slowly) thrrough the above book but I am having a problem completing the chapter "An Interlude: Changing Your Approach".

    In the function "Get-UserHomeFolderInfo", there is line "$user = Get-ADUser @params". When i run that function I get an error "Get-ADUser : Cannot find an object with identity: 'administrator.HOME' under: 'DC=home", which is unexpected. Any ideas why I am getting this and not the expected behaviour?

    function Get-FolderSize {
        [CmdletBinding()]
        Param(
            [Parameter(Mandatory=$True,
                       ValueFromPipeline=$True,
                       ValueFromPipelineByPropertyName=$True)]
            [string[]]$Path
        )
        BEGIN {}
        PROCESS {
            ForEach ($folder in $path) {
                Write-Verbose "Checking $folder"
                if (Test-Path -Path $folder) {
                    Write-Verbose " + Path exists"
                    $params = @{'Path'=$folder
                                'Recurse'=$true
                                'File'=$true}
                    $measure = Get-ChildItem @params |
                               Measure-Object -Property Length -Sum
                    [pscustomobject]@{'Path'=$folder
                                      'Files'=$measure.count
                                      'Bytes'=$measure.sum}
                } else {
                    Write-Verbose " - Path does not exist"
                    [pscustomobject]@{'Path'=$folder
                                      'Files'=0
                                      'Bytes'=0}
                } #if folder exists
            } #foreach
        } #PROCESS
        END {}
    } #function
    
    function Get-UserHomeFolderInfo {
        [CmdletBinding()]
        Param(
            [Parameter(Mandatory=$True)]
            [string]$HomeRootPath
        )
        BEGIN {}
        PROCESS {
            Write-Verbose "Enumerating $HomeRootPath"
            $params = @{'Path'=$HomeRootPath
                        'Directory'=$True}
            ForEach ($folder in (Get-ChildItem @params)) {
                
                Write-Verbose "Checking $($folder.name)"
                $params = @{'Identity'=$folder.name
                            'ErrorAction'='SilentlyContinue'}
                $user = Get-ADUser @params
    
                if ($user) {
                    Write-Verbose " + User exists"
                    $result = Get-FolderSize -Path $folder.fullname
                    [pscustomobject]@{'User'=$folder.name
                                      'Path'=$folder.fullname
                                      'Files'=$result.files
                                      'Bytes'=$result.bytes
                                      'Status'='OK'}
                } else {
                    Write-Verbose " - User does not exist"
                    [pscustomobject]@{'User'=$folder.name
                                      'Path'=$folder.fullname
                                      'Files'=0
                                      'Bytes'=0
                                      'Status'="Orphan"}
                } #if user exists
    
            } #foreach
        } #PROCESS
        END {}
    }
    

    Powershell cmdline output:

    PS C:\Dev\Powershell> Get-UserHomeFolderInfo -verbose

    cmdlet Get-UserHomeFolderInfo at command pipeline position 1
    Supply values for the following parameters:
    HomeRootPath: c:\users
    VERBOSE: Enumerating c:\users
    VERBOSE: Checking Administrator
    VERBOSE: + User exists
    VERBOSE: Checking C:\users\Administrator
    VERBOSE: + Path exists

    User : Administrator
    Path : C:\users\Administrator
    Files : 189
    Bytes : 7623892
    Status : OK

    : Checking administrator.HOME
    ser : Cannot find an object with identity: 'administrator.HOME' under: 'DC=home,DC=kelly,DC=za
    rogram Files\WindowsPowerShell\Modules\FolderInfo\FolderInfo.psm1:50 char:21
    $user = Get-ADUser @params
    ~~~~~~~~~~~~~~~~~~
    ategoryInfo : ObjectNotFound: (administrator.HOME:ADUser) [Get-ADUser], ADIdentityNot
    ullyQualifiedErrorId : ActiveDirectoryCmdlet:Microsoft.ActiveDirectory.Management.ADIdentityNo
    soft.ActiveDirectory.Management.Commands.GetADUser

    : + User exists
    : Checking C:\users\administrator.HOME
    : + Path exists
    : administrator.HOME
    : C:\users\administrator.HOME
    : 287
    : 12951945
    : OK

  • #74029

    Frank Tucker
    Participant

    There is no user named administrator.home on the domain home.

    There's just a folder administrator.home
    The folder names under users do not always match a users name. Rename an old user and notice the folder name does not change.

    I'm guessing there is a folder for a local user named administrator and one for a domain user home\administrator.

    Get-aduser can't find a user that is not there.

    The next part of the function is just checking that the folder named administrator.home exist.

    IF there is any thing stored in $user it checks the folder.

  • #74042

    Ciaran.Kelly
    Participant

    Hi Frank, thx for answering

    When I look at the code, I am expecting that if the user Administrator.home is not found, I would get a messages saying "- User does not exist":

          if ($user) {
                    Write-Verbose " + User exists"
                    $result = Get-FolderSize -Path $folder.fullname
                    [pscustomobject]@{'User'=$folder.name
                                      'Path'=$folder.fullname
                                      'Files'=$result.files
                                      'Bytes'=$result.bytes
                                      'Status'='OK'}
                } else {
                    Write-Verbose " - User does not exist"
                    [pscustomobject]@{'User'=$folder.name
                                      'Path'=$folder.fullname
                                      'Files'=0
                                      'Bytes'=0
                                      'Status'="Orphan"}
                } #if user exists
    

    Instead of getting alot of ugly red text and then a messaging saying the user does exist and the folder size for administrator.Home. Am I misunderstanding something?

  • #74179

    Frank Tucker
    Participant

    There is data in $user so the Else statement is not being used.

     function just-test{
        $user ="xyz"
        $user = get-aduser -identity $user -erroraction silentlyContinue
        
        if ($user){
            write-host $user "still has the value of xyz"    
        }
        else{
            write-host $user "the else is not going to run because $user has data"
        }
    }
    
  • #74183

    Frank Tucker
    Participant
    function just-test{
        $user ="xyz"
        $aduser = get-aduser -identity $user -erroraction silentlyContinue
        
        if ($aduser){
            write-host $user "exists"   
        }
        else{
            write-host $user "does not exists"
        }
    }
    
    • #74209

      Ciaran.Kelly
      Participant

      Thx Frank, I get it now – I had to reread it a few times but I got there in the end!
      I had the crazy assumption that because the identity "administrator.home" coud not be found then
      the variable "$user" would not contain any value 🙂

You must be logged in to reply to this topic.