Showing posts with label Reports. Show all posts
Showing posts with label Reports. Show all posts

Tuesday, August 4, 2015

Powershell Script - Get-LocalUsers - Remotely query all local users and details

Here's a lovely script that runs with parallel pipeline queries using the [ADSI] .NET class to remotely query SAM to build nice object outputs which are great for security audits.  Here's some sample output:

PS C:\> 'Server1','Server2' | Get-LocalUsers


Server          : Server1
UserName        : Administrator
Active          : True
PasswordExpired : False
PasswordAgeDays : 16
LastLogin       : 6/3/2015 6:34:27 PM
Groups          : Administrators
Description     : Built-in account for administering the computer/domain

Server          : Server1
UserName        : Guest
Active          : False
PasswordExpired : False
PasswordAgeDays : 0
LastLogin       : 
Groups          : Guests
Description     : Built-in account for guest access to the computer/domain

Server          : Server2
UserName        : Administrator
Active          : True
PasswordExpired : False
PasswordAgeDays : 1
LastLogin       : 3/5/2015 7:28:14 PM
Groups          : Administrators
Description     : Built-in account for administering the computer/domain

Server          : Server2
UserName        : Guest
Active          : False
PasswordExpired : False
PasswordAgeDays : 0
LastLogin       : 
Groups          : Guests
Description     : Built-in account for guest access to the computer/domain


PS C:\> 

And the code:

Wednesday, July 22, 2015

Powershell Script - Get-ServerHDDinfo - Remotely pull hdd size info quickly

I wrote two versions of this function for different scenarios.  The first version is much faster but requires PS remoting enabled on all target servers.  the second version will work with any server with remote WMI enabled which means it will support older 2003 servers much more easily.

The core of the functions is the same: pull the Win32_Volume WMI class and build a report from hdd information.  The first function remotely executes a script block which builds it's own report and then sends it back to the calling computer, while the second version calls each computer's WMI directly and builds one report for all servers.  Even with Get-WmiObject's ability to query multiple servers at once, it still can't match the speed of Invoke-Command, which leverages the faster CIM framework.


Version 1 - Much faster Invoke-Command Version


Version 2 - Slower WMI

Sunday, June 28, 2015

Powershell Script - Compare-SourceFiles - source code comparison between environments (prod, bcp, qa, dev)

I just built this little script this weekend while troubleshooting a release. We needed a way to compare the code between different environments as well as compare folder permission security within environments on different nodes in the farm. This function does exactly that, producing a nice report (use -ReportFile to output to a file as well) used to prevent change drift and environment variance. If the files are text files, a line by line comparison is also generated should the file hashes be different, which shows specific lines that are different. Binary files are still flagged as different should their MD5 hashes be different. When checking ACLs, effective file permissions are displayed for each source which can speed up troubleshooting permissions issues.


PS c:\> Compare-SourceFiles -Source1 \\server1\c$\prod -Source2 \\server2\c$\dev -ACLs

WARNING: Files missing from '\\server1\c$\prod': 3
\FrameXML.log
\gx.log
\Sound.log



WARNING: File Hash different: '\a.exe'


WARNING: ACL Access Permissions different: '\a.exe'
   File Permissions: '\\server1\c$\prod\a.exe'

FileSystemRights AccessControlType IdentityReference      IsInherited InheritanceFlags PropagationFlags
---------------- ----------------- -----------------      ----------- ---------------- ----------------
     FullControl             Allow BUILTIN\Administrators       False             None             None
     FullControl             Allow NT AUTHORITY\SYSTEM          False             None             None
     FullControl             Allow DOMAIN\User1                 False             None             None



   File Permissions: '\\server2\c$\dev\a.exe'

           FileSystemRights AccessControlType IdentityReference                IsInherited InheritanceFlags PropagationFlags
           ---------------- ----------------- -----------------                ----------- ---------------- ----------------
                FullControl             Allow BUILTIN\Administrators                 False             None             None
ReadAndExecute, Synchronize             Allow Everyone                               False             None             None
        Modify, Synchronize             Allow NT AUTHORITY\Authenticated Users       False             None             None
                FullControl             Allow NT AUTHORITY\SYSTEM                    False             None             None





WARNING: File Hash different: '\connection.log'

InputObject                                                                                    SideIndicator
-----------                                                                                    -------------
2/16 16:02:39.744  GRUNT: state: LOGIN_STATE_CONNECTING result: LOGIN_OK                       =>                  
2/16 16:02:39.946  GRUNT: state: LOGIN_STATE_AUTHENTICATING result: LOGIN_OK                   =>           
2/16 16:02:40.128  GRUNT: state: LOGIN_STATE_CHECKINGVERSIONS result: LOGIN_OK                 =>           
2/16 16:02:40.205  GRUNT: state: LOGIN_STATE_HANDSHAKING result: LOGIN_OK                      =>           
2/16 16:02:40.375  GRUNT: state: LOGIN_STATE_AUTHENTICATED result: LOGIN_OK                    =>           
2/16 16:02:40.375  ClientConnection Initiating: COP_CONNECT code=CSTATUS_CONNECTING            =>           
2/16 16:02:40.708  ClientConnection Completed: COP_CONNECT code=RESPONSE_CONNECTED result=TRUE =>           
2/16 16:02:40.724  ClientConnection Initiating: COP_AUTHENTICATE code=CSTATUS_AUTHENTICATING   =>           
2/16 16:02:41.311  ClientConnection Completed: COP_AUTHENTICATE code=AUTH_OK result=TRUE       =>           
2/16 16:02:41.780  ClientConnection Initiating: COP_GET_CHARACTERS code=43                     =>           
2/16 16:02:42.286  ClientConnection Completed: COP_GET_CHARACTERS code=44 result=TRUE          =>           
2/16 16:02:59.661  GRUNT: state: LOGIN_STATE_DISCONNECTED result: LOGIN_OK                     =>           
2/16 16:03:03.816  ClientConnection Initiating: COP_LOGIN_CHARACTER code=77                    =>           
2/16 16:03:04.237  ClientConnection Completed: COP_LOGIN_CHARACTER code=78 result=TRUE         =>           
2/16 16:01:53.409  GRUNT: state: LOGIN_STATE_CONNECTING result: LOGIN_OK                       <=           
2/16 16:02:14.449  GRUNT: state: LOGIN_STATE_FAILED result: LOGIN_CONVERSION_REQUIRED          <=           
2/16 16:02:14.453  GRUNT: state: LOGIN_STATE_FAILED result: LOGIN_CONVERSION_REQUIRED          <=           





WARNING: File Hash different: '\cpu.log'

InputObject                                                                         SideIndicator
-----------                                                                         -------------
2/16 16:02:30.896  vendor: 1                                                        =>           
2/16 16:02:30.896  features: 00000397                                               =>           
2/16 16:02:30.896  cores: 2                                                         =>           
2/16 16:02:30.896  threads: 4                                                       =>           
2/16 16:02:30.896  vendor id string= GenuineIntel                                   =>           
2/16 16:02:30.896  standard (13): 1b=02100800 1c=7FDAFBBF 1d=bfebfbff 4a=1C004121   =>           
2/16 16:02:30.896  extended (8): 1c=00000021 1d=2c100000 8c=00000000                =>           
2/16 16:02:30.896  processor brand string= Intel(R) Core(TM) i5-4670K CPU @ 3.40GHz =>           
2/16 16:01:42.716  vendor: 1                                                        <=           
2/16 16:01:42.716  features: 00000397                                               <=           
2/16 16:01:42.716  cores: 2                                                         <=           
2/16 16:01:42.716  threads: 4                                                       <=           
2/16 16:01:42.716  vendor id string= GenuineIntel                                   <=           
2/16 16:01:42.716  standard (13): 1b=00100800 1c=7FDAFBBF 1d=bfebfbff 4a=1C004121   <=           
2/16 16:01:42.716  extended (8): 1c=00000021 1d=2c100000 8c=00000000                <=           
2/16 16:01:42.716  processor brand string= Intel(R) Core(TM) i5-4670K CPU @ 3.40GHz <=           






==============================
File Source Comparison Summary
==============================

Path              FileCount MissingFiles SharedFiles DifferentHashes DifferentACLs
----              --------- ------------ ----------- --------------- -------------
\\server1\c$\prod         3            3           3               3             1
\\server2\c$\dev          6            0           3               3             1



Thursday, June 25, 2015

Powershell Script - Get-LastBootTime - remotely find out when your servers where last rebooted

Here's another handy and simple function which lets you query a bunch of servers at once to find out when they were last rebooted.  It's taking advantage of Get-WmiObject and the root\civ2\Win32_OperatingSystem class.  For maximum parallelization, I've forced pipeline usage to first colapse the process block into a single array to create only one Get-WmiObject call.  This is significantly faster than the default pipeline behavior which is sequential, with 1000 servers completing under a minute (local LAN) vs sequential calls taking close to 10 minutes.

Here's it in action:

PS C:\> Get-Content .\serverlist.txt | Get-LastBootTime

Name      UpSince
----      -------
NOTSERVER Unknown             
SERVER001 6/20/2015 10:46:25 PM
SERVER002 6/20/2015 10:26:24 PM
SERVER003 6/20/2015 10:30:17 PM
SERVER004 6/20/2015 10:27:52 PM
SERVER005 6/20/2015 10:38:27 PM
SERVER006 6/20/2015 10:30:12 PM
SERVER007 6/20/2015 11:54:01 PM
SERVER008 6/20/2015 10:28:39 PM
SERVER009 6/20/2015 10:26:15 PM
SERVER010 6/20/2015 10:27:01 PM

Source Code:

Wednesday, June 24, 2015

Powershell Script - ConvertTo-HtmlTable - making pretty html formatted tables for reports and emails.

Here's a nifty utility function I built long ago that really makes emailing reports or rendering web reports easy as pie (cake is a lie). You can even change the font color, background color, border color, border width, and cell padding around the text.


Here's a little example which displays the current directory contents and opens up your browser for you after the report's finished.

dir . | select Name,Length,Mode,LastWritetime | ConvertTo-HtmlTable | Set-Content .\test.html
Invoke-Item .\test.html

And the output in your browser: