Skip to main content

Posts

Showing posts with the label Startup Script

PowerShell Get running processes and its command line path

How to get running processes with PowerShell using WMI and display its path. PowerShell below will display the path and also the command line of the process on how the process was executed. The code below will also display the process-id, the process-id or PID can also be supplied to an application called handle64 from SysInternal tools. This tool is quite cool since it will display more info about the PID supplied to handle application. Download link for handle64: https://docs.microsoft.com/en-us/sysinternals/downloads/handle Example output of handle64: Skype.exe pid: 42360 DESKTOP-Name\iuser-xname    40: File          C:\Windows    84: File          C:\Program Files (x86)\Microsoft\Skype for Desktop   24C: File          C:\Windows\WinSxS\x86_microsoft.windows.common-controls_6595b23cb64144ccf1df_6.0.19041.1_none_fd031af4F787f5b0106f2   380: File          C:...

Compare multiple variables Conditions with if else in PowerShell

Multiple conditions to evaluate is sometimes necessary in order to decide whether the script should continue to execute a specific command or do another process. PowerShell is able to do this kind of task using if else statement. Example script below just shows 3 variables that should match and it will output "OK", the output is just a write-host command but it could be replaced with other codes. And if one of the statements does not match the "else" statement is executed. Example code: All statements or condition matches $var1 = "Online" $var2 = "Running" $var3 = "Proceed"   if (( $var1 -eq "Online" ) -and ( $var2 -eq "Running" )   -and ( $var3 -eq "Proceed" )) {   write-host "OK"   } else {   write-host "Not OK unable to Proceed"   } Output: OK One of the variable does not match: $var1 = "Online" $var2 = "Running" $var...

Python check if an item is on the list

How to check whether an item exist on the list? If then else and using the "in" operator in Python is able to check whether an item is on the list or not. Here's a code example, it's not a complete program but a demonstration how the snippet can be used, the code can be integrated into an existing complete code. lst = list() #list declaration in Python         #if item is already on the list it will just continue and will not execute the else command            if yitem in lst: continue          else:           print(" Item 'yitem' is not present in the list, appending item to the list.")           lst.append(yitem)  #action that will be done if item was not found on the list For completeness sake if something needs to be done if the item is found, then the code will be like this:        if yitem in lst:      ...

Read or get the list of contents on S3 bucket

S3 or Simple Storage Service bucket is quite handy to store files or any data on AWS cloud. As with any storage, online or offline organizing the data is quite an issue. When there are a lot of files of data on the storage; finding the data that you will need will be difficult. Especially, if the data or bucket is not organized properly. And even if the data has been organized by folders with proper time stamp but no specific data catalog to classify which one is which one it is quite a challenge to find or get the data on time. S3 is ideal for backup, since backup is often accessed only when it is needed. So, familiarization of the backup folder structure is necessary. In order to find or get easily the data, listing the contents of the S3 bucket is a good option when unable to find the files or data needed. Once the list is ready, opening the list via editor such as notepad and searching or finding thru the editor will provide an option to find things quickly and easily. ...

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 ...