Sunday, 2 December 2018

JSON Requests in PowerShell

PowerShell is extremely powerful. I am a trainer and consultant in various technologies like PowerShell, SharePoint, Javascript etc and no matter what the technology is, what is crucial for any technology? DATA

Data can be exchanged in various formats like Xml, Variables in parameters in URL.

What is the most recent way to exchange data? JSON

JSON is a different way of expressing the data. Lets take the example of object Apple.
The Apple is a fruit red in color.

Fruit named Apple
In computer language, we can express that as

var fruit;
fruit="apple";

For additional details, JSON comes to the rescue where color description can be given.

fruits="{'Name':'Apple','Color':'Red'}"

In Powershell, we can read JSON easily using ConvertTo and ConvertFrom Json function.PowerShell is very simple compared to other programming languages. One line of code!

Below is a script that converts the fruits JSON and displays that in beautiful table format.
$fruits="{'Name':'Apple','Color':'Red'}","{'Name':'Mango','Color':'Yellow'}"
$fruits |ConvertFrom-Json

In real world, I heavily use SharePoint and there are times when the JSON results are not converted sometimes. In that case, I have to do minor tweaks to display the JSON data in powershell. Below is a code that reads REST results from SharePoint, but the tabular data is not displayed.
$listName="PSRestList"
$restUrl="/_api/web/lists/GetByTitle('"+$listName+"')/items"
$listItems=Read-SPObject -targetSite $targetSite -User $User -restUrl $restUrl
$listItems

How to handle the error? By replacing the conflicting columns with another column.
#ID error
$results = $listItems.ToString().Replace("ID", "_ID") | ConvertFrom-Json
$results.d.results|select Title



Saturday, 1 December 2018

Updating Registry Values

PowerShell is simply superb. Windows changed the world by giving a computer for every Home. I love Windows and prefer Windows over Mac, since looks are always deceptive.

As a kid, I used to play on Windows games like Xatax and enjoyed BASICs programming in school. I had to type in the command prompt to login to Windows. Windows can be modified using Registry values. Just as A,B,C is fundamental to English. Registry values are low level Windows Fundamentals. This contains many Windows Configuration details.

You can open the Registry Editor by typing "regedit" in the command prompt(Start->Run). You are advised not to change any settings unless you are an administrator. In case , you are changing, take a backup and then change.The registry contains Key Value pairs.They are grouped under various folder like locations like HKEY_CURRENT_USER, HKEY_CURRENT_CONFIG. They are called hives, because the original Windows developer of NT version hated bees.

How can we view the registry values in PowerShell,example of HKEY_CURRENT_USER?
Make sure you have permission to view the folder:
READ:
Get-ChildItem -Path Registry::HKEY_CURRENT_USER

CREATE:
1. Take a backup of Registry using File->Export 
Save in a location like c:\backup\registry\backup.reg
You can restore in case of any errors by using File->Import from above location.

2. Suppose you want to add a "Open with notepad" icon, you need to add a registry value to 
"Open with notepad" under shell location
Change the value to notepad.exe %1

Below are the commands:
Push-Location
Set-Location -Path Registry::HKEY_CLASSES_ROOT
Test-Path ".\*\shell\Open With Notepad"
Set-Location -LiteralPath "Registry::HKEY_CLASSES_ROOT\*\shell"
New-Item -Name "Open With Notepad"
Set-Location -LiteralPath  "Registry::HKEY_CLASSES_ROOT\*\shell\Open With Notepad"
New-Item -Name "command"


UPDATE
Use Set-Item to update the key value
Set-Item  -LiteralPath "Registry::HKEY_CLASSES_ROOT\*\shell\Open With Notepad\command" -Value "notepad %1"



Reference:




Thursday, 9 February 2017

SharePoint Calculated Columns

SharePoint Calculated Columns are very interesting.


References:
https://msdn.microsoft.com/en-us/library/office/bb862071(v=office.14).aspx

Sunday, 5 February 2017

SharePoint and PowerShell PnP View Default Value of a Field

In this page, the steps are given to get the default value of a Choice Column in a list in SharePoint.

Open PowerShell and execute the following commands. Change the URL to your sharepoint URL and execute with your credentials

Install-Module -Name "SharePointPnPPowerShellOnline"
#Please change Demo to yourname in the url
$siteurl = "https://xxx.sharepoint.com/sites/XXX/Demo"

Connect-SPOnline -Url $siteurl -Credentials (Get-Credential)
#View all Fields for Student List
Get-PnPField -List "FruitsDemo1" -Identity "Color" |select DefaultValue

Friday, 9 December 2016

SharePoint PowerShell PnP

SharePoint and PowerShell PnP is soo cool.

Below are some of the snippets I have been playing with. Some of them are helpful for solving business requests asked by developers in the MSDN forums.

Small lines of code to get huge business requirements done in a fraction of a second...

1. Connection Code
$siteurl = "https://sharepointisgreat.sharepoint.com/sites/Dev"
$User = "admin@sharepointisgreat.onmicrosoft.com"
Connect-SPOnline -Url $siteurl -Credentials (Get-Credential)

2. List Operations
#Searching for command to add list
Get-Command *list*
help New-SPOLIst -Examples
New-SPOList -Title "SimplePSList" -Template GenericList -Url "SimplePSList"

#Add list item
help Add-SPOListItem -Examples
Add-SPOListItem -List "SimplePSList" -Values @{"Title"="Item1"}

#Update list item
help Set-SPOListItem -Examples
Set-SPOListItem -List "SimplePSList" -Identity 1 -Values @{"Title"="Item1-Updated"}

#Remove list item
help Remove-SPOListItem -Examples
Remove-SPOListItem -List "SimplePSList" -Identity 1

3. Adding Managed Metadata Column
#View all Taxonomy
Export-SPOTaxonomy -Path C:\assignments\Taxonomy.txt .. Get the term set path
$guid = [Guid]"ea45415d-f5a6-4344-b8a7-9b6c0d7fbf77"#New-Guid

#Add Field with GUID
Add-SPOTaxonomyField -List "StudentMMDemo" -DisplayName "Veggie" -InternalName "Veggie" -id $guid -TermSetPath "Edureka|Food|Vegetables"


#remove
Remove-SPOField -List "StudentMMDemo" -Identity "Veggie"

4. Add a Web Part to a Page /SitePages/samplePowerShell.aspx
$PageUrl = "/SitePages/samplePowerShell.aspx"
Add-SPOWebPartToWebPartPage -ServerRelativePageUrl $PageUrl -Path "C:\demo\SSOM\PowerShellCEWP.dwp" -ZoneId 0 -ZoneIndex 0  

5. Send Mail
Send-SPOMail -To $User -Subject "Mail From PowerShell" -Body "This is a sample mail from PowerShell code"

6. Add a file to a Document Library in SharePoint.Can also be used to move master pages.
Add-SPOFile -Folder "Shared Documents" -Path "C:\demo\UploadSP\Moving Full Trust Code to the Cloud.docx"

7. View Users
$siteurl = "https://sharepointisgreat-admin.sharepoint.com"
$User = "admin@sharepointisgreat.onmicrosoft.com"

Connect-SPOService -Url $siteurl -Credential (Get-Credential)
Get-SPOUser -Site $siteurl |select -Property *Login*

Saturday, 19 November 2016

Thank You Jeff for the PowerShell Jump Start tutorials

"Please paste the following in PowerShell ISE and execute"
$newline = "`r`n"
$message = ""
$message = $message +"`r`n"+[char[]][int[]](67,79,78,71,82,65,84,85,76,65,84,73,79,78,83)

$message = $message +"`r`n"+[char[]][int[]](69,84,69,82,78,65,76,32,83,85,67,67,69,83,83)

$message = $message +"`r`n"+"abcbestabcwishes".Replace("abc"," ")

$message = $message +"`r`n"+"You are soo brigxt ! Otxers need sunglasses!".Replace('x','h')

$message = $message +"`r`n"+"Wish u days full of qoney!".Replace('q','m')

$message = $message +"`r`n"+"May you always mile".Insert(15,'s')

$message = $message +"`r`n"+[char[]][int[]](72,97,112,112,121,32,70,111,114,101,118,101,114)

$message = $message +"`r`n"+[char[]][int[]](66,111,114,110,32,116,111,32,87,105,110)

$message = $message +"`r`n"+[char[]][int[]](69,116,101,114,110,97,108,32,83,116,114,101,110,110,103,116,104)

$message = $message +"`r`n"+[char[]][int[]](84,104,97,110,107,32,89,111,117)

Write-Host $message

Monday, 5 September 2016

PowerShell Real Time Examples

Below are some of the Real Time Examples of how SharePoint can saved, analysed or managed with the help of PowerShell:

1. Getting the Active Directory properties of SharePoint User.

SharePoint stores the user details in the form of claim token which needs to be decoded when we are trying to pull the details of user like the Site Collection of a Site Collection Administrator or Owner.
Here is the sample script which came to the rescue , instead of writing huge lines of code in C# in Visual Studio. Get-SPUser is fantastic if the User Profile stores all the AD details.

$ownerSP=Get-SPSiteAdministration -Url "http://w15-sp"|select OwnerLoginName
$m = [Microsoft.SharePoint.Administration.Claims.ClaimProviderManager]::Local
$ownerAD=$m.DecodeClaim($ownerSP.OwnerLoginName)
Get-ADUser -Identity $ownerAD -Properties * | select department,county