Implement a command line shell by using Command Dispatcher in Python
Lets implement a command shell by using a command dispatcher. The objective is…
March 03, 2021
We will list few interesting automation techniques with powershell.
$script_path = dir "$($myInvocation.MyCommand.Definition)"
$script_path = $script_path.DirectoryName
$script_name = $MyInvocation.MyCommand.Name
Write-Output "================================================"
Write-Output ""
Write-Output " My Fancy Banner"
Write-Output ""
Write-Output " - Installs All Dependencies"
Write-Output ""
Write-Output " To run silently add -Silent"
Write-Output "================================================"
Write-Output ""
If you have written powershell script in another file, you can include that with Import-Module
$script_path = dir "$($myInvocation.MyCommand.Definition)"
$script_path = $script_path.DirectoryName
Import-Module $script_path\Modules\my-module.psm1
First we can write functions
function Get-IsAdministrator
{
$Identity = [System.Security.Principal.WindowsIdentity]::GetCurrent()
$Principal = New-Object System.Security.Principal.WindowsPrincipal($Identity)
$Principal.IsInRole([System.Security.Principal.WindowsBuiltInRole]::Administrator)
}
function Get-IsUacEnabled
{
(Get-ItemProperty HKLM:\Software\Microsoft\Windows\CurrentVersion\Policies\System).EnableLua -ne 0
}
Then use them,
If (!(Get-IsAdministrator)) {
...
}
# check for UAC enabled user
If (Get-IsUacEnabled) {
...
}
If (!(Get-IsAdministrator)) {
If (Get-IsUacEnabled) {
# We are not running "as Administrator" - so relaunch as administrator
# Create a new process object that starts PowerShell
$newProcess = new-object System.Diagnostics.ProcessStartInfo "PowerShell";
# Specify the current script path and name as a parameter
$newProcess.Arguments = $myInvocation.MyCommand.Definition
# Specify the current working directory
$newProcess.WorkingDirectory = "$script_path"
# Indicate that the process should be elevated
$newProcess.Verb = "runas";
# Start the new process
[System.Diagnostics.Process]::Start($newProcess);
# Exit from the current, unelevated, process
Exit
} Else {
Throw "You must be administrator to run this script"
}
}
New-Item "Dir_Name" -ItemType Directory -Force
Remove-Item "my_file"
Remove-Item "folder" -Force -Recurse
If ([System.IntPtr]::Size -ne 4) {
Write-Output "Detected 64bit Architecture..."
...
} Else {
Write-Output "Detected 32bit Architecture"
Test-Path "file_path"
# Example
If (Test-Path "file_path") {
# Found file
Write-Output " - File Found . . ."
}
DownloadFileWithProgress $url $file
Where url is the actual http url from where you want to download file. And file is the destination file where you want to write.
Start-Process $file -ArgumentList <command options>
Example of installing NSIS,
# Downloading nsis
$file = "nsis.exe"
$url = "URL for NSIS"
DownloadFileWithProgress $url $file
Start-Process $file -ArgumentList '/S' -Wait -NoNewWindow -PassThru
To temporarily add a path to PATH variable,
$env:Path += ";my_path"
Note: Do not forgot to use +
. Else, it will overwrite previous path
$env:Path += ";my_path"
[Environment]::SetEnvironmentVariable
("Path", $env:Path, [System.EnvironmentVariableTarget]::Machine)
We will first get PATH variable, and check if our desired value is there. If its not there, we will add that.
$Path = (Get-ItemProperty -Path 'Registry::HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environment' -Name PATH).Path
$my_value = "xyz"
If (!($Path.ToLower().Contains("$my_value".ToLower()))) {
$newPath = "$my_value"
Set-ItemProperty -Path 'Registry::HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environment' -Name PATH -Value $newPath
$env:Path = $newPath
}
if ( (Get-ChildItem "my_folder | Measure-Object).Count -eq 0 ) {
# folder empty
...
}
For single file
Copy-Item "my_file" "des_dir" -Force
Move-Item "my_file" "des_dir"" -Force
For multiple files using *
Copy-Item "my_folder\*.dll" "dest_folder" -Force
Move-Item "my_folder\*.dll" "dest_folder" -Force
Example, your file path is: /my/path/file.txt
And, you want to replace STR_TO_REPLACE
string with target string TARGET_STRING
(Get-Content -path /my/path/file.txt -Raw) -replace 'STR_TO_REPLACE', 'TARGET_STRING' | Set-Content -Path /my/path/file.txt
And, if you have your value set in some environment variable name: MY_ENV_VAR
,
(Get-Content -path /my/path/file.txt -Raw) -replace 'BRANCH_NOT_SET', $env:MY_ENV_VAR | Set-Content -Path /my/path/file.txt
Lets implement a command shell by using a command dispatcher. The objective is…
You have drupal 7 image from docker hub, and want to connect tomongo db via php…
While running docker commands with some images, I started getting error: The…
Introduction In this post, I will show several ways to use conditionals while…
Introduction In this post, I will take example for Python project. And how we…
While doing code review of one of junior, I encountered following code: What…
Introduction Strapi is a backend system provides basic crud operations with…
Introduction I had to create many repositories in an Github organization. I…
Introduction I was trying to download some youtube videos for my kids. As I have…
Introduction In this post, we will explore some useful command line options for…
Introduction In this post, we will see how we can apply a patch to Python and…
Introduction We will introduce a Package Manager for Windows: . In automations…