Solved

GPResult Formatting

Posted on 2013-01-28
7
881 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.
Auditing domain password hashes is a commonly overlooked but critical requirement to ensuring secure passwords practices are followed. Methods exist to extract hashes directly for a live domain however this article describes a process to extract u…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…
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 antispam), the admini…

732 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