Posts

Showing posts with the label PowerShell

List completed cron jobs in Centos

Listing cron jobs or log files within a specific time frame is quite hard especially if the log or logs are quite a big file.

But of course, doing the lazy way but a smarter way is always a good option.

Use SED or stream editor.

In Centos the log is in: /var/log/cron

/var/log - path for the file
 cron - is the file that keeps the record for cron jobs, there is no filename extension

To check the logs within the 24 hours time, sed can do it easily and quickly.

Here's one line, time saver command to check the cron log file:

sed -n '/Mar 10 00:00:01/ , /Mar 11 00:01:01/p' /var/log/cron

To redirect the output to a file:

sed -n '/Apr 10 00:00:01/ , /Apr 11 00:01:01/p' /var/log/cron > cron24_hours.record.log

You can replace /var/log/cron with any other files as long as it follows the time format of Month, Day of the month and the time in HR:MM:SS format.


Download the free Linux Android App cheat sheet, see link below. It's free. Enjoy.



Cheers..till next time!

=======…

PowerShell copy file, link or folder to users’ desktop

Deploying a shortcut link, file or folders can easily be done via group policy preferences.
Or it can also be done using PowerShell as logon or start up script.
A one liner code snippet to deploy a file, a shortcut links or a folder to user’s desktop.
Copy a file: copy-item "\\shared_folder\files_2be_copied.txt" "c:\users\public\desktop"
Copy a shortcut link to user desktop: copy-item "\\shared_folder\shortcut_file_2be_copied.lnk" "c:\users\public\desktop"
Copy a folder: copy-item "\\shared_folder\folder_2be_copied" "c:\users\public\desktop" -Container -Recurse


================================ Free Android Apps:
Click on links below to find out more:
Linux Android App cheat sheet: https://play.google.com/store/apps/details?id=com.LinuxMobileKit
Multiplication Table for early learnershttps://play.google.com/store/apps/details?id=com.TableMultiplication
Catholic Rosary Guide  for Android:
https://play.google.com/store/apps/details?id=com.my…

PowerShell move matching files in a folder

Requires PS 3.0
This is a two lines code snippet to move matching files in a source folder to another folder.
#==============================

$SourceFolder = "C:\File_monitor_records\"
# Find all files matching *.csv in the folder specified  # Find csv files with 2016 in its filename Get-ChildItem -Path $SourceFolder -Filter *2016*.csv | move-item  -destination C:\File_monitor_archives\2016\

#==============================
-Filter *2016*.csv –adjust or create your own pattern, use regex if necessary
Test the pattern before moving the files to make sure correct files are being moved. Check and double check, to avoid wasting time and effort.
This will just like matching files.

Get-ChildItem -Path $SourceFolder -Filter *2016*.csv


Move files and change file name:
http://quickbytesstuff.blogspot.sg/2015/06/powershell-move-files-and-change-file.html

================================ Free Android Apps:
Click on links below to find out more:
Linux Android App cheat sheet: https://play.google.com/s…

Hide the taskbar in Windows 10

Image
Windows 10 update changes the way how to hide the taskbar.
The old way was the traditional way of hiding the taskbar just like in Windows XP, by right clicking on an empty taskbar and selecting “properties” and from the properties window there is an option to hide the taskbar automatically.
However, Windows 10 OSes that has  been updated, right clicking the empty taskbar does not show “properties” anymore.  Instead of clicking or finding properties, click on “settings” and on the settings Window, an option to hide the taskbar in desktop mode can be enabled or disabled.
See screens shot below, on how to do it:





================================ Free Android Apps:
Click on links below to find out more:
Linux Android App cheat sheet: https://play.google.com/store/apps/details?id=com.LinuxMobileKit
Multiplication Table for early learnershttps://play.google.com/store/apps/details?id=com.TableMultiplication
Catholic Rosary Guide  for Android:
https://play.google.com/store/apps/details?id=com.myrosar…

Copy folder/file to multiple folders

Copy a single folder to multiple folders?

Copy a file to multiple folders?

This can be done easily using PowerShell script.

Script below requires PS 3.0, but it can easily be tweak for other version.

Here's the script:

#++++++++++++++++++++++++++++++
$source_folder="D:\xfolder\United Folders"
dir -Directory "D:\Unity\Project_016\" | ForEach-Object {
write-host $_.fullname
Copy-Item -Path $source_folder -Destination $_.FullName
}
#++++++++++++++++++++++++++++++

What the script does is the $source_folder is to be copied to all folders in the destination.

Basically, the "United Folders" is to be copied to the root of the sub folder in destination folder which is "D:\Unity\Project_016\".

Example:
D:\Unity\Project_016\01
D:\Unity\Project_016\02
D:\Unity\Project_016\03

After executing the script, the folders will look like this:
D:\Unity\Project_016\01\United Folders
D:\Unity\Project_016\02\United Folders
D:\Unity\Project_016\03\United Folders

It dep…

Linux search string in text files

Search a string or a pattern in text files without opening the file.

Grep is a handy tool to find or search a string in text files.

Grep is an available tool in Linux and Unix OSes.

So if you have some data or information stored in text files and forget where the file is located.

As long as you know the keyword or a string to search for, then grep and find command will be your utmost friend.

"Grep" and "find" are tools to make life easier to get the information you want but how to use it?

Command below will search recursively in the patch specified for all the text files and display the file where the match is found, output will also include the path and the filename.

find /home/00_Notes -name '*.txt' -print0 | xargs -0r grep -H 'vanity baseline'



The xargs -0r,  is zero r.

The above command will search recursively in all folders and subfolders for text files which contains the string "vanity baseline".

If a match is found an output will be…

PowerShell list processes by username

List all processes by username in PowerShell, requires that PowerShell to be run in elevated mode.




List all running processes for all users:

   Get-Process -IncludeUserName  | ft -AutoSize

     ft - Autosize --will not truncate the output



List all running processes and filter by UserName:

Get-Process -IncludeUserName  | Where UserName -EQ -EQ "This_User_Name"  | ft -AutoSize

Above script will filter the output to the specified username.


Get running word or winword process for the specified user and kill or stop the process.

Get-Process winword -IncludeUserName  | Where UserName -EQ "This_User_Name"  | Stop-Process -Force

-Force parameter will suppress the prompt whether to kill or stop the process, use sparingly in production environment

Requires PS 4.0 version to run above commands.

Cheers!! till next time.
================================ Free Android Apps:
Click on links below to find out more:
Linux Android App cheat sheet: https://play.google.com/store/apps/details?…

Shutdown or reboot computers with user abort option in Windows

Delete Directories with Wildcards using rd or rmdir

Deleting files in command prompt using wildcards is quite straight forward.
Command below will delete all text (".txt") files on the specified path.
Del D:\txtlog\*.txt
Command above will delete all files with ".txt" extension in d:\txtlog directory.
Easy enough to delete all matching files.
Using the same method with rmdir or rd command this will not work.

For example, if we have a directory on d drive that is auto-generated by an application and the filename is consistent with a pattern plus incrementing number at the end to differentiate the folder from other folders.

   D:\baklogs\log1\    D:\baklogs\log2\    D:\baklogs\log3\    Etc..    D:\baklogs\log100\

The folder name has a consistent pattern that is preceded by the word “log” plus incrementing number.
If the command below is executed to remove the directories in one go, an error is shown which has this message: "The filename, directory name, or volume label syntax is incorrect."
rmdir D:\baklogs\…

PowerShell get filename with extension

#=======================================

$Folder_file_path="D:\test_folder\"
$initCount=0

[array]$data_files = Get-ChildItem $Folder_file_path  #get all items and save to array

  $i_count=$data_files.count  #count items on the array and assign to $i_count variable

  Write-Host $initCount   #optional to display count value

#if statement to process if files are found

if($data_files.count -gt 0){


#increment for loop from zero to last index of the array
 for  ($initCount; $initCount -lt $i_count; $initCount++ ) { 

       #Write-Host "$($data_files.Count) Files found in folder!"

       Write-Host   ($data_files[$initCount].name ) #display the filename with the extension

        Write-Output  ($data_files[$initCount].name ) >> d:\Files_list.txt # write data to a text file; ">>" append operator since for loop is used

 }

}else{

    Write-Host No files in the folder!
}

#=======================================

Change the script folder path to the desired location.

The…

Check Domain Name, DHCP and IP Address