Skip to main content

Posts

Showing posts from 2017

PowerShell remove line feed

Line feed is useful in some ways like formatting lines so it will be easy to read if there’s a line in between the other line.
But there are also instances that line feed might be annoying or simply we need to get rid of it since it’s not desired on the output.
There instances that Out-string parameter in PowerShell will append a line feed on its output.
Or if you take the input or output from other sources but want to remove the line feed, PowerShell can remove the line feed without any hassle.
Code below is using PowerShell to remove line feed from an input, the input could either be a text file or a string pipe to another variable.
$string_with_line_feed -replace "`n|`r"
`n -  backtick with n refers to the line feed character `r – backtick with r refers to the carriage return
Or the code above can be written as follows:
$string_with_line_feed -replace "`n|"
So, basically line feed is replace with nothing. But for readability and to avoid confusion it’s better to …

Find or search files in Windows

Search files recursively using PowerShell or the old good dir command line.
To search using PowerShell:
gci d:\ -recurse | where-object {$_.name -like "g*.txt" }
GCI – a short form for Get-ChildItem Recurse – is to search recursively (folder and sub-folders) G*.txt - * tells the PowerShell to search any document that start with g and don’t care anything that follows it
To do it using the old way via command line, open command prompt and type:
Dir /s d:\ g*.txt
To save the search output just use the “>” redirect operator.
Dir /s d:\ g*.txt > d:\G_filenames.txt
/s - parameter to search recursively (folder and sub-folders)

The search result will be written to G_filenames.txt
In PowerShell to save the search result type:
gci d:\ -recurse | where-object {$_.name -like "g*.txt" } | out-file d:\gxfiles.txt


If you still want the very conventional way of searching.

Minimize all windows folders by pressing "windows key" + "d", win d (press both keys a…

PowerShell Test Port Forwarding

Port forwarding is necessary if the device is that needs to connect to the unsecured world of the Internet.
Of course, before opening port or doing any port forwarding make sure that security is in place. Firewall rules, software configuration are properly set, up-to-date anti-virus and other settings that needs to be done to secure the system.
PowerShell can test whether a remote port is open or a Port Forwarding rules on either on the Firewall or router is set correctly.
Here’s a one liner code, to check whether the Port is open or not.
Both one line code below does the same thing, check whether the Port is open or not.
1 . New-Object System.Net.Sockets.TCPClient -ArgumentList "Remote.Public.IP.Address", 3389
2. Test-NetConnection -Port 80 -InformationLevel Detailed

Replace the port number, with any port number to be tested.
If the connected property on the displayed output is true, then the port is open.

More details on the link below: https://technet.microsoft.com/en-us/lib…

Linux bash script get folder size

Having an error, no space left on the device?
But you don't know which folder is consuming the most space?
Bash script below will get all the folder size and display the size for each folder.
How to use the script below?
Open your favourite terminal editor, vi, vim or other editor.
At terminal, type: vi dirfoldersize.sh
Copy and paste the code below to the editor.
Save the file.
Then make the script executable, chmod +x dirfoldersize.sh
Run the script by typing, ./dirfoldersize.sh
If everything goes well, you will get the folder size for each folder on the directory where the script runs. See sample output below.
=============== #code begins start copy below
#!/bin/bash curdir=$(pwd) compath="$curdir/dirlist.txt" ls -l | grep ^d | awk '{print $9}'  > $compath

while IFS= read -r var do

  foldersize=$(du -sh $var |awk '{print $1}')
  echo "$curdir/$var folder size: $foldersize"
done < "$compath"
#code ends, end copying from above line ======…

Powershell move selected files

Powershell script below will sort files base on its last write time, it will select the files and move the selected files to another folder.

This script will remove the first 3 old files base on the lastwritetime property, quite useful to archive files or grouping large files into multiple folders.
#=====Code Snippet Start
$xfiles= Get-ChildItem d:\the_log_folder |  Sort-Object -Property @{Expression={$_.LaswriteTime}; Ascending = $true} | Select-Object -first 3 | select fullname | ft -HideTableHeaders
write $xfiles >  d:\move_files_listing.txt
ForEach ($movefiles in (Get-Content "d:\move_files_listing.txt"))
{     
  If ($movefiles[1].Length –gt 0) {
      move-item $movefiles d:\the_archive_folder
      write $movefiles
     }
}
#=====Code Snippet Ends

Code explanation:
--# Sort-Object -Property @{Expression={$_.LaswriteTime}; Ascending = $true # this will sort the files beginning from the file with the  last write time or basically the old files base on its write time.
--# …

Symantec endpoint protection deployment fails

Deploying Symantec Endpoint Protection or SEP 14 on remote computers will fail if Firewall or services on remote computer is not properly set.

Deploying SEP version 14 to computers running Windows 8.1 will not install successfully.

If ICMPV4 is not allowed on the firewall will also cause fail deployment.

Even though ICMPV4 is enabled and allowed on the firewall and the deployment still fails.

If unable to deploy SEP 14 to remote computers running Windows 8.1, either do manual installation, send a link to user and ask them to install if user has a local admin password.

Of course, if trying to install to multiple computers remote deployment is the best way to do.

It's efficient, faster and user productivity is not disturbed.

One solution for this issue, is to enable "remoteregistry" service on the remote computers.

To enable "remoteregistry" on remote computers, open an elevated command prompt and enter the domain credentials.

Type:
C:\>sc \\James_PC_001 confi…

Excel VBA copy or reference a value

Excel VBA provide options on how to pass value from worksheet to worksheet or from workbook to another workbook.
To optimize or make the VBA code run faster is to make the worksheet interaction lesser.
Recording an Excel VBA macro to copy and paste a value will have a similar output to the code below.
Sub Macro1() ' ' Macro1 Macro '     Range("A2").Select     Selection.Copy
    Sheets("Sheet2").Select     Range("A3").Select     ActiveSheet.Paste
End Sub
what the code does is to select the cell on the active sheet for copying and select another worksheet and select the cell where the value will be placed and paste the copied cell value.
Steps are: a. Select the cell and copy the value b. Select the worksheet where the value is to be copied c. Next step, is select the cell on the selected worksheet d. Paste the value on the cell
Steps are absolutely fine, if it works as expected. But if you are copying hundreds or thousands of cells. It will be a w…

Wanna disable SMBv1 to stop WannaCry

Link from Technet why SMBv1 should be disabled:

STOP using SMBv1

Link below shows how to to check whether SMBV1 is enabled and how to disable SMBV1:

How to disable SMBv1? - How to check if SMBv1 is enabled?


PowerShell codes below is from the link above:

Windows 8 and Windows Server 2012

To check whether SMB1 and SMB2 are enabled:

Get-SmbServerConfiguration | Select EnableSMB1Protocol, EnableSMB2Protocol

To disable SMBv1 on the SMB server, run the following cmdlet:

Set-SmbServerConfiguration -EnableSMB1Protocol $false

=====================

Windows 7, Windows Server 2008 R2, Windows Vista, and Windows Server 2008

Requires Windows PowerShell 2.0 or a later version of PowerShell

To disable SMBv1 on the SMB server, run the following cmdlet:

Set-ItemProperty -Path "HKLM:\SYSTEM\CurrentControlSet\Services\LanmanServer\Parameters" SMB1 -Type DWORD -Value 0 -Force

Or if you're not comfortable using PowerShell just open registry editor and do it manually.

PowerShell is quite straight forw…

Excel find last row with value in a specific column

1. How to find last row with value using vba in a specific column?

2. How to find last row with value  using vba in a worksheet?

The questions above are entirely two different scenarios.

The first question will find the last row with value in a specific column within the worksheet.

While the second question find the last row with value of the entire worksheet.

The first question is applicable if you need to find the last row in different columns.

Let's say Column 1 or Column A, or Column 2 or Column B etc.

Example code below will find the last row in a specific column via VBA code.

===================
'command Cells(Rows.Count, 1) = find the last row with value in Column 1 for Sheet3

Dim last_row As Integer

last_row = Worksheets("sheet3").Cells(Rows.Count, 1).End(xlUp).Row

Msgbox last_row

'replace 1 with the desired column 
'ex: command Cells(Rows.Count, 7) = find the last row with value in Column 7
===================

If you don't need to specify a worksheet or…

How to insert landscape orientation in Word?

If you are writing a thesis, a report or an article that you need to insert a landscape layout in a specific page in Word document but your orientation all long is portrait and changing the orientation causes the whole layout to change.
Inserting landscape in a specific page in a word document can be done by inserting section breaks. Let’s just make a basic example.
Let’s say you have 3 page document and you want that first page in Portrait mode, second page in landscape and third page in portrait layout also.
First, click on “home” tab, click the “show/hide paragraph marks” option or see the icon below. Enabling this option the section breaks, spaces and tabs will be visible. But what we are interested is to see the “section breaks”.


In the first page position the cursor on the last area at the bottom of the page.
In Word 2010, click on “page layout” in the ribbon tab. Click on “breaks” and select “next page”. In the second page, position also the cursor at the middle or bottom of t…