Link to home
Start Free TrialLog in
Avatar of Joseph Moody
Joseph MoodyFlag for United States of America

asked on

GPResult Formatting

I am stuck on an issue. I have a script where it prompts for a computer name, looks at the current logged on user, and runs a gpresult.

My problem is when no user is logged in, it will try to do a gpresult for @{Username=} instead of only showing the computer results.

Can someone point me in the right direction?

$computer= Read-Host "What is the computer name?"
$User= Get-WmiObject Win32_Computersystem -ComputerName $computer | Select-Object Username

set-location "P:\Script Explorer\Scripts\Group Policy Results\"


if ($User -eq @{Username=}) {
gpresult.exe /scope COMPUTER /s $Computer /h Report.htm /f}

elseif {
gpresult.exe /s $Computer /user GCBE\$user /h Report.htm /f}

start report.htm

Open in new window

Avatar of Meir Rivkin
Meir Rivkin
Flag of Israel image

try this:

$computer= Read-Host "What is the computer name?"
$User= Get-WmiObject Win32_Computersystem -ComputerName $computer | Select-Object Username

set-location "P:\Script Explorer\Scripts\Group Policy Results\"


if ($User -ne $null -and $User -eq @{Username=}) {
gpresult.exe /scope COMPUTER /s $Computer /h Report.htm /f
}
else {
gpresult.exe /s $Computer /user GCBE\$user /h Report.htm /f
}

start report.htm

Open in new window

Avatar of Joseph Moody

ASKER

Thank you for the help!

I get this "Missing statement after '=' in hash literal." on line 7. I had this problem originally. When I quoted  @{Username=}, it didn't seem to work either. It would remove the error but the condition didn't evaluate correctly.
what @{Username=} suppose to be?
i dont think u need that if u get valid $user
If I run "Get-WmiObject Win32_Computersystem -ComputerName $computer | Select-Object Username" on a computer without someone logged in, the user returned is @{Username=}
ASKER CERTIFIED SOLUTION
Avatar of Meir Rivkin
Meir Rivkin
Flag of Israel image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
You helped me a lot! Thank you!
My final script is:

set-location "P:\Script Explorer\Scripts\Group Policy Results\"

$computer= Read-Host "What is the computer name?"
$User= Get-WmiObject Win32_Computersystem -ComputerName $computer | Select-Object Username
$Username=$User.Username

if ($Username -eq $null) {
Get-WmiObject Win32_NetworkLoginProfile -ComputerName $computer | Select-Object Caption
$InputUser=Read-Host "What user would you like to use?"
}


if ($Username -eq $null) {
gpresult.exe /s $Computer /user $InputUser /h Report.htm /f
}

else {
gpresult.exe /s "$Computer" /user "$Username" /h Report.htm /f
}

start report.htm 

Open in new window