Showing posts with label windows. Show all posts
Showing posts with label windows. Show all posts

Aug 10, 2018

Telnet on Windows with no Telnet Client using Powershell

If you do not have telnet client installed on your machine, you can use PowerShell to run a telnet command as shown below:

$Computer= "<server-name>";
$Port = <port-number>;
$nl = [Environment]::NewLine

Write-Host "$nl Trying to telnet to '$Computer' on Port: '$Port'$nl"
Try {
    $tcp = New-Object System.Net.Sockets.TcpClient
    $tcp.connect($Computer, $Port)
    Write-Host "Successfully connected to $Computer on Port: $Port"$nl -ForegroundColor Green
    $tcp.close() 
}
Catch {
    Write-Host "Could not connect to $Computer on Port: $Port"$nl -ForegroundColor Red
    Write-Host $_.Exception.Message -ForegroundColor Red
}

Mar 30, 2017

OpenSSL | Creating Certification Authority & Domain Certificates for IIS

Setup

Download the OpenSSL binaries from this location.
(Please note that version 1.0.2 works better in windows)

Extract the binaries at location:
C:\OpenSSL\
The binaries will be downloaded in bin folder. Now create a demo folder at this location

Add the following location to windows PATH variable.
C:\OpenSSL\bin
Create 2 Environment Variables:
set RANDFILE=C:\OpenSSL\demo\.rnd
set OPENSSL_CONF=C:\OpenSSL\bin\openssl.cfg

Open Command Prompt and type openssl. You should get a window like this:





This means that the setup is correct and complete. Else fix your windows PATH variable. Close this window.



Create a Root Certification Authority

Now we need to create a certificate for Root Certification Authority. Open Command Prompt and change location to demo folder using command:
cd C:\OpenSSL\demo

Create key as (location: C:\OpenSSL\demo):
openssl genrsa -out RootCA.key 4096

Use this key to create a Root Certificate using command:
openssl req -new -x509 -days 7300 -key RootCA.key -out RootCA.crt
The certificate expiration is set to 20 years (7300 days). Change to match your requirements.

You will have to enter details as:

The certificate name is the input entered against field: Common Name
Please note that if you press enter without entering a value, the default value (value displayed in []) will be used to create certificate. The certificate RootCA.crt will be created at location:
C:\OpenSSL\demo
This certificate will have to be installed in Trusted Root Certification Authority in any machine which the website is installed or used.



Create a Domain Certificate

Now we need to create a domain certificate. Create a folder contoso at location:
C:\OpenSSL\demo
Create a file csr_detail.cnf using the details below in contoso folder:
[req]
distinguished_name = req_distinguished_name
req_extensions = v3_req

[req_distinguished_name]
countryName = US
countryName_default = US
stateOrProvinceName = New York
stateOrProvinceName_default = NY
localityName = New York
localityName_default = New York
organizationalUnitName = Contoso Unit
organizationalUnitName_default = Contoso Unit
commonName = contoso.com
commonName_default = contoso.com
organizationName = Contoso
organizationName_default = Contoso
commonName_max  = 64

[ v3_req ]
# Extensions to add to a certificate request
basicConstraints = CA:FALSE
keyUsage = nonRepudiation, digitalSignature, keyEncipherment
subjectAltName = @alt_names

[alt_names]
DNS.1 = www.contoso.com
DNS.2 = files.contoso.com
IP.1 = 0.0.0.0
Edit it to match your needs.

Open Command Prompt and change location to contoso folder using command:
cd C:\OpenSSL\demo\contoso 

Create a KEY for the certificate using the command:
openssl genrsa -out contoso.com.key 2048

Create a CSR file for the certificate using the command:
openssl req -new -out contoso.com.csr -key contoso.com.key -config csr_details.cnf
You will be asked to input fields as:
Press enter in each field to add default value already set from CNF file.


Create a certificate using the CNF file, KEY and the CSR file using the command:
openssl x509 -req -days 3650 -in contoso.com.csr -CA ..\RootCA.crt -CAkey ..\RootCA.key -set_serial 01  -extensions v3_req -extfile csr_details.cnf -out contoso.com.crt
This certificate will be created as contoso.com.crt at location:
C:\OpenSSL\demo\contoso

Now we need to create a PFX file so that this certificate can be imported in IIS. Use the following command:
openssl pkcs12 -export -out contoso.com.pfx -inkey contoso.com.key -in contoso.com.crt
You will be asked to enter a password (at least 4 characters) as:




Import this PFX file in IIS and use it in both the domains: www.contoso.com & files.contoso.com


Oct 31, 2015

Change Creation Date of file to Modified Date using PowerShell

Use the following PowerShell command to change the Creation Date to the Modified Date of all files in a folder.

Get-ChildItem -recurse -filter *.JPG | % { $_.CreationTime = $_.LastWriteTime }

Oct 8, 2015

Rename all files replacing a text using PowerShell

Let's say you have a number of files at location C:\demo with text "OldValue" in it. And you want to replace text "OldValue" to "NewValue" in all files. To do that follow the steps below:

  1. Open Windows PowerShell.
  2. Navigate to location C:\demo
  3. Run the following Windows PowerShell command: 
    Get-ChildItem -Filter "*OldValue*" -Recurse | Rename-Item -NewName {$_.name -replace 'OldValue','NewValue' }
  4. All files will be renamed.

  • The command Get-ChildItem -Filter "*OldValue*" finds all files with the string "OldValue" anywhere in the name.
  • Get-ChildItem searches recursively through all subfolders.
  • | (the pipe character) instructs Windows PowerShell to take each item (object) found with the first command (Get-ChildItem) and pass it to the second command (Rename-Item)
  • Rename-Item renames files and other objects.
  • {$_.name -replace 'OldValue','NewValue' } instructs Rename-Item to find the string "OldValue" in the file name and replace it with "NewValue"

Sep 15, 2015

Local SMTP server for Testing and Development on Windows

Use Papercut.

Download the application and run it. It will set up an SMTP server when it's running. Configure your application to use 'localhost' (or the machine where the server is running) as the SMTP server. If you need to use a non-default port number, you can configure it in Options.

As per the CodePlex webpage:
Papercut is a simplified SMTP server designed to only receive messages (not to send them on) with a GUI on top of it allowing you to see the messages it receives. It doesn't enforce any restrictions on addresses, it just takes the message and allows you see it. It is only active while it is running, and if you want it in the background, just minimize it to the system tray. When it receives a new message, a balloon message will show up to let you know.