Monday, September 18, 2023

git stash usages

----
Sure, here are some examples of how you gcan use `git stash`:

1. **Save changes to a new stash**:
   
   - Suppose you're working on a new feature in a dedicated branch, but you need to switch to another branch to fix a bug.
   - You've made changes that you don't want to commit yet, so you can stash them. Here's how you can do it:

   ```bash
   git stash save "WIP on new feature"
   ```

   This command will save your changes to a new stash with the message "WIP on new feature". You can use any message that helps you remember what changes are in the stash.

2. **List all stashes**:
   - If you want to see all of the stashes that you've currently saved, you can use this command:

   ```bash
   git stash list
   ```

   This command will show you a list of all stashes that you currently have.

3. **Apply changes from the most recent stash**:
  - Once you're ready to continue working on your new feature, you can apply the changes from your most recent stash using this command:

   ```bash
   git stash pop
   ```

   This command will apply the changes from the most recent stash and then remove it from your list of stashes.

4. **Apply changes from a specific stash**:
  -  If you have multiple stashes and you want to apply changes from a specific one, you can do so with this command:

   ```bash
   git stash apply stash@{10}
   ```

   This command will apply the changes from the stash named `stash@{10}`.

5. **Discard the most recent stash**:
   - If you decide that you no longer need the changes in your most recent stash, you can discard it with this command:

   ```bash
   git stash drop
   ```

   This command will discard the most recent stash.

6. **Remove all stashes**:
   - If you want to remove all of your stashes at once, you can use this command:

   ```bash
   git stash clear
   ```

   This command will remove all of your stashes.

Note Remember, any changes that are stashed are kept locally and are not transferred when you push your commits to a remote repository.

Sunday, September 17, 2023

Ways of Identifying duplicate rows in SQL

----

To find duplicate rows using partition and rank, you can use the following steps:

- First, you need to use the PARTITION BY clause to divide the rows in your table into groups based on one or more columns that you want to compare for duplicates. For example, if you want to find duplicate rows based on the name and address columns, you can use PARTITION BY name, address.

- Next, you need to use the ORDER BY clause to specify the order of rows within each partition. You can use any column that you want to sort by, such as the ID column. For example, you can use ORDER BY ID.

- Then, you need to use the RANK () function to assign a rank to each row within each partition. The rank starts from 1 and increases by 1 for each row. If two or more rows have the same values in the partition columns, they will have the same rank. For example, you can use RANK () OVER (PARTITION BY name, address ORDER BY ID) AS rank.

- Finally, you need to select the rows that have a rank greater than 1. These are the duplicate rows that have the same values in the partition columns as another row. You can use a subquery or a common table expression (CTE) to filter these rows. For example, you can use:

```sql
-- Using a subquery

SELECT * FROM (
  SELECT *, RANK () OVER (PARTITION BY name, address ORDER BY ID) AS rank
  FROM table_name
) t
WHERE rank > 1;

-- Using a CTE

WITH cte AS (
  SELECT *, RANK () OVER (PARTITION BY name, address ORDER BY ID) AS rank
  FROM table_name
)

SELECT * FROM cte WHERE rank > 1;

```

There are some other ways to find duplicate rows in SQL, depending on the database system and the requirements. Here are some examples:

- You can use the EXISTS or NOT EXISTS operator to check if a row exists in a subquery that matches the values of another row. For example, you can use this query to find duplicate rows based on the name and address columns:

```sql
SELECT * FROM table_name t1 
WHERE EXISTS (
  SELECT 1 FROM table_name t2
  WHERE t1.name = t2.name
  	AND t1.address = t2.address
  	AND t1.id <> t2.id
);

```

- You can use the JOIN clause to join a table with itself and compare the values of the columns. For example, you can use this query to find duplicate rows based on the name and address columns:


```sql
SELECT t1.* FROM table_name t1
JOIN table_name t2
	ON t1.name = t2.name
	AND t1.address = t2.address
	AND t1.id <> t2.id;
```

- You can use the DISTINCT or GROUP BY clause to select only the unique values of the columns and then use the EXCEPT or MINUS operator to subtract them from the original table. For example, you can use this query to find duplicate rows based on the name and address columns:

```sql
SELECT name, address FROM table_name

EXCEPT

SELECT DISTINCT name, address FROM table_name;
```

To identify duplicate rows using group by in SQL, you can use the following steps:

- First, you need to select the columns that you want to check for duplicates using the SELECT statement. For example, if you want to find duplicate rows based on the name and address columns, you can use SELECT name, address.
- Next, you need to use the GROUP BY clause to group the rows by the selected columns. For example, you can use GROUP BY name, address.
- Then, you need to use the COUNT function in the HAVING clause to filter the groups that have more than one row. These are the groups that contain duplicates. For example, you can use HAVING COUNT (*) > 1.
- Finally, you need to execute the query and see the results. For example, the complete query would look like this:

```sql
SELECT name, address FROM table_name 
	GROUP BY name, address 
	HAVING COUNT (*) > 1;
```

This query will return all the rows that have duplicate values in the name and address columns.


Remove port in windows

----

To remove a port in Windows, you need to find and kill the process that is using that port. You can do that by following these steps:

- Open a command prompt as an administrator.
- Type the command `netstat -ano | findstr :8000` and press Enter. This will show you the process ID (PID) of the process that is using port 8000.
- Type the command `taskkill /F /PID ` and press Enter, where `` is the number you found in the previous step. This will force the process to terminate and free the port.
- You can verify that the port is no longer in use by typing the command `netstat -ano | findstr :8000` again and seeing no results.

```sh
	netstat -ano | findstr :8000
	taskkill /F /PID  8992  #  example PID 8992 which was return by above statement
```


Wednesday, September 13, 2023

Extract SQL query from Open Database connection in EXCEL

----
1. This script opens an Excel file, loops through each connection in the workbook, checks if the connection is an OLEDB connection,
2. then prints the command text (SQL query) of each OLEDB connection.
3. Rplace "C:\path\to\your\Spreadshee.xlsx" with the path to your Excel file1.

```sh
# Create an Excel COM object
$excel = New-Object -ComObject Excel.Application
Extracting SQL queries from Excel’s open data connection

# Open the workbook
$workbook = $excel.Workbooks.Open("C:\path\to\your\Spreadshee.xlsx")

# Loop through each connection in the workbook
foreach ($connection in $workbook.connections) {
    # Check if the connection is an OLEDB connection
    if ($connection.Type -eq 2) {
        # Get the OLEDB connection
        $oledbConnection = $connection.OLEDBConnection

        # Print the command text (SQL query)
        Write-Output $oledbConnection.CommandText
    }
}

# Close the workbook and quit Excel
$workbook.Close()
$excel.Quit()

# Release the COM objects
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($workbook) | Out-Null
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($excel) | Out-Null
[System.GC]::Collect()
[System.GC]::WaitForPendingFinalizers()

```
Please note that this script requires Excel to be installed on your machine, as it uses Excel’s COM object model.
Also, running scripts that use COM objects might have issues with non-interactive sessions (like a scheduled task or a remote session).

Extract Data from Excel using powershell

----

1. This script opens an Excel file and reads data from a sheet named Sheet. 

2. Replace "SELECT * FROM [Sheet1$]" with the query that suits your needs.

3. Also, please ensure that the Microsoft.ACE.OLEDB.12.0 provider is installed on your machine, as it’s required to read data from Excel1.


```sh

#Read data from Excel
$cnStr = "Provider=Microsoft.ACE.OLEDB.12.0;Extended Properties='Excel 12.0;HDR=YES';Data Source=c:temptest.xlsx;Mode=Share Deny Write;Jet OLEDB:Engine Type=37;"
$cn = New-Object System.Data.OleDb.OleDbConnection $cnStr
$cn.Open()
$cmd = $cn.CreateCommand()
$cmd.CommandText = "SELECT * FROM [Sheet1$]"
$rdr = $cmd.ExecuteReader()
$dt = new-object System.Data.DataTable
$dt.Load($rdr)
$dt | Out-GridView


```
Please note that this script reads data from Excel using SQL,but it doesn’t extract SQL queries stored in Excel. 
If you have SQL queries stored in cells of an Excel sheet and you want to extract them, you would need to adjust the script to select those specific cells.

Saturday, September 9, 2023

Get Team chat details using Microsoft.Graph

----
1. Install-Module Microsoft.Graph
2. Connect MicrosoftGraph API  Module
3. Get chat id using Get-MgChat commandlet
4. Get Chat member for particular chat id using Get-MgChatMember
5. Get chat messages\Chat history for particular chat using Get-MgChatMessage
```sh
Install-Module Microsoft.Graph
Import-Module Microsoft.Graph

Connect-MgGraph

<# "Get-MgChat" ,"Get-MgChatMember","Get-MgChatMessage" | 
& {Process{Find-mggraphcommand Get-MgChat|select permissions|
&{Process{Connect-MgGraph -Scopes $_}} }} #>

$displayName="< Display Name>"
$user=Get-MgUser  -Filter "DisplayName eq '$displayName'"

Get-MgChat  | select Topic,ChatType,id,WebUrl

$chatID=$(Get-MgChat  | select id -First 1).id


#Get all Chat members for chat 
Get-MgChatMember -ChatId $chatID | select AdditionalProperties).AdditionalProperties

#Get ACS chat members
(Get-MgChatMember -ChatId $chatID |Where DisplayName -eq raz| select AdditionalProperties).AdditionalProperties

#Get AAD chat members
(Get-MgChatMember -ChatId $chatID |Where DisplayName -eq 'razesh'| select AdditionalProperties).AdditionalProperties

#Get chat messages for chat id

Get-MgChatMessage -ChatId $chatID |& {Process{ if ($($_.From.User.DisplayName)) {Write-host "From :$($_.From.User.DisplayName)" ;"Message:$($_.body.Content)";  Write-Host ""}}}

```

Invoke Microsoft Graph API endpoint using accesstoken in az cli


----

To get an access token for Graph API call, you can follow the steps below:


Go to the Azure portal and sign in to your account.
Navigate to the Azure Active Directory resource that you want to use.
1. Click on the App registrations tab.
2. Select the app that you want to use.
3. Click on the Certificates and  secrets tab.
4. Click on the New client secret button.
5. Enter a description for the client secret and select an expiration date.
6. Click on the Add button.
7. Copy the value of the client secret and store it securely.
8  Use the copied client secret value to authenticate your requests.
9. You can use the following code snippet in PowerShell to get an access token:


```sh
$tenantId = "< your-tenant-id >"
$appId = "<your-app-id >"
$clientSecret = "< your-client-secret >"
$resource = "https://graph.microsoft.com"
$body = @{grant_type="client_credentials";client_id="$appId";client_secret="$clientSecret";resource="$resource"}
$oauthUrl = "https://login.microsoftonline.com/$tenantId/oauth2/token"
$oauth = Invoke-RestMethod -Method Post -Uri $oauthUrl -Body $body
$accessToken = $oauth.access_token

# Note: Replace < your-tenant-id >, < your-app-id >, and < your-client-secret  > with your own values.
```

----

Example 1: 


```sh
$tenantId = "< your-tenant-id >"
$appId = "<your-app-id >"
$clientSecret = "< your-client-secret >"
$resource = "https://graph.microsoft.com"
$body = @{grant_type="client_credentials";client_id="$appId";client_secret="$clientSecret";resource="$resource"}
$oauthUrl = "https://login.microsoftonline.com/$tenantId/oauth2/token"
$oauth = Invoke-RestMethod -Method Post -Uri $oauthUrl -Body $body
$accessToken = $oauth.access_token

$headers = @{
    "Authorization" = "Bearer $accessToken"
}
$response=Invoke-RestMethod -Uri "https://graph.microsoft.com/v1.0/users" -Headers $headers 
$response.value  | select id , displayname

# Note: Replace < your-tenant-id >, < your-app-id >, and < your-client-secret  > with your own values.
# Note: Need User.Read.All, User.ReadWrite.All, Directory.Read.All, Directory.ReadWrite.All Permissions to access MS graph users API endpoint
```


Example 2:

```sh
$tenantId = "< your-tenant-id >"
$appId = "<your-app-id >"
$clientSecret = "< your-client-secret >"
$resource = "https://graph.microsoft.com"
$body = @{grant_type="client_credentials";client_id="$appId";client_secret="$clientSecret";resource="$resource"}
$oauthUrl = "https://login.microsoftonline.com/$tenantId/oauth2/token"
$oauth = Invoke-RestMethod -Method Post -Uri $oauthUrl -Body $body
$accessToken = $oauth.access_token

$headers = @{
    "Authorization" = "Bearer $accessToken"
}
$response=Invoke-RestMethod -Uri "https://graph.microsoft.com/v1.0/users" -Headers $headers 
$response. value  | select id , displayname

(Invoke-RestMethod -Uri "https://graph.microsoft.com/v1.0/groups?$filter=resourceProvisioningOptions/Any (x:x eq 'Team')" -Headers $headers).value |select id , displayname , mailNickName , mail


# Note: Replace < your-tenant-id >, < your-app-id >, and < your-client-secret  > with your own values.
# Note: Need 	Group.Read.All, Group.ReadWrite.All, Directory.Read.All, Directory.ReadWrite.All  Permissions to access MS graph groups API endpoint

```



Example 3:

```sh
$tenantId = "< your-tenant-id >"
$appId = "<your-app-id >"
$clientSecret = "< your-client-secret >"
$displayName ="< displayName>"
$resource = "https://graph.microsoft.com"
$body = @{grant_type="client_credentials";client_id="$appId";client_secret="$clientSecret";resource="$resource"}
$oauthUrl = "https://login.microsoftonline.com/$tenantId/oauth2/token"
$oauth = Invoke-RestMethod -Method Post -Uri $oauthUrl -Body $body
$accessToken = $oauth.access_token

$headers = @{
    "Authorization" = "Bearer $accessToken"
     "Content-Type"  = "application/json"
}

$filter='$filter'
$uri = "https://graph.microsoft.com/v1.0/users?$filter=displayName eq '$displayName' "
$response=Invoke-RestMethod -Uri $uri -Headers $headers 
$response. value  | select id , displayname

# Note: Replace < your-tenant-id >, < your-app-id >, and < your-client-secret  > with your own values.
# Note: Need User.Read.All, User.ReadWrite.All, Directory.Read.All, Directory.ReadWrite.All Permissions to access MS graph users API endpoint

```

How to invoke Azure Management API with Access token using AZ CLI


#### list of steps to invoke Azure Management API with Azure access token using AZ CLI.

1. log into Azure  using Az CLI 
2. Get access token using account get-access-token 
3. add bearer accesstoken as authorization Header
4. Invoke Azure Managment API 

```sh

az login
$subId=(az account list --query "[?isDefault].id" )|ConvertFrom-Json
$accesstoken =  [string](az account get-access-token | ConvertFrom-Json).accessToken
$headers = @{ Authorization = "Bearer $accesstoken" }
$url="< Azure Management API >"
$r= Invoke-WebRequest  -Uri  $url -Headers $headers
If ($r.StatusCode -eq 200 ) {($r.Content |ConvertFrom-Json).value}

# Replace  < Azure Management API > with your values  
```

Example: 

```sh
az login
$subId=(az account list --query "[?isDefault].id" )|ConvertFrom-Json
$accesstoken =  [string](az account get-access-token | ConvertFrom-Json).accessToken
$headers = @{ Authorization = "Bearer $accesstoken" }
$url="https://management.azure.com/subscriptions/$subId/providers/Microsoft.Compute/locations/eastus/runCommands?api-version=2022-11-01"
$r= Invoke-WebRequest  -Uri  $url -Headers $headers
If ($r.StatusCode -eq 200 ) {($r.Content |ConvertFrom-Json).value}
```
________


App Registration in Azure

----

#### Register App using AZ Cli 

1. Login into azure using az cli 
2. Create azure active directory service principle usign AZ CLI

```sh

$AppName="<AppName >"
az login
$subId=az account list --query "[?isDefault].id" | ConvertFrom-Json
az account set --subscription $subid
az ad sp create-for-rbac -n $AppName
```
-----

#### Register App using AZ Portal



1. Go to the Azure portal and sign in to your account.
2. Navigate to the Azure Active Directory resource that you want to use.
3. Click on the App registrations tab and click on New registration
4. Enter User-facing Display name  and click on Register

Saturday, September 2, 2023

Create Azure Communication identity and token using az cli


### Add the Azure Communication Services extension for Azure CLI by using the az extension command.

1. Add communication extension in az cli  (one time activity)
2. Connect to Azure using Az Cli
3. Create Identity using az communication user-identity 
4. Issue Identity Token for specific scope (voip, chat)


```sh 
az extension add --name communication
$acsName="< acs-Name >"
$resouceGroup="< Resource Group Name >"

$connection=az communication list-key --name $acsName --resource-group $resouceGroup  --query "primaryConnectionString"

$rawId=az communication user-identity user create --connection-string $connection  --query "rawId"

az communication identity token issue --scope chat voip  --user $rawId --connection-string $connection  --query "token"


Note:

#$rawId=az communication identity user create --connection-string $connection  --query "rawId"

#This command is implicitly deprecated because command group 'communication identity token' is deprecated and will be removed in a future release. Use communication user-identity token' instead.
```
___

Example :

```sh 

az extension add --name communication

az login

$acsName="aztds-acs-poc0821"

$resouceGroup="rg-aztds-acs-poc"

$connection=az communication list-key --name $acsName --resource-group $resouceGroup  --query "primaryConnectionString"

$rawId=az communication user-identity user create --connection-string $connection  --query "rawId"

az communication identity token issue --scope chat voip  --user $rawId --connection-string $connection  --query "token"

```

Get Teams User's Availability using Microsoft Graph API


Get Teams User's Availability using Microsoft Graph API through Invoke-RestMethod 


```sh
# Import the required modules

Install-Module -Name AzureAD
Install-Module MicrosoftTeams


Import-Module AzureAD
Import-Module MicrosoftTeams

	# Connect to Azure AD
Connect-AzureAD

	# Get an access token for the Graph API
$accessToken = (Get-AzureADToken -Resource "https://graph.microsoft.com").AccessToken

	# Set the Graph API endpoint URL
$graphApiUrl = "https://graph.microsoft.com/v1.0/users//presence"

	# Set the headers for the request
$headers = @{
    "Authorization" = "Bearer $accessToken"
}

	# Send the request to the Graph API and get the response
$response = Invoke-RestMethod -Method Get -Uri $graphApiUrl -Headers $headers

	# Output the presence status of the user
$response.availability
```

Get Teams User id using Microsoft Grapth API


Get Teams User id using Microsoft Grapth API

```sh
Install-Module Microsoft.Graph

# Import the required module
Import-Module Microsoft.Graph

# Connect to Microsoft Graph
Connect-MgGraph

# Get the user object for the specified user
$user = Get-MgUser -Filter "mail eq '[email protected]'"

# Output the user ID
$user.Id

```

Post message using Microsoft Graph powershell moduel

Post message using Microsoft Graph powershell moduel <\i>


```sh
Install-Module Microsoft.Graph -Scope CurrentUser
Install-Module Microsoft.Graph.Beta


Import-Module Microsoft.Graph -Scope CurrentUser

# Connect to MgGraph

Connect-MgGraph


Find-MgGraphCommand -command Get-MgUser | Select -First 1 -ExpandProperty Permissions

#Grant permission

Connect-MgGraph -Scopes "User.Read.All","Group.ReadWrite.All"

$user = Get-MgUser -Filter "displayName eq 'Raj'"

# Get Teams for user 
Get-MgUserJoinedTeam -UserId $user.Id

# Get Teams for user 

$TeamId=$(Get-MgUserJoinedTeam -UserId $user.Id).ID
$team = Get-MgTeam -TeamId $TeamId

# Get Team channel  for specific Team

$channel = Get-MgTeamChannel -TeamId $team.Id -Filter "displayName eq 'General'"

# Post message in specific Team channel
New-MgTeamChannelMessage -TeamId $team.Id -ChannelId $channel.Id -Body @{ Content="Hello Teams"} 

# Post message in specific Team channel
New-MgTeamChannelMessage -TeamId $team.Id -ChannelId $channel.Id -Body @{ Content="Hello World" } -Importance "urgent"

# DisConnect to MgGraph
Disconnect-MgGraph
 
```

clone git repository with specific branch only

clone git repository with specific branch only

```sh
#syntax

git clone --single-branch --branch <branch-name >  git-url

#example
git clone --single-branch --branch master https://github.com/career-tutorials/ck.git --progress

```

Re install NPM Packages

Reinstall all NPM Packages

 Remove modules under node_modules on Linux or Mac OS 
```sh
rm -rf node_modules && npm install
```

 Remove modules under node_modules on Windows 
```sh
del /s /q node_modules && npm install
```

 Clean  modules from cache , rebuild  and install packages 
```sh 
npm cache clean --force
npm rebuild
npm install

```
 The npm audit fix command will install any packages that are missing in node_modules but it’s present in package.json 

```sh
 npm audit fix
```

 npm update will update any packages that are present in the project.

```sh
npm update

#so we can run this to reinstall any packages that are missing.

```

Availability of Teams user using Microsoft.Graph and Microsoft.Graph.CloudCommunications communication module

Availability of Teams user using Microsoft.Graph and Microsoft.Graph.CloudCommunications Poweshell module

```sh
#Install Module 
Install-Module Microsoft.Graph -Scope CurrentUser
Install-Module Microsoft.Graph.CloudCommunications

#Import Module 
Import-Module Microsoft.Graph
Import-Module Microsoft.Graph.CloudCommunications

# Connect to Microsoft Graph

Connect-MgGraph

# Fina required permission for Get-MgCommunicationPresenceByUserId

Find-MgGraphCommand -command Get-MgCommunicationPresenceByUserId  | Select -First 1 -ExpandProperty Permissions

# Grant scope of access 
Connect-MgGraph -Scopes "Presence.Read.All","Presence.Read"

# get user details using  Microsoft Graph
$user1 = Get-MgUser -Filter "displayName eq 'Rajesh'"
$user2 = Get-MgUser -All | Where-Object {$_.DisplayName -like "*Mark*"}
$params = @{
			ids = @(
                  $user1.Id
                  $user2.Id
				)
		}


# Get Presence  by user id 
#Get-MgCommunicationPresenceByUserId -BodyParameter $params


# Get Presence by user id for Availability other than offline ,DoNotDisturb ,BeRightBack, Away

Get-MgCommunicationPresenceByUserId -BodyParameter $params | where Availability  -NotIn 'Offline','DoNotDisturb','BeRightBack','Away'

# Get Presence by user id who are available 

Get-MgCommunicationPresenceByUserId -BodyParameter $params | where Availability  -EQ 'Available'


#There is other way Get Presence for user id
 
 Get-MgUserPresence -UserId $user1.Id


```