Avatar of haz sh
haz sh

asked on 

PowerShell Formatting

I am running a script to query AD. My output is like below. I want the results to display as in column and rows. Please assist


Name           ADSPath                                                     lastLogonTimestamp
----           -------                                                     ------------------
HAZ-DC         LDAP://CN=HAZ-DC,OU=Domain Controllers,DC=haz,DC=local      2020-03-19 3:08...


$Result = "" | Select-Object Name,ADSPath,lastLogonTimestamp
            $Result.Name = [String]$Account.Properties.name
            $Result.ADSPath = [String]$Account.Properties.adspath
            $Result.lastLogonTimestamp = [DateTime]::FromFileTime([Int64]::Parse($Account.Properties.lastlogontimestamp))
            $Results = $Results + $Result

# Output the results
$Results | Out-File C:\test\PCLogon.CSV
Powershell

Avatar of undefined
Last Comment
oBdA
ASKER CERTIFIED SOLUTION
Avatar of oBdA
oBdA

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of haz sh
haz sh

ASKER

It is the final result. How can I save Out-GridView into a file 
Avatar of oBdA
oBdA

You can't; that's a form only.
You can save it as a csv and then open it in Excel, but you'll need -NoTypeInformation switch:
$Results | Export-Csv -Path C:\test\PCLogon.CSV -NoTypeInformation 

Open in new window

Avatar of haz sh
haz sh

ASKER

I think you can help me further. I am using this script to get list of everything from AD. It includes Server and Workstations. I want to limit the search to everything except servers 

# Calculate the UTC time, in FileTime (Integer) format and convert it to a string
$LLTSlimit = (Get-Date).ToFileTimeUTC().ToString()

# Create the LDAP filter for the AD query
# Searching for ***enabled*** computer accounts which have lastLogonTimestamp
$LDAPFilter = "(&(objectCategory=Computer)(lastLogonTimestamp<=$LLTSlimit) (!(userAccountControl:1.2.840.113556.1.4.803:=2)))"

# Create an ADSI Searcher to query AD
$Searcher = new-object DirectoryServices.DirectorySearcher([ADSI]"")
$Searcher.PageSize = 6000
$Searcher.filter = $LDAPFilter

# Execute the query
$Accounts = $Searcher.FindAll()
Avatar of oBdA
oBdA

Any specific reason you're not using the AD cmdlets?
Get-ADComputer -Filter "operatingSystem -like '*Server*'" -ResultSetSize $null

Open in new window

Avatar of haz sh
haz sh

ASKER

Not really. I was given this to execute. Can that script be modified to exclude servers.
Or How to fetch all the inventory dump of objects in  AD except servers with their last logon date and Location, OU etc 
Avatar of oBdA
oBdA

Get-ADComputer -Filter "operatingSystem -like '*Server*'" -ResultSetSize $null -Properties location, lastLogonTimeStamp |
	Select-Object -Property Name, Location, @{n='LastLogon'; e={[DateTime]::FromFileTime($_.lastLogonTimeStamp)}}, DistinguishedName |
	Export-Csv -Path C:\test\PCLogon.CSV -NoTypeInformation 

Open in new window

Avatar of haz sh
haz sh

ASKER

Thanks OBdA. With the script which I am using can we exclude server ou ?
Avatar of oBdA
oBdA

Sorry, the above would only find servers. To find non-Server OS, use -notlike in the Filter:
Get-ADComputer -Filter "operatingSystem -notlike '*Server*'" -ResultSetSize $null -Properties location, lastLogonTimeStamp |
	Select-Object -Property Name, Location, @{n='LastLogon'; e={[DateTime]::FromFileTime($_.lastLogonTimeStamp)}}, DistinguishedName |
	Export-Csv -Path C:\test\PCLogon.CSV -NoTypeInformation 

Open in new window

Avatar of haz sh
haz sh

ASKER

This is the output

Get-ADComputer : The term 'Get-ADComputer' is not recognized as the name of a cmdlet,
function, script file, or operable program. Check the spelling of the name, or if a path was
included, verify that the path is correct and try again.
At line:1 char:1
+ Get-ADComputer -Filter* "operatingSystem -notlike '*Server*'" -Result ...
+ ~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (Get-ADComputer:String) [], CommandNotFoundExc
   eption
    + FullyQualifiedErrorId : CommandNotFoundException
Avatar of oBdA
oBdA

If you're on a server, run one of these:
# Full set of AD Management Tools:
Install-WindowsFeature -Name RSAT-AD-Tools
# PowerShell AD Management Tools only:
Install-WindowsFeature -Name RSAT-AD-PowerShell

Open in new window

On Windows 10, run
Add-WindowsCapability -Online -Name 'Rsat.ActiveDirectory.DS-LDS.Tools~~~~0.0.1.0'

Open in new window

Powershell
Powershell

Windows PowerShell is a task automation and configuration management framework from Microsoft, consisting of a command-line shell and associated scripting language built on the .NET Framework. PowerShell provides full access to the Component Object Model (COM) and Windows Management Instrumentation (WMI), enabling administrators to perform administrative tasks on both local and remote Windows systems as well as WS-Management and Common Information Model (CIM) enabling management of remote Linux systems and network devices.

27K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo