Showing posts with label Debian 10. Show all posts
Showing posts with label Debian 10. Show all posts

Saturday, May 2, 2020

Adding datestamp to a filename using a function with a filename as a parameter in Powershell 7 in Debian 10

function adddatetofilename{
    Param(
        [Parameter(Mandatory=$true,position=1)]
        [string]$filename

    )
   
#$filename="\home\sourav\test.txt"
new-item -type file -name $filename
$todaysdate=get-date -Format MMddyy
$file=Get-ChildItem $filename 
$newfilename=$file.BaseName+"."+$todaysdat+$file.Extension
Rename-Item -path $filename -NewName $newfilename -verbose
}
adddatetofilename -filename ./test.txt

Adding a datestamp to a filename in powershell 7 in Debian 10

create a test file

new-item -type file -name test.txt

$filename="\home\sourav\test.txt"

$todaysdate=get-date -Format MMddyy

$file=Get-ChildItem $filename  

$newfilename=$file.BaseName+"."+$todaysdat+$file.Extension

Rename-Item -path $filename -NewName $newfilename -verbose

Sunday, April 26, 2020

Understanding variables and editing the powershell profile for custom message when logging in in powershell 7 in Debian 10

creating variable

$a=1
$b=2
$c=$a+$b

to see the type of the variable

$a | Get-Member

gm is the alias of Get-Member

so we can do

$a | gm

as gci is the alias of get-child-items

we can do

$items=gci

$items|gm

to get the processes

$processes=get-process

$process | gm

by default the variables are loosely typed,for some reason if we want strict type variables
we need to cast it like this

[int]$dozen=12

if we get the type

$dozen|gm

it will be shown as a integer

Now I can not assign a string inside it for obvious reasons

 $dozen="kshinfo"

so the above line will not execute

to see all the variables

get-variable

in the output we will see the profile.sh file

now from terminal to edit the profile file

code $PROFILE

will open the profile in vscode if it is installed

Now to get a custom message like a greeting
showing the time and the day

we need to write

#Create a greeting that says good morning or good afternoon based on time of the
day
$date=Get-Date

$hour=$date.Hour

$dayofweek=$date.DayOfWeek

if ($hour -lt 12)

{
$greeting="Good Morning,"

}

else
{

    $greeting="Good Evening,"
    }

Write-Host "$greeting I hope you are having a great $dayofweek"





save the file and close it and restart the powershell session

you will see the custom prompt


Saturday, April 25, 2020

Install VSCode in Debian10 as powershell editor

curl https://packages.microsoft.com/keys/microsoft.asc | gpg --dearmor > packages.microsoft.gpg
sudo install -o root -g root -m 644 packages.microsoft.gpg /usr/share/keyrings/
sudo sh -c 'echo "deb [arch=amd64 signed-by=/usr/share/keyrings/packages.microsoft.gpg] https://packages.microsoft.com/repos/vscode stable main" > /etc/apt/sources.list.d/vscode.list'

Then update the package cache and install the package using:

sudo apt-get install apt-transport-https
sudo apt-get update
sudo apt-get install code # or code-insiders


Source:https://code.visualstudio.com/docs/setup/linux

Thursday, April 23, 2020

Exporting command output to csv,import csv and see the content as a formatted table in powershell 7 in Debian 10

exporting command output to a csv file

get-process |  sort-object -property ws -descending | select-object -property processname,ws -first 5|Export-csv /home/sourav/Desktop/top5proc.csv


to see the content of the csv

 Get-Content ./Desktop/top5proc.csv


to impoer the csv file

import-csv ./Desktop/top5proc.csv  

if there are lots of data in the csv file we can format the output which will produce data in a more readable format

import-csv ./Desktop/top5proc.csv|Format-Table

Using Pipes in Powershell 7 on Debian 10

using get-process we get a detailed info of processes running

from that output if we only want to filter the output by just processname,id,ws we need to use

 get-process | select-object -Property processname,id,ws

to sort the output with a particular column like working set(ws) which lists processes based on memory usages in a ascending order


to see the same result in a descending manner

 get-process | select-object -Property processname,id,ws | sort-object -property ws -descending

to see the top 5 processes as they are consuming memory in a descending manner

get-process |  sort-object -property ws -descending | select-object -property processname,ws -first 5


Tuesday, April 21, 2020

Powershell basics on linux part 1,creating files and folders,deleting files and folders,creating multiple files with variable names using batch processing using powershell 7 on Debian 10

powershell basics

get-childitem
to see the subdirectories in the current directory


get-process
to see the current processes


Create a new folder

New-Item -Type Directory -name FileTest

we can use ls to see if it is created

To see the current working directory or pwd

get-location

Now to get inside a folder or cd

set-location './FileTest/'

to see the contents inside that folder

get-childitem

create a text file

new-item -name test1.text


to delete the text file

remove-item ./test1.text

Using Linux binary or tools installed nside powershell

nano test2.text

write something and save it by ctrl+x

to see the content of the file

Get-Content ./test2.text

to get to the parent folder or cd .. we have to use

set

Set-Location ..

to delete the folder

 Remove-Item ./FileTest/

in this case you will be prompted for confirmation

to create 10 text files with names like 1 to 10

1..10|%{new-item -name "test$_.text"}

ls to see the files created like

test1.txt,test2,txt.... test10.txt


Install powershell 7 on Debian 10

See the version of Debian

lsb_release -a

First I need to access all the commands stored in /usr/local/sbin and /usr/sbin

I need to edit the .bashrc file in my user's home profile and the last two lines at the end

export PATH=$PATH:/usr/local/sbin
export PATH=$PATH:/usr/sbin



save the file 


and 

Source .bashrc

to reload the file to see the change


to use the old ifconfig

sudo apt install net-tools



to see the dynamically generated ip 

ifconfig

I enabled ssh while installing so now using tool like putty I can access debian from outside

Now let me install cshell zshell and korshell

 sudo apt install csh zsh ksh

Now I am using Debian 10 which I can verify by using  lsb_release -a command

So to install powershell I have to use these commands



# Download the Microsoft repository GPG keys
wget https://packages.microsoft.com/config/debian/10/packages-microsoft-prod.deb

# Register the Microsoft repository GPG keys
sudo dpkg -i packages-microsoft-prod.deb

# Update the list of products
sudo apt-get update

# Install PowerShell
sudo apt-get install -y powershell

# Start PowerShell
pwsh





Source:https://docs.microsoft.com/en-us/powershell/scripting/install/installing-powershell-core-on-linux?view=powershell-7