1

Well, i'm trying to execute a script with another credentials and i got the next error:

You cannot call a method on a null-valued expression.
At C:\Automation\SoftwareInstallation\runas_test\whoami_test.ps1:10 char:1
+ Write-Host $result.StandardOutput.ReadToEnd()
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

This is my code:

$username = 'idb\frida_automation_scc'
$pass = Get-Content -Path C:\Automation\SoftwareInstallation\runas_test\settings.txt -Raw

$password = ConvertTo-SecureString $pass -AsPlainText -Force
$MyCredential=New-Object -TypeName PSCredential -ArgumentList $username, $password 

$result=Start-Process -NoNewWindow powershell.exe -ArgumentList "-file C:\Automation\SoftwareInstallation\runas_test\whoami_test2.ps1" -Credential $MyCredential -PassThru -RedirectStandardOutput test1.txt

Write-Host $result.StandardOutput.ReadToEnd()

And this is the another command:

$result= whoami 

Write-Host $result

return $result

I don´t know why this error happens if the command are well written and the script brought back the value in a .txt file.

  • Does this answer your question? [You cannot call a method on a null-valued expression](https://stackoverflow.com/questions/27525170/you-cannot-call-a-method-on-a-null-valued-expression) – Daniel Mann Mar 07 '22 at 17:14
  • You can't redirect stdout _and_ read from it directly - either read the output back from the file, or remove the redirection. – Mathias R. Jessen Mar 07 '22 at 17:14
  • Got it, i actually validating if the value of the variables are empty. – Andrés Valero Mar 07 '22 at 17:27

0 Answers0