Skip to main content

Posts

Showing posts with the label Script

PowerShell regex count specific character or number

Example input is: 0009911000


$Input_Number = '0009911000'

#[1] = tells regex to match on the right or look forward
$xtotal=[regex]::Matches($Input_Number,'0+')[1].Value.Length
#[0] = tells regex to match on the left or look backward
$ytotal=[regex]::Matches($Input_Number,'0+')[0].Value.Length

#Matches($Number,'0+') means #Matches($input, $pattern)

Write-Output $xtotal Write-Output $ytotal
Image output from PowerShell ISE:


Above code works or matches if input is something like this: “0009911000” There are zeroes on the left and right and in between are any numbers or characters.
For input like this: 0000991100010 in which zeroes are in different position of the string the below code will work.
$Input_Number = '0000991100010' $xytotal=[regex]::Matches($Input_Number,'[0]*[0]*[0]+') Write-Output $xytotal

Output will be something like this: Groups: {0000} Success: True Captures : {0000} Index: 0 Length: 4 Value: 0000
Groups: {000} Success: T…

Test AD account password without logging in to the computer

Documenting AD password is a good practice and should be a part of IT policy.
It’s easier to troubleshoot and support users if the password is known to IT. Of course, ethical use of the password should be followed.
How to test an active directory account password whether it’s working or not, without logging in or signing in on a computer?
PowerShell can help to test a user account password quite easily.
AD admin account is needed and the PowerShell must be elevated while running the script below. Correct version of PowerShell is needed for this script.
See image below:


That’s it! Just replace the username and password to be tested and you can tell your users that the password is not working and needs password reset for their account to work.
The script is written for educational purposes and used it ethically.
Cheers!! Till next time. Happy Coding!!!
================================ Free Android Apps:
Click  links below to find out more:
Excel Keyboard guide: https://play.google.com/store/apps/det…

Replace column value of text files using awk

AWK is a handy tool to process large text files.
Ever came across a need to replace 100 or more text files value in a single column?
Of course, the old school method is to do it manually which is definitely time consuming and not efficient at all.
Below is a sample code how to replace value in a column for all text files found in the directory specified.
#!/bin/bash
for file in /home/pc_text/Documents/source/*
do xfilename=$(basename $file)
#echo "$xfilename" if you need to display the filename while the code is running
awk 'FNR==1{print $0}' $file >> /home/pc_text/Documents/output/$xfilename
awk 'NR>1{print $1 , $2 , $3 , $4, "0.873" , $6 , $7 }' $file >> /home/pc_text/Documents/output/$xfilename
done
Code explanation:
for file in /home/pc_text/Documents/source/* - get all text files for processing
xfilename=$(basename $file) – get all the filename of the text files
awk 'FNR==1{print $0}' $file >> /home/pc_text/Documents/outpu…

Working with text in PowerShell

Working with a text file or text format in PowerShell is a good thing to learn.
Getting or monitoring output from PowerShell for further processing or analysis and having it in text file format can easily be imported to other applications.
Working with text or other format, requires these basic operations: Save, Overwrite, Append and Save As.
Save, overwrite, append and save as is a common operation when editing word documents, excel file and other office applications since it’s freely available on the menu of the application. But can we do this in PowerShell? The answer is a resounding, yes.


So, how to do it? Examples below shows how to work with text files in PowerShell and do the operation mentioned above.

Equivalent operations for Save, Overwrite, Append and Save As in PowerShell:
===Set-content ==== will overwrite the file ===Add-content ==== will just add or append something to the file ===Out-File ==== is like "Save As" since you have the option to save the file to a…

Change Password of Scheduled Tasks in PowerShell

How to change Tasks Scheduler password?
How to set a new password for scheduled tasks in Windows Task Scheduler using a script?
PowerShell is always a friend to cure laziness in an effective and efficient way.
Of course, not all things can be done by automation. Sometimes manual method is more applicable and a secure method on some scenario.
If you have a few servers that has scheduled tasks on it and need to change the password occasionally to comply with cyber security policies, changing tasks manually might be painful and chances are some tasks might be missed.
Changing the password in Task Scheduler using PowerShell is an efficient method if the task has to be done repeatedly on a few servers.
To change the password, the TaskName must be known since it’s an input or a required field for the command used to change the Task password.
To get Tasks Name use the PowerShell code below:
Get-ScheduleTask - TaskPath "\"| Get-ScheduledTaskInfo
The TaskPath refers to location of the Task Na…

PowerShell Ping IP get status or reply

How to ping sweep a network?
How to ping sweep a subnet?
There’s a lot of ways to ping the whole subnet or do a sweep ping to the whole network, of course download any free software that can do this, or just manually ping the whole network by manually changing the IP address.
Or do an old school method, using a batch file. This link shows how to ping the whole subnet via command line. http://quickbytesstuff.blogspot.sg/2015/11/how-to-ping-whole-subnet-using-batch.html
Since PowerShell is readily available from Windows 7 up to the newer versions of Windows. Then PowerShell is the perfect choice to do this task.
Open PowerShell ISE or the PowerShell command line, then type or copy the commands below:
$ping = New-Object System.Net.Networkinformation.Ping 1..254 | % { $ping.send(“192.168.1.$_”) | select address, status | ft -auto }

Change the IP Address (192.168.X.X) to the IP address range of your network.
What the script does, is to Ping the whole network starting from 1 to 254, basically the w…

Remove startup files in Windows 10

How to delete, remove the files or program during startup in Windows 10?

To disable the startup program in Windows 10, right click on the task bar choose the empty space and select task manager.

On the Window tabs, Processes, Performance, etc. click on "Startup".

Right click on the program you want to manage select "Disable" if you need to disable, or if you need to enable it again select "enable" of course.

There's also an option "Open File location"|, if you need to check the  exact location of the program or file.

If you need to add custom files to start-up folder, the path would be:

C:\Users\\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup

Or open run box, by pressing shortcut key Windows + R and on the run box type: shell:startup

This will open the startup folder path location stated above.



Till next time. Cheers!

================================ Free Android Apps:
Click on links below to find out more:
Catholic Rosary Guide  …

Check PowerShell Version

How to check PowerShell version?

Open PowerShell ISE or PowerShell command line and type: get-host | select version

Sample Output:
Version: 2.0

Why need to check the PowerShell version?

If some commands that you find online is not working, it's either the command is wrong or the command is available for specific versions only.

New commands will work on new version of course, old commands may not work also on newer version if the command is obsolete or deprecated.

As the odds say, "Change is the only permanent thing in this world." and this quote is quite applicable in today's digital world.


Till next time. Cheers!

================================ Free Android Apps:
Click on links below to find out more:
Catholic Rosary Guide  for Android:
https://play.google.com/store/apps/details?id=com.myrosaryapp
http://quickbytesstuff.blogspot.sg/2014/09/how-to-recite-rosary.html
https://play.google.com/store/apps/details?id=soulrefresh.beautiful.prayer
Excel Keyboard shortcuts guide http…

How to get uptime in Windows using PowerShell

If you need to monitor how long the machine has been up, or whether the machine rebooted by itself or someone has just rebooted the server for without any reason. Then getting the uptime of the machine or server is important.
PowerShell code below gets the uptime by using a command line and processing its output.
#==========================
#get the uptime and save to text file Invoke-command -ScriptBlock { net statistics server >c:\online.txt }  $xcmd = select-string -path "c:\online.txt" -Pattern "since" | select line |ft -hidetableheaders  write-output $xcmd | out-file c:\online1.txt  $stry = get-content c:\online1.txt | out-string  $concatString = $stry.Substring(18,15) Write-host  $concatString
#Get the date $StartDate= [datetime] $concatString $EndDate= (GET-DATE) $xdate = NEW-TIMESPAN –Start $StartDate –End $EndDate write-output $xdate #==========================
Sample output:
Days              : 3 Hours             : 12 Minutes           : 12 Seconds           : 17 …