Weird PowerShell error
Got a weird issue with a PowerShell tool in my PDQ server. The script is as follows
$computer = '$(Computer:TARGET)'
$host.ui.RawUI.WindowTitle = "Logon details of $Computer"
$ErrorActionPreference = 'SilentlyContinue'
$users = $false
$locked = $false
$users = (Get-WmiObject -Class win32_computersystem -ComputerName
$computer).username.Split("\")[1]
If ((Get-Process logonui -ComputerName $Computer) -and ($users))
{
$Locked = $True
}
foreach ($user in $users) {
$Output = New-Object PSObject
$Output | Add-Member noteproperty Computer $Computer
$Output | Add-Member noteproperty Username $User
$Output | Add-Member noteproperty Locked $Locked
$Output
}
Running this, I get the following error message:
You cannot call a method on a null-valued expression.
At C:\ProgramData\Admin
Arsenal\Temp\PDQInventoryConsole\9240\0c70b1e4-c5ba-4021-8b87-30fda7f784b7\97029f86-3b48-402e-b9ec-bdad13666cd5.ps1:7
char:1
+ $users = (Get-WmiObject -Class win32_computersystem -ComputerName $co ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : InvokeMethodOnNull
If I change the first line of the script from
$computer = '$(Computer:TARGET)'
To
$computer = Read-Host 'Enter computer name'
And then run it from ISE, I get the desired output
Computer Username Locked
-------- -------- ------
spare-01 False False
The server has been rebooted several times since. The issue was first noticed at some point during version 16, but this was part of a bigger issue, which I was told would be fixed in version 17 so didn't report this sooner. We've just upgraded to version 17, although the bigger issue that this was originally part of has been solved, this issue is still present.
Any suggestions on what I can do to resolve this issue?
Comments
Hello Craig,
In looking at this, I believe that the tool is ignoring the $ErrorActionPreference variable. I am still waiting to hear back from some others to verify, but I believe it is a bug in the software. Can you send an email to support@pdq.com to have us look into that more? If I find out anything else I will let you know. Thanks!