You cannot call a method on a null-valued expression.


hello,

working on cleanup of fileservers stuck on powershell code. code below.

$directories = get-content "location"   $output = @()   foreach($dir in $directories) {     $files = get-childitem -path $dir -filter "*.*" -recurse | { -not $_.psiscontainer }        $filessorted = $files| sort lastaccesstime -descending       $newestfile = $filessorted | select -first 1     $newestfiledate = $newestfile.lastwritetime       $oldestfile = $filessorted | select -last 1     $oldestfiledate = $oldestfile.lastwritetime       $outobj = "" | select dirname,newestfilename,newestfiledate,oldestfilename,oldestfiledate     $outobj.dirname = $dir     $outobj.newestfilename = $newestfile.name     $outobj.newestfiledate = $newestfiledate.tostring("dd.mm.yyyy hh:mm:ss")     $outobj.oldestfilename = $oldestfile.name     $outobj.oldestfiledate = $oldestfiledate.tostring("dd.mm.yyyy hh:mm:ss")       $output += $outobj }   $output | export-csv "location" -notypeinformation -delimiter ";" -encoding utf8

problem following:

you cannot call method on null-valued expression. @ line:12 char:5 +     $outobj.newestfiledate = $newestfiledate.tostring("dd.mm.yyyy hh:mm:ss") +     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~     + categoryinfo          : invalidoperation: (:) [], runtimeexception     + fullyqualifiederrorid : invokemethodonnull  cannot call method on null-valued expression. @ line:14 char:5 +     $outobj.oldestfiledate = $oldestfiledate.tostring("dd.mm.yyyy hh:mm:ss") +     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~     + categoryinfo          : invalidoperation: (:) [], runtimeexception     + fullyqualifiederrorid : invokemethodonnull  cannot call method on null-valued expression. @ line:12 char:5 +     $outobj.newestfiledate = $newestfiledate.tostring("dd.mm.yyyy hh:mm:ss") +     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~     + categoryinfo          : invalidoperation: (:) [], runtimeexception     + fullyqualifiederrorid : invokemethodonnull  cannot call method on null-valued expression. @ line:14 char:5 +     $outobj.oldestfiledate = $oldestfiledate.tostring("dd.mm.yyyy hh:mm:ss") +     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~     + categoryinfo          : invalidoperation: (:) [], runtimeexception     + fullyqualifiederrorid : invokemethodonnull


how can solve this?


this occur, if object $newestfiledate $null. presume, there no files in directory.

so make test if there files , then, run code:

$directories = get-content "location"   $output = @()   foreach($dir in $directories) {     $files = get-childitem -path $dir -filter "*.*" -recurse | { -not $_.psiscontainer }        if($files -ne $null)     {         $filessorted = $files| sort lastaccesstime -descending           $newestfile = $filessorted | select -first 1         $newestfiledate = $newestfile.lastwritetime           $oldestfile = $filessorted | select -last 1         $oldestfiledate = $oldestfile.lastwritetime           $outobj = "" | select dirname,newestfilename,newestfiledate,oldestfilename,oldestfiledate         $outobj.dirname = $dir         $outobj.newestfilename = $newestfile.name         $outobj.newestfiledate = $newestfiledate.tostring("dd.mm.yyyy hh:mm:ss")         $outobj.oldestfilename = $oldestfile.name         $outobj.oldestfiledate = $oldestfiledate.tostring("dd.mm.yyyy hh:mm:ss")           $output += $outobj     } }   $output | export-csv "location" -notypeinformation -delimiter ";" -encoding utf8




Windows Server  >  Windows PowerShell



Comments

Popular posts from this blog

CRL Revocation always failed

Failed to query the results of bpa xpath

0x300000d errors in Microsoft Remote Desktop client