The API is not the database or even the server, it is the code that governs the access point(s) for the server. APIs allow developers to interact with online services without actually having to point and click their way through a UI.
Where we can have this scenario?
You can use REST APIs to manage GitHub, Azure, work with Microsoft 365, or use cognitive services in Azure. Still, we have a lot of other use cases. In this post, we will explain with a very simple guide on how you can make REST API calls from PowerShell.
How to interact with APIs through Powershell?
The two most common PowerShell methods of interacting with REST API’s are to use either Invoke-RestMethod or Invoke-WebRequest. To interact with a REST API the best option is to use Invoke-RestMethod. Invoke-RestMethod turns input JSON or XML into native PowerShell objects to make further interaction easy. In this post, we will continue our travel with Invoke-RestMethod to access the API.
Example
Let’s start with a simple example. We’ll need an existing REST API to work. Browsing around on the Internet, I come across a Dummy REST API called dummy.restapiexample.com/api/v1/. Every REST API has a base URI and an endpoint. Our API has a base URI of http://dummy.restapiexample.com/ and has an endpoint of /API/v1/employees, making the endpoint URI http://dummy.restapiexample.com/api/v1/employees.
Once the above code is executed the GetAPI_EMPDetails will have the object of employee details. Post the, we can start our future manipulation based on the data which we GET from the API.
In our requirement, the client needs to Export all custom user properties from the old site to the new site as they planned to migrate from the new environment. In this case, writing C# or other language code to retrieve the User information is time taking process so we decided to make this with help of Powershell. So in this article, we are going to explain with the simple Powershell Script.
This example shows how to get all active/Inactive users and theirs custom properties based on domain, post fetching the data we planned to save them in the CSV file which will be easy to share. It is not necessary to export some custom properties then should be removed from properties.
Note: Sitecore user profile custom properties are case sensitive
STEP #1: Get user details using Get-User cmdlet with condition parameter
From the below code, we are fetching the user information based on the “addotnetAdmin/” domain. Once it’s executed, the user variable will have all the users who match the domain “addotnetAdmin/”.
As a System Admin, I came up with a requirement of creating an XML file for a currently running process from Multiple systems as output using PowerShell code. Post this, the same XML file will be consumed by an external trend reporting process.
The ConvertTo-Xml cmdlet creates an XML-based representation of one or more .NET objects. To use this cmdlet, pipe one or more objects to the cmdlet, or use the InputObject parameter to specify the object. We also use Export-Clixml for the same scenario, we will review this in the upcoming article.
In this article, I am going to explain with simple XML creation method using XmlWrite and here we want to get current running service information using Get-Service from each computer and save the results to an XML file.
STEP #1: Get currently Running service using the Get-Service cmdlet
STEP #2: Convert the output in the ConvertTo-XML with As parameter
Technically ConvertTo-XML is working but in its current usage, it is returning an XML document object and only showing the top two properties.
ConvertTo-XML -as String, this command converts the process objects that represent all of the services on the computer into an XML document and finally convert as String as output
Recently, I came up with a requirement of creating an XML file as output using PowerShell code. I want to get some service information from each computer and save the results to an XML file. Post this, the XML file will be consumed by an external trend reporting process. In this article, I am going to explain with simple XML creation method using XmlWrite with some static inputs.
STEP #1: Create & Set The Formatting with XmlWriterSettings class
First, we need to get an XmlTextWriter object to create the XML and assign the Indent values as true to make the elements to be arranged in a new line. The XmlWriterSettings class provides properties that control data conformance and output format.
$xmlObjectsettings = New-Object System.Xml.XmlWriterSettings #Indent: Gets or sets a value indicating whether to indent elements. $xmlObjectsettings.Indent = $true $xmlObjectsettings.IndentChars = ” “
STEP #2: Set the XML File Path and Create The Document
# Set the File path & Create The Document $XmlFilePath = “C:\dotnet-helpers\MyXmlFile.xml” $XmlObjectWriter = [System.XML.XmlWriter]::Create($XmlFilePath, $xmlObjectsettings)
STEP #3 : Write the XML Declaration
When WriteStartDocument is called the writer validates that what you are writing is a well-formed XML document.For example, it checks that the XML declaration is the first node, that one and only one root-level element exists, and so on. If this method is not called, the writer assumes an XML fragment is being written and applies no root level rules.
#Write the XML delcaration. $XmlObjectWriter.WriteStartDocument()
STEP #4: Start the Root Element and build with child nodes
In this step we are going to form the HTML elments using the WriteStartElement , WriteEndElement() ,WriteElementString , WriteComment…
XmlObjectWriter.WriteComment(“writes out a start tag with the specified local name.”) $XmlObjectWriter.WriteStartElement(“BaseSettings“) # <– BaseSettings
$XmlObjectWriter.WriteEndElement() # <– End ChildConfigSettings $XmlObjectWriter.WriteEndElement() # <– End ConfigSettings $XmlObjectWriter.WriteEndElement() # <– End BaseSettings
############################################################################
#Project: Creating a Simple XML Document using XmlWriter()
#Developer: Thiyagu S (dotnet-helpers.com)
#Tools : PowerShell 5.1.15063.1155 [irp]
#E-Mail: mail2thiyaguji@gmail.com
###########################################################################
# Create & Set The Formatting with XmlWriterSettings class
$xmlObjectsettings = New-Object System.Xml.XmlWriterSettings
#Indent: Gets or sets a value indicating whether to indent elements.
$xmlObjectsettings.Indent = $true
#Gets or sets the character string to use when indenting. This setting is used when the Indent property is set to true.
$xmlObjectsettings.IndentChars = " "
# Set the File path & Create The Document
$XmlFilePath = "C:\dotnet-helpers\MyXmlFile.xml"
$XmlObjectWriter = [System.XML.XmlWriter]::Create($XmlFilePath, $xmlObjectsettings)
# Write the XML declaration and set the XSL
$XmlObjectWriter.WriteStartDocument()
# Start the Root Element and build with child nodes
$XmlObjectWriter.WriteComment("writes out a start tag with the specified local name.")
$XmlObjectWriter.WriteStartElement("BaseSettings") # <-- BaseSettings
$XmlObjectWriter.WriteStartElement("ConfigSettings") # <-- Start ConfigSettings
$XmlObjectWriter.WriteElementString("India","200$")
$XmlObjectWriter.WriteElementString("UAE","150$")
$XmlObjectWriter.WriteStartElement("ChildConfigSettings") # <-- Start ChildConfigSettings
$XmlObjectWriter.WriteElementString("UK","250$")
$XmlObjectWriter.WriteEndElement() # <-- End ChildConfigSettings
$XmlObjectWriter.WriteEndElement() # <-- End ConfigSettings
$XmlObjectWriter.WriteEndElement() # <-- End BaseSettings
# Finally close the XML Document
$XmlObjectWriter.WriteEndDocument()
$XmlObjectWriter.Flush()
$XmlObjectWriter.Close()
Output XML File :
There are additional ways to create custom XML. and there are plenty of methods that we can experiment. We will discuss in the upcoming posts
Mostly When REST APIs return data in JSON format, you can get at it through PowerShell.So there you have it, working with JSON data can be easy once you turn it into PowerShell objects. Hopefully, this helps you work with APIs and process data on yours.
Recently I was processing some JSON files for one of my projects and wanted to merge multiple similar JSON data into a single JSON. All the data I was processing were in a similar JSON format, however, coming from different sources. This post will discuss simple JSON data and how we can combine it with single JSON data.
Declare/Get the JSON data and assigned it to the variable. To make a simple example, I had created two variables to contain the EMP details based on Countries and want to combine those data to have consolidated Employee details.
Declaring the Empty array for Merging the two JSON objects (ie., $Ind_EMPDetails & $UK_EMPDetails)
$totalEMPDetails = @()
Step #3
Merge the two JSON objects using the ConvertFrom-Json cmdlet. As we are aware, Powershell can’t merge the Json object directly and we need to perform by converting it into the custom PSCustomObject object. We need to use ConvertFrom-Json cmdlet for Combining the JSON objects, ConvertFrom-Json cmdlet converts a JavaScript Object Notation (JSON) formatted string to a custom PSCustomObject object that has a property for each field in the JSON string.
We already discussed the reading/Writing XML file using PowerShell in previous posts. Now in this post, we will discuss how to add the value to the Array or Object from the XML using Powershell.
PowerShell has XPath but you don’t have necessarily have to use it. Instead of XPath PowerShell provides the simplest/Easiest way to read XML files, manipulate the XML document, we use the same in the example below.
The simplest way to read an XML document in PowerShell is to typecast a variable to the type [XML]. To create this variable, you can use the Get-Content cmdlet to read all of the text in an XML document. To typecast the output of Get-Content we can simply prepend the text [xml] before the variable. This tells PowerShell that we need this variable typecasted as a System.Xml.XmlDocument type instead of the default array type that normally comes from Get-Content.
$XML1path = “C:\dotnet-helper\EMPdetails.xml”
Once you’ve executed the above cmdlet, the Get-Content cmdlet will read all the raw text from the XML document and cast the output to type System.Xml.XmlDocument, you now have a variable called $XmlDocument that contains the entire XML node tree that represents that document.
STEP 2:
Now you can declare the empty array for getting XML value.
[String[]]$str_EMP_Array = @()
STEP 3:
After the execution of STEP 1, the $XmlDocument variable will have the entire XML node tree. Now you can loop the XML elements from the parent Node. From the below code, you can loop all the child elements by pointing to the parent node.
foreach ($emp_Detail in $XmlDocument.EMPLOYEE_DETAILS.EMP_NAME) { [String[]]$str_EMP_Array += $emp_Detail }
STEP 4:
Finally, print the Array data.
$str_EMP_Array
Final Code:
############################################################################
#Project : How to add values to the string Array from xml using Powershell
#Developer : Thiyagu S (dotnet-helpers.com)
#Tools : PowerShell 5.1.15063.1155 [irp]
#E-Mail: mail2thiyaguji@gmail.com
###########################################################################
$XML1path = "C:\dotnet-helper\EMPdetails.xml"
[String[]]$str_EMP_Array = @()
[xml]$XmlDocument = get-content $XML1path
foreach ($emp_Detail in $XmlDocument.EMPLOYEE_DETAILS.EMP_NAME)
{
[String[]]$str_EMP_Array += $emp_Detail
}
$str_EMP_Array
Question: Hey dotnet-helpers, I wanted to see the values of all the Environment variables in my servers/PC, How do I do this in PowerShell?
Dotnet-helpers Reply :
Windows environment variables provide information about the Windows operating system. Separated into the system and user-level scopes, default environment variables can be read and modified, and new environment variables can be also added.
PowerShell can access and manage environment variables in any of the supported operating system platforms. The PowerShell environment provider simplifies this process by making it easy to view and change the environment variables.
We access environment variables using the built-in variable called $env followed by a colon and the name of the environment variable. The $env variable can be used to access all user context environment variables.
Display all the Environmental Variables
Example 1: Get Environment Variables using dir/gci/cd
Syntax : dir env: OR gci env: OR ls env: OR cd env:
You can get the complete list of environment variables using the below cmdlets which will list out all the environment variables as shown below.
Example 2: Get Environment Variables using Get-Childitem
You can get the complete list of environment variables using the Get-Childitem cmdlet. The following command list all the local environment variables.
Get-Childitem -Path Env:* | Sort-Object Name
Dispay the specific Environment Variable
Syntax : $Env:<variable-name>
In the above syntax, the dollar sign ($) indicates a variable, and the drive name (Env:) indicates an environment variable followed by the variable name (windir).
Example 1: Get Specific Environment variable using $ENV
This script will find temporary files.
$ENV:TEMP
Example 2: Get Specific Environment variable using Get-Childitem
Instead of listing all the environment variables, you can also get the value of a specific environment variable by passing the specified variable name.
In the previous post, we have discussed how to zip the folder/files, so here we going to discuss how to unzip files and folders archives. The process is even easier than compressing them; all you need is the source file and a destination for the data ready to unzip.
In the above command, replacing <PathToZipFile> and <PathToDestination> with the path to the files you want to Un-compress and the name and folder you want it to go to, respectively
The destination folder specified to extract the files into will populate with the contents of the archive. If the folder didn’t exist before unzipping, PowerShell will create the folder and place the contents into it before unzipping.
If the destination folder already exists in the destination, PowerShell will return an error when it tries to unzip the files. However, you can use -Force PowerShell to overwrite the data with the new ones using the -Force parameter.
As part of System Admin, you will have control of more servers and there may be hundreds of empty folders as junk files which may take up your hard disk. While the junk files occupy disk and it became more Junk in the servers so it became critical to maintaining the important files.
The empty folders don’t take up disk space, but to organize your data better, you may want to trim them every once in a while. If you feel to manually delete empty folders then it will need to routine and time consuming manual work. So we below PowerShell script will help you t to query and delete all empty folders and subfolders.
The following PowerShell command-line deletes empty folders, located under the specified base folder recursively.
STEP #1: Get the recursive child items
First, we need to get the child items from the source path ie., C:\dotnet-helpers\TEMP Folder
//gci alias of Get-ChildItem gci “C:\dotnet-helpers\TEMP Folder” -r
STEP #2: Fetch all the empty folders
To filter the folders/directories available in the current context, the following property can use $_.psiscontainer. It will return a boolean, indicating whether the current object is a directory or not.
PSIsContainer retrieves an array of strongly typed FileSystemInfo objects representing files and subdirectories of the current directory. The count is not 0, it doesn’t exist at all meaning that the directory is empty or holds other empty folders
The revisiting of Admin access is an important activity for the support team as most of the time we may provide temporary access for a certain time and forgot to roll back.
In this post, we will explore a great way of extracting Admin user data . This is the simplest way to extract using the Sitecore Powershell ISE.
Sample 1: Get All User mapped with specific domain
##################################################
#Project: Get a list of all users for Domain
#Developer: Thiyagu S (dotnet-helpers.com)
#Tools : PowerShell 5.1.15063.1155 & SiteCore 8.1
#E-Mail: mail2thiyaguji@gmail.com
##################################################
#Getting User list by filtering with domain "dotnet-helpers"
$allSitecoreUsers = Get-User -Filter “dotnet-helpers\*”
$allSitecoreUsers | ForEach-Object {
Write-Host $_.Name
}
Sample 2: Get All Admin Users mapped with specific domain
#####################################################################################################
#Project: How to get Sitecore Admin users for the domain using Sitecore PowerShell Extensions.
#Developer: Thiyagu S (dotnet-helpers.com)
#Tools : PowerShell 5.1.15063.1155 & SiteCore 8.1
#E-Mail: mail2thiyaguji@gmail.com
#####################################################################################################
$adminSitecoreUsers = Get-User -Filter “dotnet-helpers\*”
$adminSitecoreUsers | Where{$_.IsAdministrator -eq $true } | ForEach-Object {
Write-Host $_.Name
}
To provide the best experiences, we use technologies like cookies to store and/or access device information. Consenting to these technologies will allow us to process data such as browsing behavior or unique IDs on this site. Not consenting or withdrawing consent, may adversely affect certain features and functions.