?
Solved

GPResult Formatting

Posted on 2013-01-28
7
Medium Priority
?
909 Views
Last Modified: 2013-01-28
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

0
Comment
Question by:Joseph Moody
  • 4
  • 3
7 Comments
 
LVL 42

Expert Comment

by:Meir Rivkin
ID: 38826852
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

0
 
LVL 22

Author Comment

by:Joseph Moody
ID: 38826863
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.
0
 
LVL 42

Expert Comment

by:Meir Rivkin
ID: 38826872
what @{Username=} suppose to be?
i dont think u need that if u get valid $user
0
What Security Threats Are We Predicting for 2018?

Cryptocurrency, IoT botnets, MFA, and more! Hackers are already planning their next big attacks for 2018. Learn what you might face, and how to defend against it with our 2018 security predictions.

 
LVL 22

Author Comment

by:Joseph Moody
ID: 38826891
If I run "Get-WmiObject Win32_Computersystem -ComputerName $computer | Select-Object Username" on a computer without someone logged in, the user returned is @{Username=}
0
 
LVL 42

Accepted Solution

by:
Meir Rivkin earned 2000 total points
ID: 38826950
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.Username -ne '') {
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

0
 
LVL 22

Author Closing Comment

by:Joseph Moody
ID: 38827278
You helped me a lot! Thank you!
0
 
LVL 22

Author Comment

by:Joseph Moody
ID: 38827281
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

0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A brief introduction to what I consider to be the best editor for PowerShell.
The following article is intended as a guide to using PowerShell as a more versatile and reliable form of application detection in SCCM.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Loops Section Overview

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question