Wednesday, August 30, 2023

Performance of process, CPU Utilization and Memory Utilization for specific system

Performance of each process in the machine

```sh
	$servername ="Machine1" # update Machine \hostname
	gwmi Win32_PerfFormattedData_PerfProc_Process -filter "Name <> '_Total' and Name <> 'Idle'" -Computer $servername | 
    where { $_.PercentProcessorTime -gt 0 } | 
    select PSComputerName, Name, PercentProcessorTime | 
    sort PercentProcessorTime -Descending
```
CPU Utilization

```sh
		$serverName ="Machine1"  # update Machine \hostname
		$ProcessorTime = (Get-WmiObject -ComputerName $serverName -Class win32_processor -ErrorAction Stop |
        	Measure-Object -Property LoadPercentage -Average | 
            Select-Object Average).Average
		$ProcessorTime
```
Memory Utilization

```sh
	$serverName ="Machine1"  # update Machine \hostname
	$ComputerMemory = Get-WmiObject -ComputerName $ServerName -Class win32_operatingsystem -ErrorAction Stop;
	$MemoryUtilization  = ((($ComputerMemory.TotalVisibleMemorySize - $ComputerMemory.FreePhysicalMemory)*100)/ $ComputerMemory.TotalVisibleMemorySize); $RoundMemory = [math]::Round($Memory, 2)
	$MemoryUtilization
```

Saturday, August 26, 2023

How to get Teams User id using Microsoft Teams Module


We can get Teams user id  using MicrosoftTeams powershell module

 1.Install  MicrosoftTeams Powershell module

```sh
# Install  Microsoft Team  powershell module

Get-module *teams* 

Install-Module -Name MicrosoftTeams

Update-Module MicrosoftT

```

 2.Connect to Team Tenant 

```sh
#  Connect to Team Tenant 

Connect-MicrosoftTeams 

# It will prompt for AAD Login else # or  you can specify credentials through script
$credential = Get-Credential
Connect-MicrosoftTeams -Credential $credential

```
` Note :Unfortunately, it is not possible to connect to Microsoft Teams using the Connect-MicrosoftTeams cmdlet with **MFA enabled without using an interactive 	  login**. If you want to automate the process of connecting to Microsoft Teams, you may need to use a service account that does not have MFA enabled or use a different method of authentication, such as using a certificate or an application ID


 3.Get group id for Your teams group
```sh
$groupid=$(Get-Team -DisplayName 'Your-Teams-Group' |select GroupId)
```


 4.Get teams UserId

```sh

$users=$(Get-TeamUser   -GroupId $groupid  )
$UserID=$(Get-TeamUser   -GroupId $groupid  | where name -EQ "Your-User-Name").UserID

```


Complete Script 

```sh

Get-module *teams* 
Install-Module -Name MicrosoftTeams
Update-Module MicrosoftTeams 
Connect-MicrosoftTeams
$groupid=$(Get-Team -DisplayName 'Notifications' |select GroupId).groupid

$UserID=$(Get-TeamUser   -GroupId $groupid  | where name -eq "Raj").UserI
```

Monday, August 21, 2023

Invoke Web request by -passing proxy


We can invoke web request by passing proxy if proxy is configured at windows level
```sh 
 $request=[System.Net.WebRequest]::Create("https://test/v1/healtcheck")
 $request.Proxy=[System.Net.WebProxy]::new() #blank proxy
 $response=$request.GetResponse()
 
```

Tuesday, August 15, 2023

Dotnet SDK installation using package manager CLI in Windows and Linux

#### Install .NET updates using the Windows Package Manager CLI (winget):


 - Install the .NET 7 sdk 

```sh
	winget install dotnet-sdk-7
```

 - Install the .NET 7 runtime, run 

```sh
	winget install dotnet-runtime-7

```

 - To update an existing installation: 

```sh

	winget upgrade
    
    winget upgrade --include-unknown 
```



  
 - Install dotnet SDK using packages in Linux 
  
 
```sh

#update all packages before install .net 

sudo apt update 

# following command will install dotnet core 2.1, 2.2, 3.0 and 3.1

sudo apt install dotnet-sdk*

# following command will install dotnet 6.0  if not able to find package then we can install using manuall steps using below steps 
sudo apt install dotnet-sdk-6.0

# following command will install dotnet 7.0  if not able to find package then we can install using manuall steps using below steps 
sudo apt install dotnet-sdk-7.0

#following command will install runtime
	sudo apt install dotnet-host

#following command will upgrade\update dotnet SDK package
sudo apt upgrade dotnet-sdk-7.0


#uninstall dotnet SDK packges using apt remove
sudo apt-get remove dotnet-sdk-6.0

sudo apt remove dotnet*
sudo apt remove netstandard*
sudo apt remove aspnetcore*

```




###  Install dotnet 6.0  7.0 SDK in Linux using manual steps 

```sh
wget https://dot.net/v1/dotnet-install.sh -O dotnet-install.sh
chmod +x ./dotnet-install.sh


# following command will install dotnet  6.0 version
./dotnet-install.sh --version latest


# following command will install dotnet  7.0 version
./dotnet-install.sh --channel 7.0

#Add below environment variable to add dotnet path in path variable 

export DOTNET_ROOT=$HOME/.dotnet

export PATH=$PATH:$DOTNET_ROOT:$DOTNET_ROOT/tools

#Note- add these environment variables in   ~/.bashrc ~/.profile to avoid define these variables for each linux instance
```


###  Install dotnet 8.0 preview SDK in Linux using manual steps 


```sh
# following command will install dotnet  8.0 preview version

wget https://download.visualstudio.microsoft.com/download/pr/32f2c846-5581-4638-a428-5891dd76f630/ee8beef066f06c57998058c5af6df222/dotnet-sdk-8.0.100-preview.7.23376.3-linux-x64.tar.gz -O  dotnet-sdk-8.0.100-preview.7.23376.3-linux-x64.tar.gz

mkdir -p $HOME/.dotnet && tar zxf dotnet-sdk-8.0.100-preview.7.23376.3-linux-x64.tar.gz -C $HOME/.dotnet

export DOTNET_ROOT=$HOME/.dotnet

export PATH=$PATH:$HOME/.dotnet

```
### Install project templates using dotnet CLI in windows \Linux 

```sh 
# syntax 

dotnet new install <TemplateName>

# we can install templates for chat bot projects using dotnet cli in windows\linux

dotnet new install Microsoft.Bot.Framework.CSharp.CoreBot

dotnet new install Microsoft.Bot.Framework.CSharp.EmptyBot

dotnet new install Microsoft.Bot.Framework.CSharp.EchoBot


# we can install templates for clean Architecture  using dotnet cli in windows\linux

dotnet new install Ardalis.CleanArchitecture.Template

dotnet new install Clean.Architecture.Solution.Template::8.0.0-preview.6.18


#  to list of installed templated 

dotnet new uninstall

```
### The .NET entity framework is  available as a .NET global tool. We can install the tool with the following command in windows \Linux OS

```sh
dotnet tool install --global dotnet-ef

```

### The .NET Upgrade Assistant is also available as a .NET global tool. We can install the tool with the following command in windows \Linux OS

```sh
	dotnet tool install -g upgrade-assistant
```

 - NET Upgrade Assistant is installed as a .NET tool, it can be easily updated by running following command in windows \Linux OS

```sh
    dotnet tool update -g upgrade-assistant
```

Now we can upgrade existing project to new .net framework

```sh
  #usage
	
  upgrade-assistant upgrade
  
  upgrade-assistant upgrade 
  
  upgrade-assistant upgrade  --operation Inplace --targetFramework net6.0
  
  upgrade-assistant upgrade  --operation SideBySide --targetFramework LTS --destination  
  
```





Sunday, August 6, 2023

How to get New GUID in bat programming



We can generate new GUID using command let GUID class in Powershell  

```sh
New-GUID

[guid]::NewGuid().ToString()
```

We can leverage invoking poweshell from batch programming 

```sh

POWERSHELL -COMMAND "$([guid]::NewGuid().ToString())"


FOR /F %a IN ('POWERSHELL -COMMAND "$([guid]::NewGuid().ToString())"') DO ( SET NEWGUID=%a )
echo %NEWGUID%

```

How to generate Sequence dates, Random number table in Excel


We can generate sequence Dates using Sequence functions.

```sh

#Following is the formula to generate sequence Dates

=TEXT(DATE(2024,1,1)+SEQUENCE(1,7,0,1),"DD-mmm-YYYY")

#Following is the formula to generate sequence Months

=TEXT(DATE(YEAR(TODAY()),MONTH(TODAY())+SEQUENCE(1,7),DAY(TODAY())),"mmm-YYYY")

#Following is the formula to generate sequence Years

=TEXT(DATE(YEAR(TODAY()+SEQUENCE(1,7)),MONTH(TODAY()),DAY(TODAY())),"YYYY")

```


We can codes\ids with particular number increment 


```sh
#Following is the formula to generate codes \ids in 5 rows with starts with 1000 and  increment of 1000
=SEQUENCE(5,1,1000,1000)

#Following is the formula to generate codes \ids in 5 columns with starts with 1001 and  increment of 1000
=SEQUENCE(1,5,1001,1000)
```

We can generate table with random numbers 

```sh
=SEQUENCE(5,5,INT(RAND()*100),INT(RAND()*100))
```

Saturday, August 5, 2023

How to install dotnet cli in Linux or WSL



1. Download Dotnet-install shell script
```sh
wget https://dot.net/v1/dotnet-install.sh -O dotnet-install.sh
chmod +x ./dotnet-install.sh
```

2. Install latest.NET  SDK using dotnet-install.sh

```sh
./dotnet-install.sh --version latest

```


2.1 Install .NET Runtime instead of the SDK, use the --runtime parameter.
```sh
./dotnet-install.sh --version latest --runtime aspnetcore
```

You can install a specific major version with the --channel parameter to indicate the specific version. The following command installs .NET 7.0 SDK.

```
Bash
./dotnet-install.sh --channel 7.0
```

3. Set Environment variables for DOTNET_Root and update Path environment variable

```sh 
~/.bashrc

export DOTNET_ROOT=$HOME/.dotnet

export PATH=$PATH:$DOTNET_ROOT:$DOTNET_ROOT/tools

```


How to call PowerShell command -let or PowerShell script from C#


We can call poweshell command-let from C#


```sh 

Install Powershell SDK package then  Add reference of System.Management.Automation
  #Install-Package Microsoft.PowerShell.SDK#
  #dotnet add package Microsoft.PowerShell.SDK
```

Following is the code to invoke powershell command-let

```sh
#usings -
  using System.Management.Automation;
  using System.Management.Automation.Runspaces;


  using (PowerShell ps = PowerShell.Create()){
      ps.AddCommand("Get-Process");
      ps.AddParameter("Name", "excel");

      foreach (PSObject result in ps.Invoke()){
         Console.WriteLine(result);
      }
}

```

We can call powershell script file  from C#

```sh
Following is the code to invoke powershell  script

#usings
 using System.Management.Automation;
 using System.Management.Automation.Runspaces;

 var ps1File = @"C:\Users\\install-scheme.ps1";
 var startInfo = new ProcessStartInfo(){
                FileName = "powershell.exe",
                Arguments = $"-NoProfile -ExecutionPolicy ByPass -File \"{ps1File}\"",
                UseShellExecute = false
            };
 Process.Start(startInfo);
```

We can invoke powershell command-let \cli from C# using CliWrap 

```sh 
#Install Package 
dotnet add package CliWrap -Version 3.6.4 # Install-Package CliWrap -Version 3.6.4

# using 
  using CliWrap;
  using CliWrap.Buffered;
  using System;
  using System.Threading;
  using System.Threading.Tasks;
   


var result= await cli.Wrap("powershell")
                     .WithArguments(new[]{"Get-Process"," -Name",\"devenv"})
                     .ExecuteBufferedASync();
Console.WriteLine(result.StandardOutput);
Console.WriteLine(result.StandardError);

```




```

Friday, August 4, 2023

How to generate sequence id number in Excel


We can generate sequence number using row is haivng data 


Following is the formula to generate sequence number in A2 column when B2 column 
```sh
	IF(ISBLANK(B2),"",COUNTA(B$2:B2))

# We can also do it with sequence formula , Note 1 is offset  if we used first row for header

SEQUENCE(COUNTA(B:B)-1)

```

We can do it both COUNTA and Sequence when data in column B is not empty

```sh
	IF(ISBLANK(B2),"",SEQUENCE(COUNTA(B$2:B2)))

	# if want to fill the sequence number when entered data in cell
 	Ctrl+G   then cick on Special then click on blanks
```