Solved

GPResult Formatting

Posted on 2013-01-28
7
848 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:sedgwick
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:sedgwick
ID: 38826872
what @{Username=} suppose to be?
i dont think u need that if u get valid $user
0
Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 
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:
sedgwick earned 500 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

The article will show you how you can maintain a simple logfile of all Startup and Shutdown events on Windows servers and desktops with PowerShell. The script can be easily adapted into doing more like gracefully silencing/updating your monitoring s…
A brief introduction to what I consider to be the best editor for PowerShell.
I designed this idea while studying technology in the classroom.  This is a semester long project.  Students are asked to take photographs on a specific topic which they find meaningful, it can be a place or situation such as travel or homelessness.…
Delivering innovative fully-managed cloud services for mission-critical applications requires expertise in multiple areas plus vision and commitment. Meet a few of the people behind the quality services of Concerto.

932 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now