Wednesday, 22 April 2020

How to create a certificate for EXE on Windows 10

Steps:

1. Download the Windows 10 SDK
https://developer.microsoft.com/en-US/windows/downloads/windows-10-sdk/

2. Open Command Prompt CMD as Administrator

Go to the folder with Sign Tool
C:\Program Files (x86)\Windows Kits\10\App Certification Kit


signtool sign /a MyFile.exe  TO get a valid certificate try the link https://www.certum.eu/en/cert_offer_en_open_source_cs/

Thursday, 27 February 2020

Get Windows Details

Get Windows Details

Below is a short script to get Windows Edition

$windowsDetails=Get-WmiObject -Class Win32_OperatingSystem |select Caption

If you are interested in more Windows related commands, see here 

Here is a sample video of the script that allows you to display the report as shown below:

You can access the running tool from here:


If you have a custom requirement like this, mention in the comment box and I will try to update the scripts to your custom requirement when I am free.

If you have any other custom report requirements for other objects like Office 365,SharePoint, SQL or any other System, you may contact https://calendly.com/spthangu for Professional consulting for small requirements that take 5mins to 1 hour max for a small charge of 10$-50$.





Thursday, 12 September 2019

PowerShell Here String

Here String

This is a short blog on Here String. I decided to write down about this since I wanted to share the experiences with PowerShell features, that I heavily use.

I commonly use the following type of code to store large strings in a variable.
 
$data=@"
{
  "@odata.id": "https://graph.microsoft.com/v1.0/directoryObjects/$($userID)"
}
"@

Why do I use the above instead of using a normal string?
$data="{
  "@odata.id": "https://graph.microsoft.com/v1.0/directoryObjects/$($userID)"
}"

Reasons:
1.The above code throws error due to the presence of string with double quotes
2. Using back ` is terrible for strings with many double quotes .

What was I doing to allow double quotes and have large strings declared in a variable?
I used @ at the end and beginning of the string and the string was able to store large data.

I did not know that the term used for this feature was Here string until I read various msdn blogs on PowerShell and came to know the Here String term.

What is Here String?

A simple String that allows you to declare multi ling string along with Quotes and variable values.
Make sure that you have a line break after the first @", else the following command throws error:
$HereString=@"My
"life"
is
in
my hands"@

The following code works correctly
$when="Always"
$HereString=@"
My
"life"
is
in
my hands $when
"@

Below the output:
My
"life"
is
in
my hands Always






Saturday, 22 December 2018

Office 365 and PowerShell Mails

PowerShell and Office 365

Office 365 is a great product and there are nice ways to manage them with PowerShell itself.
Lets get started by connecting.

#Connect to Exchange
#Set-ExecutionPolicy RemoteSigned
#https://docs.microsoft.com/en-us/powershell/exchange/exchange-online/connect-to-exchange-online-powershell/connect-to-exchange-online-powershell?view=exchange-ps
$UserCredential = Get-Credential
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $UserCredential -Authentication Basic -AllowRedirection
Import-PSSession $Session -DisableNameChecking

Once, you connect, see the Mailbox
Get-Mailbox

I

Thursday, 13 December 2018

SharePoint CSOM to add data from CSV

PowerShell and Excel

PowerShell is very simple and powerful. Microsoft Office products like Word, Excel, PowerPoint has changed the world and become a part of every household.Advanced COM programming is possible, which I use for advanced operations on Word. Lets see how PowerShell is helpful with  single line code with Excel.

Command Available
Export-CSV
$data| Export-csv -Path $csvPath -Append

Import-CSV
$newFruits=Import-Csv -Path $csvPath

Let is say you have a Fruits data along with color information, we will display the fruits information in Excel as shown below:

$yourFolder="C:\demo\thangu"
$csvPath=$yourFolder+"\fruitsCsv.csv"
#New powerShell Table
$hashContent = [PSCustomObject]@{Name="Apple";Color="Red"}
$hashContent | Export-csv -Path $csvPath -Force
$hashContent = [PSCustomObject]@{Name="Mango";Color="Yellow"}
$hashContent | Export-csv -Path $csvPath -Append
$newFruits=Import-Csv -Path $csvPath



Sample Application to move data to external system(SharePoint)


SharePoint List Addition
Lets try to add some items from CSV to SharePoint List having fruits data.I used CSOM to read the data from CSV file created above and used the following CSOM code to move the data from CSV to Fruits list.

foreach($fruit in $newFruits){
$newItemInfo = New-Object Microsoft.SharePoint.Client.ListItemCreationInformation
$newItem=$psList.AddItem($newItemInfo)

$newItem["Title"] = $fruit.Name
$newItem["Color"] = $fruit.Color
$newItem.Update()
$Context.ExecuteQuery()
}


Tuesday, 4 December 2018

Resting in Peace with SharePoint Rest Apis

Introduction

Are you tired of writing many lines of code to do CRUD functionality? Lets take the simple case of trying to Read a SharePoint List and add items to a SharePoint list.You would have to write two functions using CSOM/JSOM/SSOM:
  1. ReadList() 
  2. AddListItem()
With the advent of REST in SharePoint, you can focus on your functionality and not on the code/language. All you have to do is to construct the URL based on your needs and reuse the REST CRUD code.
SharePoint team has done a lot of coding and made common functionality available for access from a URL.
You append the SharePoint site URL with REST Url.

Construction of REST Url

The REST Url begins with /_api/
After the api, you specify the Object. Example web, site. For nested objects /web/lists
Lets begin with a simple Url that reads the Site Url.
First, you get the Site Collection  Url and then append that with the Rest Url
Example:
https://abc.sharepoint.com/sites/dev/_api/site/Url
If you open the above Url, you would see a output like below:
<d:Url>https://abc.sharepoint.com</d:Url>
The results are mostly accessed by data.d or data.d.results for multiple items
If you are new to REST, here is a quick explanation on REST basics. REST technology allows developers to write functions using any technology of their choice and publish that as a URL which does the following tasks for a resource/object:
GET- Read the object and returns the results mostly in JSON/Xml format
POST- Get the data details and Add Items
PUT - Update the Object 
DELETE - Delete the Object

SharePoint and PowerShell code to do REST

Lets use PowerShell to get connected to SharePoint URL and do various Read operations. The cool thing is there is only one PowerShell code and the URL alone changes. In regular CSOM coding, you need to remember the Class and the methods to get your job done. in REST, the code remains the same and the URL alone changes.
You can download the code from here.
Here is a quick summary of the PowerShell code to do the CRUD operations.
  1. Get connected to SharePoint using SharePoint credentials
  2. Add the necessary headers to $headers based on CRUD operation
    1. GET- "Accept", "application/json;odata=verbose"
    2. POST- "X-RequestDigest", $formDigest from /_api/contextinfo
    3. PUT- "X-HTTP-Method", "PUT" or "MERGE"
    4. PUT- "X-HTTP-Method", "DELETE"
  3. Invoke-RestMethod -Uri $fullUrl -Headers $headers -Method Post -WebSession $webSession -ContentType "application/json;odata=verbose"
Once, you run the above code, you can start using Read-SPObject,Create-SPObject,Update-SPObject on various SharePoint objects like Site, Lists,Files and many more based on your needs.

Add List Items using REST

Lets start with our original need to read a SharePoint List and add Items to a SharePoint list say PSRestList. 
$targetSite="https://xxx.sharepoint.com/sites/dev"
$User="admin@xxx.onmicrosoft.com"
$restUrl="/_api/web/lists/getbytitle('PSRestList')"
$listsPSRest=Read-SPObject -targetSite $targetSite -User $User -restUrl
Below are the details needed to add a item to a SharePoint list. Note that the data part contains the List Name, List Columns like Title.
#Add List Item
$listName="PSRestList"
$data=@"
{ '__metadata': { 'type': 'SP.Data.$($listName)ListItem' }, 'Title': 'OneTwoThree','CurrencyThangu':'2500' }
"@
$restUrl="/_api/web/lists/GetByTitle('"+$listName+"')/items"
$listItemNew=Create-SPObject -targetSite $targetSite -User $User -restUrl $restUrl -data $data

Summary

As a quick summary, you learnt the following:
  1. Basics of REST and its benefits
  2. Construction of SharePoint REST Url
  3. PowerShell code that Invokes the SharePoint URL
  4. Adding a SharePoint List Item using the REST Url.
If you like to learn by watching videos, here is a sample REST demo.