Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get-LoggedOnUser broken within Windows Sandbox due to truncated ComputerName environment variable #947

Closed
obuolinis opened this issue Apr 8, 2024 · 5 comments
Assignees
Labels
Type: Issue / Bug Something isn't working as expected

Comments

@obuolinis
Copy link

Describe the bug
When run from within Windows Sandbox Get-LoggedOnUser fails with the following error message:
Exception calling "GetUserSessionInfo" with "1" argument(s): "The operation completed successfully"

To Reproduce
Steps to reproduce the behavior:

  1. Run any PSADT package from within Windows Sandbox (tested on Windows 11 only)
  2. You get the error during the Initialization stage in the console and/or in the log.

Toolkit Version:
Tested with 3.9.3

Powershell Version:
5.1

Additional context
This has been bugging me for quite some time already since I really love using Windows Sandbox as my PSADT package testing environment.
According to my investigation this is related to both Windows and Windows Sandbox design. The root cause is that Get-LoggedOnUser is calling GetUserSessionInfo CS function using Windows environment variable ComputerName as the name of the host:

Write-Output -InputObject ([PSADT.QueryUser]::GetUserSessionInfo("$env:ComputerName"))

An actual computer name within Sandbox is a random GUID whereas ComputerName variable truncates it to 15 chars which is what breaks the function.

Fix Suggestion
This could possibly be a rather simple fix - you could be calling GetUserSessionInfo using DNSHostName property value of WMI class Win32_ComputerSystem rather than an environment variable. This property holds a full non-truncated computer name.
I'm not aware of any potential repercussions.

@coder-labeler coder-labeler bot added the Type: Issue / Bug Something isn't working as expected label Apr 8, 2024
@AlkHacNar
Copy link

AlkHacNar commented Jul 26, 2024

don't work with the 3.10.1 too, but you can fix/workaround it with you change $Env:ComputerName to $(hostname)

@mjr4077au
Copy link
Member

mjr4077au commented Jul 26, 2024

@AlkHacNar, does [System.Environment]::MachineName work as you'd expect?

@AlkHacNar
Copy link

no, its the same as $Env:ComputerName
image

@mjr4077au
Copy link
Member

Thanks mate, that's actually really good to know.

@AlkHacNar
Copy link

just tested (Get-WmiObject win32_computersystem).DNSHostName as @obuolinis mentioned and it works too in the sandbox
image

@DanGough DanGough self-assigned this Jul 29, 2024
DanGough added a commit that referenced this issue Jul 29, 2024
…HostName() instead of $env:ComputerName -Fixes #947 (Windows Sandbox)
sintaxasn pushed a commit that referenced this issue Jul 30, 2024
…HostName() instead of $env:ComputerName -Fixes #947 (Windows Sandbox)
mjr4077au pushed a commit that referenced this issue Aug 12, 2024
…HostName() instead of $env:ComputerName -Fixes #947 (Windows Sandbox)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Type: Issue / Bug Something isn't working as expected
Projects
None yet
Development

No branches or pull requests

4 participants