You cannot call a method on a null-valued expression


can script , tell me why the you cannot call method on null-valued expression error message? cannot see why happens

write-host -object "type computername server want list services`n" -foregroundcolor cyan  $computername = read-host  $computername = $computername.toupper()    write-host -object "`ndo want sort [1 = process, 2 = user, 3 = pid, 4 = process start date]`n" -foregroundcolor cyan  $sortoption = read-host    switch($sortoption)  {      1      {          $processes = get-ciminstance win32_process -computername $computername -erroraction silentlycontinue | sort-object processname          $sorttext = "process name"      }        2      {          $processes = get-ciminstance win32_process -computername $computername -erroraction silentlycontinue |                          foreach -erroraction silentlycontinue {                              $owner = invoke-cimmethod -inputobject $_ -methodname getowner -erroraction silentlycontinue                              $_ | add-member -notepropertyname owner -notepropertyvalue $owner.user -passthru -erroraction silentlycontinue                          } | sort-object owner          $sorttext = "owner"      }        3      {          $processes = get-ciminstance win32_process -computername $computername | sort-object processid          $sorttext = "processid"      }        4      {          $processes = get-ciminstance win32_process -computername $computername | sort-object creationdate          $sorttext = "creationdate"      }  }    write-host -object "`ndo want print screen or file? [press 1 screen, 2 file (will include process commandline in log)]`n" -foregroundcolor cyan  $userinput = read-host    # actions based on user input  switch($userinput)  {      # print screen      1      {          clear-host          write-host -object ("sorting $sorttext") -foregroundcolor yellow          write-host -object ("printing services $computername`n") -foregroundcolor white -backgroundcolor darkred          write-host -object ("{0,-15} {1,-65} {2,-10} {3,-20} {4}\{5}" -f "server name", "process name", "process id", "creationdate", "domain", "owner")            foreach($process in $processes)          {              $owner = invoke-cimmethod -inputobject $process -methodname getowner -erroraction silentlycontinue              write-host -object ("{0,-15} {1,-65} {2,-10} {3,-20} {4}\{5}" -f $process.csname, $process.name, $process.processid, $process.creationdate, $owner.domain.toupper(), $owner.user.toupper())          }      }        # print file, including commandline of service      2      {          clear-host          write-host -object "output log c:\temp\processes.txt" -foregroundcolor white -backgroundcolor darkred          out-file -filepath c:\temp\processes.txt -append -inputobject ("sorting $sorttext")          out-file -filepath c:\temp\processes.txt -append -inputobject ("{0,-15} {1,-65} {2,-10} {3,-120} {4,-20} {5}\{6}" -f "server name", "process name", "process id", "path", "creationdate", "domain", "owner")            foreach($process in $processes)          {              $owner = invoke-cimmethod -inputobject $process -methodname getowner -erroraction silentlycontinue              out-file -filepath c:\temp\processes.txt -inputobject ("{0,-15} {1,-65} {2,-10} {3,-120} {4,-20} {5}\{6}" -f $process.csname, $process.name, $process.processid, $process.path, $process.creationdate, $owner.domain.toupper(), $owner.user.toupper()) -append          }      }  }


freddy

found out because system , system idle process not have owner.

problem solved


freddy



Windows Server  >  Windows PowerShell



Comments

Popular posts from this blog

Error: 0x80073701 when trying to add Print Services Role in Windows 2012 Standard

Disconnecting from a Windows Server 2012 R2 file sharing session on a Windows 7,8,10 machine

Windows 2016 RDS event 1306 Connection Broker Client failed to redirect the user... Error: NULL