Skip to main content

Posts

Showing posts from May, 2020

PowerShell launch an application and close it automatically

Code below will run an application and PowerShell will also close it after a few seconds. $process_id = Invoke-CimMethod Win32_Process -MethodName "Create" -Arguments @{ CommandLine = 'cmd /c "C:\dev\remind.bat"' } | Select-Object processid | ft -HideTableHeaders | Out-String   Start-Sleep -Seconds 2.5   stop-process $process_id   Sample contents of remind.bat: @echo off echo "Shutdown the server. Do it now." Notepad ================== Notepad is on the batch file so the command window will stay on the top of any running applications. Sample output of the above code:   Code below will run calculator and close after a few seconds specified on the code: Invoke-CimMethod Win32_Process -MethodName "Create" -Arguments @{ CommandLine = 'calc.exe' } | Select-Object processid | ft -HideTableHeaders | Out-String   Start-Sleep -Seconds 2.5   $get_process_id = Get-Process

Linux sed remove duplicates and get unique values

Removing duplicates and getting unique values is quite easy provided that the input data follows a specific format, for example the string or raw data has spaces in between. But a dilemma can occur if the data has no spaces in between the characters of the string, instead of spaces it is separated by dashes. So, how to remove duplicates, get the unique values and still retain the format of the raw data? Like this raw data: (just a sample string) the-quick-brown-fox-jumps-over-the-lazy-dog-jumps-over-the-lazy-cat-jumps-over-the-rabbit When removing duplicates and getting unique values via this command: sort duplicates.txt | uniq (this will work if the data is separated by spaces) duplicates.txt assumes that it has the string as illustrated above. Sample output: The output will be the exactly be the same with the input. Why? It is because the whole string is treated as literal one string, because the dashes connect between the character eliminat

Create a new text file with content in Linux

Touch command in Linux will create an empty file in Linux command line. If no other parameters is specified whether a line will be inserted to the newly created file. Cat command is for reading files via command line but it can also be used to create a file in Linux terminal window command line. Cat command below will create a file called “test_comment.txt” and with the content or the line of “Hello World of Touch and Create File” on the file created. Here’s the shell command: cat  <( echo “Hello World of Touch and Create File”)  >  test_comment.txt Note that there is no space between “<(“, if there is a space then the command will not work as expected. Touch command to create a new file and also insert a new line or insert a string on the newly created file. Here’s the shell command: touch touch_file_comment.txt; echo “Hello World of Touch, insert this line.” >  touch_file_comment.txt Basically, the command executes two commands s

PowerShell restart remote server with confirmation

Restart a remote PC or server and ask user to confirm reboot before proceeding. Here’s the code: $server_q = "D-Box-007" $response_confirmation = [ Microsoft.VisualBasic.Interaction ]:: MsgBox( "Restart this server: " + $server_q , 'YesNo' , "Confirm to reboot?" )     switch ( $response_confirmation )     {         Yes      { Restart-Computer -ComputerName $server_q    }         No       { Write-Host "Restart Cancelled"    }                  } Code above will prompt the user with “Yes” or “No”, yes will send the restart or reboot command to the remote server or computer, and no will cancel or exit the execution. PowerShell code above also demonstrate how to use a switch case statement and use the response to perform an action, in regards to user response or input. It would prevent any accidental reboot, and of course the script can be tweak for any operations that requires user c

How to copy a line using Nano editor?

Copying in Nano needs a few keys to copy and paste.  Copy and paste goes hand in hand, there’s no point to copy if you cannot paste. So, how to copy using Nano editor in Linux? To copy in Nano editor , a few key strokes are needed. On the beginning of the line that will be copied press: ctrl + 6 (to set a mark) Press “end” key or use right arrow to go to the end of the line. If multiple lines are to be copied used down arrow to select the lines. After selecting the line or lines to be copied, press: alt + 6 (to set unmark and all line/s selected is copied to the clipboard already. To copy the line or lines, position the cursor where the line/s will be copied and press: ctrl + u (this will paste the line/s or whatever is on the clipboard) That’s it, quite a few key strokes to remember but if you’re in a command line or in Putty; then these key combination to copy and paste in Nano is really a life saver. Cheers..till next time

PowerShell check access or folder permissions

In an Active Directory domain, one of the common issues is folder permissions. Of course, permission must be restricted as much as possible. In order, confidential data or things that only a group of people will know, is not made available to everyone. Employee salaries on a network share made available to everyone, will cause some employees to be disheartened. Such data, the access or folder permission should be checked properly. One way to check a folder permission is to assume or login as a certain user that is not supposed to have access to such data and checked whether the data can be viewed or not. Aside from restricting permissions for confidential data; data supposed to be accessed by everyone but some user’s complaint that they are unable to access then this will also cause some problem and may end up not being productive for the users. One way to check is to go to the user’s workstation and verify whether the user is unable to access. This kind of issue is

PowerShell Get OneDrive email address

If you have a subscription to Office 365, chances are you have OneDrive configured in your system and is linked to your email address. OneDrive is quite good since you can have your data on the cloud, of course you also have to use common sense and what data you are uploading to the cloud. OneDrive will come handy since you just need internet connection and you will be able to retrieve your notes or data anywhere. But how to check which email address is linked to your OneDrive? Well, there' a simple way using PowerShell to view registry keys. A one liner PowerShell script will come-in handy to do these kind of task. Here’s the code, if using Hotmail, or Li ve email: ( Get-ItemProperty -Path HKCU:\Software\Microsoft\OneDrive\Accounts\Personal -Name useremail ) . useremail For OneDrive Business use this code: ( Get-ItemProperty -Path HKCU:\Software\Microsoft\OneDrive\Accounts\Business1 -Name useremail ) . useremail Then the value set on th