Friday, February 13, 2015

"Telnet" in PowerShell Style

I guess that I'm not the only one that had a connection test to perform and found out that the Telnet Client is not installed on the system.

And again, Powershell to the rescue...

I decided to write a small PowerShell function to perform the same concavity test as Telnet will, and in order to perform this test we will leverage the System.Net.Sockets.TcpClient .NET object,
You may read some more about it at:
https://msdn.microsoft.com/en-us/library/system.net.sockets.tcpclient%28v=vs.110%29.aspx

And now for the function:
 
# PowerShell PSTelnet function

function PSTelnet([string]$Destination, [int]$Port) {
  
  # Create a TcpClient .NET object  
  $TCPClient = New-Object System.Net.Sockets.TcpClient
    
    # Try & Catch
    try {
          $TCPClient.Connect($Destination, $Port)
        } # END try
    catch [System.Net.Sockets.SocketException] {
            Write-Host -ForegroundColor Red "`nPSTelnet failed to connect with the following error:`n"
            Write-Output -ForegroundColor Red $Error.Item(0)
          } #END cache
  
  # Test if the connection established
  if ($TCPClient.Connected) {
    Write-Host -ForegroundColor Green "`nPSTelnet Successfully Connected"
    } # END if

  # Close and reset the connection
  $TCPClient.Close()

} # END of function

### Example of usage

PSTelnet -Destionation 127.0.0.1 -Port 445

### Example of output

PSTelnet Successfully Connected

Hope this helps,

Enjoy.

Monday, January 5, 2015

"Ping" in PowerShell style

Hi,

This PowerShell script will retrieve the IP address of a remote computer on the network,
You may provide a list of Hostnames that the script will go through.

# Set Error Action to Silently Continue
# This is in order to continue without any error in case you encounter one
$ErrorActionPreference = "SilentlyContinue"

# Get a list of computers from a TXT file
$list = Get-Content C:\Temp\Hosts.txt


# Create Ping .Net object
$Ping = New-Object System.Net.NetworkInformation.Ping

# Global var to contain all results
$BigResult = `
foreach ($l in $list) {

$Result = "" | Select-Object -Property Hostname, IP

$PingResult = ""
    
    # Try & Catch
    try {
    $PingResult = $Ping.Send($l)
    } 
        catch [System.Net.NetworkInformation.PingException]
        {
            $PingResult = ""
        } # END Try & Catch

# Check the PingResult var for successfull ping
if ($PingResult.Status -eq "Success") {
    
    $Result.IP = $PingResult.Address
    $Result.Hostname = $l

} else {
        
        $Result.IP = "Failed To Retrieve"
        $Result.Hostname = $l

    }

$Result

} # END foreach loop

# Display Output To Screen
$BigResult

# Save Output To CSV File
$BigResult | Export-Csv C:\Temp\IP.csv

Enjoy!

Thursday, December 18, 2014

Get local admins remotely using PowerShell

Small script that I've created to retrieve local admins users remotely from a list of computers.

######################

$List = "127.0.0.1", "localhost"

### This can be change to $List = Get-Content C:\ComputerList.txt
### In order to include a large list of computers to go through them

######################

$Results = foreach ($l in $list)
{
    
    $Result = "" | Select-Object -Property ComputerName, LocalAdmin
    $Result.ComputerName = $l
    $AllUsers = Get-WmiObject -Class win32_groupuser -ComputerName $l -ErrorAction 'SilentlyContinue'
    $Local_Administrators = $AllUsers | Where { $_.GroupComponent -match 'administrators' }
    $Result.LocalAdmin = ""
    $Result.LocalAdmin = $Local_Administrators | ForEach-Object {`
        $PartComponent = $_.PartComponent;
        $output = $PartComponent.LastIndexOf('=');
        $output = $PartComponent.Substring($output);
        $output = $output.Substring(2);
        $output.TrimEnd('"')
    }
    $LocaAdminsCount = $Result.LocalAdmin.Count
    $ConvertToCSV = ""
    for ($i = 0; $i -le $LocaAdminsCount; $i++)
    {
        if ($i -lt ($LocaAdminsCount - 1))
        {
            $ConvertToCSV += $Result.LocalAdmin[$i] + ", "
        }
        else
        {
            $ConvertToCSV += $Result.LocalAdmin[$i]
        } # END IF
    } # END for loop
    $Result.LocalAdmin = $ConvertToCSV
    $Result
    
} # END foreach loop

$Results # Just display the result to the screen
$Results | Export-Csv $env:TEMP\LocalAdmin.csv

### Change the output location to whatever you would like

######################

Enjoy :-)