Solved

GPResult Formatting

Posted on 2013-01-28
7
843 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Why would I want to create a function for tracking messages? I am glad you asked. As with most monotonous/routine tasks, human error tends to creep in after doing the same task over and over again. By creating a function, you load the function once…
Hi all.   The other day I had to change the passwords for a bunch of users on the fly. Because they were so many, I decided to do it in an automated way and I would like to share it with you all.   If you are not doing it directly in a Domain Co…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

743 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