Showing posts from July, 2014

PowerShell Remoting Basic

Scripts below can be run on a server/workstation to get network shares on a remote server or remote workstation.
The  first script below will prompt for a password for authentication to get the network shares on the target specified.
After the correct password has been entered, script will display the available network shares of the specified target.
A pop up box will open and user needs to enter the necessary credentials for the script to proceed its to connect to the remote computer and get the available shared folders.
The script will require user intervention to key in manually the details.

$Computer = "server1" $domain="myXdomain" $username="xDomainOwner" $UserDomain = $domain + "\" + $username
#This line below should be in one line or else there will be an error $xWmiObj =Get-WmiObject -Namespace "root\cimv2" -Class Win32_share -Impersonation 3 -Credential $UserDomain -ComputerName $Computer
$xWmiObj | sort n…

PowerShell creating Windows Forms

From this Technet blog (link below), it shows how to add a Form and a Label control using PowerShell.
The PowerShell script below is a modification from above link.
Here, I will add a Button to the form, and also add an event click listener to the button.
And also add the simple but very useful "Msgbox".
Unlike VB.Net where you can just drag and drop your control on the form and VB.Net will do the necessary things on the background. For PowerShell you have to code the controls and location.
For the script below, location control for x and y coordinates is set.
If the location is not set, the control will be overlapping with each other and other controls cannot be seen.
To dig more about MSDN System Windows Forms, check out link below:

Here's the script below, with comments above the codes:
# ======…

Linux check file and directory size

Linux beginner tips.
How to check file size in Linux in Gigabytes or Megabytes?
How to check directory size in Linux in human readable format?
Normally log files if left not monitored the size capacity will grow larger over time.
To check the size of a log file or basically any files on Linux.
Just type:
ls -lh mail.log
   dir -h mail.log
Output is similar to this: -rw------- 1 xuser xuser 3.2G Jul 20 00:54 mail.log
mail.log is the file queried for its size capacity, it can be any files on the directory.
Like:  ls -lh myVideo.mkv 
       dir -h mySong.mp3
To check the size of a directory type:
Let's say on /home/xuser directory there is a folder name "video".
To check the size of the folder name “video” browse to the folder path.
cd /home/xuser
/home/xuser # du -ah video | tail -n 1
Output will be as simple as: 257G video
It means that the folder “video” has a size capacity of 257GB.
tail -n 1 tells Linux to display only the last line which is basically the total capacity o…

Excel Remove Personal Information

Sending an excel file or sharing an excel file to someone else will have some of your personal information.

The Author field will have the name of the person who started or created the workbook.

It will also contain some information when it was last printed, date modified, company name and other data or information.

Excel 2010 provides an option to remove all this information before sending out or sharing it to other parties if it is necessary to do so.

This process cannot be undone, so it is better to save another copy of the workbook. One with Personal Info and another copy for sharing without any personal info.

Below are the steps on how to do it:

Click on "File" or the "Ribbon".

Select "Info", and click "Check for Issues".

A pop down menu will open.

Click on "Inspect Document"

Tick the options that need to be inspected. Default all check boxes are tick. Click on "Inspect" button.

Once inspecting process has been done, Ex…

Convert Windows to Virtual Machines

How to convert old servers or PC to a virtual machine?
SysInternal provides a free tool to convert Windows OS to virtual machines, in quick and easy way.
If you don't have budget to but a new hardware or a new OS that supports Microsoft Hyper-V.
Then make use of Microsoft Virtual PC.
Disk2vhd v2.01 is a tool to create a virtual machine and save it as VHD.
VHD file can be run using Microsoft Virtual PC or Hyper-V.
If you have old machines or servers that is not under maintenance and we all know that one way or another machines will go south.
While the machines or servers are still running or you're still given a chance to save them.
Disk2vhd is a cool tool, to save old machines and when time comes that there life will be over.
Then you can just plug in the VHD and run it. Dis2vhd is able to convert the OS to VM's while the system is online. Downtime will not be a concern.
Conversion time will depend on the hardware and system performance and the number of partitions that a…

Outlook VBA Get Contacts on Public Folders

How to get contacts on Outlook Public Folders using VBA?

Getting the contacts on the default folder is quite straight forward.

The GetDefaultFolder command on VBA can simply do the task.

Below is a screen shot of Outlook contact folder:

Above screen shot show the structure of Outlook contact folder, as a developer it's also good to know that the text or the string after the "-" sign is actually the default folder path.

And the string before the "-" sign is the sub folder name.

This is assuming that the Outlook settings has never been changed or customized by the user.

If ever the Contact folder has been changed by the user.
The settings can be verified also by right clicking on the contact folder and selecting properties.

See screen shot below:

Outlook Contacts in Public Folders has also the same structure with the default folder contacts but accessing the data or the items via VBA is not the same.

Public Contact Folder screen shot:

To make life easier, just c…

Find MAC Address of a remote computer on network

Finding MAC address of a remote computer on network via command line is quite straight forward.
Using the built-in tool from Windows, administrator can easily find the MAC Address of a remote computer on the network.
Nbtstat.exe is a file that can be executed via command prompt to get the MAC Address of a remote computer.
To get the MAC Address for a single remote computer type:
 nbtstat -A | find "MAC Address" Nbtstat can also get the remote machine name and MAC Address automatically using batch file script.
Batch file script below will get the remote machine's MAC Address and computer name using IP Address using the text file parameter.
Text file contents will just be IP Addresses, sequence order is not necessary.
But it would be easy to use Excel file to type two IP Address in sequence and just drag to autocomplete the last octet.
And just copy and paste from excel to notepad.
For the batch file script below:
ifile.txt - will have the list of IP Addresses. computer1.…

Clear Clipboard via command line

Clip.exe accepts parameters via pipe.
This command below uses type command and pipe to clip.exe to clear the clipboard.
cmd /c "type | clip"
Or you can pipe some other exe command line to clear the clipboard like:
  svchost | clip
    sndvol | clip
Sndvol will launch the volume mixer and at the same time clears the clipboard, while svchost.exe pipe to clip.exe will also clears the clipboard.
Command above can be type on the “run box”, press “windows key + r” to launch “run window” and type the command line. Or just open a command prompt and type those command.
Or see picture below for the run box window.

It can also be save as batch file script and save to desktop for easy access.
To make a batch file, just open notepad and type the command and save notepad as batch file script with .bat extension like “clipboard_clear.bat”

And tsdicon command, is a command line that will disconnect a user session but if it is pipe to clip.exe.
It will disconnect the session and at the same t…

Word VBA Table Auto Number of Rows

Auto numbering is quite simple and straight forward.
Type two number in sequence and drag through the cells, Excel will auto number the rows.
In Word if  table is inserted, numbering the table rows can be done manually if you have plenty of time or do some other creative ways.
But using 10 lines of VBA code below can save time typing and eliminate typo errors.
This can be used if there is a single table inserted in Word, number of rows is not a concern.
And assumes that the first column is the column where the numbering will be inserted.
Just change or modify the code to customise.
Copy the code to the Macro window and run, the VBA will insert the number in sequence depending on the number of rows in the table.
Here's the VBA code: ========================
Sub xnum() ' ' xnum Macro ' ' Auto Numbering Word Table
Dim xtbl As Table Dim xrow As Integer Dim xcol As Integer Dim xVal As String
xcol = 1 Set xtbl = ActiveDocument.Tables(1) For xrow = 1 To xtbl.Rows.Count xtbl.Cell(xrow,…

PowerShell Create Folder and Set Permissions

Code snippet below will create a folder using the old conventional "md"  or make a directory DOS command, which is equivalent to "mkdir" in Linux world.

Folder permissions or security settings will be set using ICACLS.exe which is also a command prompt.

PowerShell of course, can do this job. Using Set-ACL cmdlet and Get-ACL cmdlet will view the security permissions settings.

Example below will create a folder "xtxtFolder" and Icacls will remove the inherited permissions and grant full access to xRepoUser.

To find out more syntax on Icacls, type on command prompt "Icacls /?", don't include quotation marks.


#Create Folder
 $xFolder = {md d:\xtxtFolder}
 Invoke-Command $xFolder

 #set Folder Permissions
 #In order for the Permission to be set accordingly
 #run the script with aprropriate admin credentials
 $xPermissions = {"Icacls d:\xtxtFolder /inheritance:r /grant:r  xRepoUser:(OI)(CI)(F)"}

PowerShell Get NIC IP Address

PowerShell code snippet to get network interface card/s (NICs) IP Address configuration.

If  the server or workstation has multiple NIC's configured, code snippet below will get all the NIC's info.

The script can be modified to get the IP Address of remote computers or servers. An example code below shows on how to get IP Address of remote machines.


#Display all NIC's with Static IP Address settings

$xdata = Get-WmiObject -Class Win32_NetworkAdapterConfiguration 

Foreach ($xElement in $xdata) {

 if ($xElement.IPAddress -ne $null -and $xElement.DHCPEnabled -eq $False) {
 write-output $xElement



#Display all NIC's with IP Address configuration (DHCP and Static settings)

$xdata = Get-WmiObject -Class Win32_NetworkAdapterConfiguration 

Foreach ($xElement in $xdata) {

 if ($xElement.IPAddress -ne $null) {
 write-output $xElement



Sample output:

To tweak the script to get IP Address of remote computers or servers, use a…

Excel VBA List or Get Shared Folder Names

Excel VBA code snippet to list or get shared folder names on the specified path and save data to Excel.

This piece of code can also be used on network shared folders provided the network permission allows.


Sub GetFolderNames()

 Dim i, fso, xfolder, xsubFolders, xdata, xgetFolderName, xPath

 'xPath = "\\My_Network_Path\My_Folder"
  xPath = "c:\my_folder\"

    Set fso = CreateObject("Scripting.FileSystemObject")
    Set xfolder = fso.GetFolder(xPath)
    Set xsubFolders = xfolder.SubFolders

    i = 0
    xdata = ""

    For Each xgetFolderName In xsubFolders

        xdata = xPath & xgetFolderName.Name

        i = i + 1

       Cells(i, 3).Value = xdata


    MsgBox ("Total Number of Folders: " & i)

End Sub