PowerShell
0 - Prerequisites
Autorization info, view page how to Obtain personal token
1 - PowerShell in a nutshell
PowerShell is a command-line shell and scripting language developed by Microsoft. It is designed for automating system administration tasks and configuration management. PowerShell combines the power of Unix command-line with the flexibility of a modern scripting language.
Key features of PowerShell:
- Cmdlets: PowerShell uses special commands called cmdlets, which are small scripts designed to perform specific tasks.
- Pipeline: Allows passing the output of one cmdlet as input to another cmdlet, facilitating data manipulation.
- Access to .NET: PowerShell is built on the .NET framework, which means it can access all .NET classes and methods.
- Automation: It is widely used to automate repetitive tasks, such as user and group management, server configuration, and software deployment.
- Remote management: Supports remote system management, allowing administrators to execute commands on remote computers.
What is PowerShell used for:
- System administration: Managing users, groups, permissions, processes, and services.
- Configuration management: Configuring and managing servers and applications.
- Task automation: Creating scripts to automate repetitive and complex tasks.
- Cloud environment management: Interacting with cloud services like Azure for resource management.
- Developing custom scripts: Creating scripts for specific organizational needs.
Where can I install and use PowerShell?
PowerShell is available on multiple platforms and can be installed and used in various environments:
- Windows: PowerShell is pre-installed on Windows 10 and Windows Server 2016 and later versions. It can be launched by searching for "PowerShell" in the Start menu.
- macOS: PowerShell can be installed on macOS using Homebrew. Run the command
brew install --cask powershellin the terminal. - Linux: PowerShell is available for various Linux distributions, including Ubuntu, CentOS, and Red Hat. It can be installed by following the specific instructions for the distribution on the official PowerShell site.
- Azure Cloud Shell: PowerShell is also available in Azure Cloud Shell, a browser-based shell environment that can be used directly from the Azure portal.
- Docker: PowerShell can be run in a Docker container. You can use an official PowerShell Docker image to create and manage containers with PowerShell pre-installed.
Regardless of the platform, PowerShell offers a consistent and powerful experience for system automation and management.
PowerShell is a powerful and versatile tool that can greatly simplify system management and automation.
2 - How to use PowerShell to use our web services
Create a configuration file
Create a configuration file and insert the following content:
{
"username": "admin_xxxxx",
"grant_type": "token",
"scope": "csdemo_xxxxxxxx",
"token": "xxxxxxxxxxJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJ3ZWJhcGlhZG1pbiIsIndlYmFwaTphdXRoZW50aWNhdGlvbjpzY29wZSI6Imh1Yl9hZG1pbixjc2RlbW9fYWx5Y3NkZW1vIiwianRpIjoiNTRlOTJlY2MtN2I1OS00YWVjLThiOTMtYWVjYmQwNTk1ZmUzIiwiaWF0IjoxNjk0MDkwMzc5LCJpc3MiOiJBbHlDRVNydjJTcnZJc3N1ZXIiLCJhdWQiOiJBbHlDRVNydjJTcnZBdWRpZW5jZSJ9.QyQdBWnULnM9TF1eCuY7x8JIXxPuOUiyg1_YnxbjaCE",
"filePath": "",
"webapi_base_url": "https://xxxxxxxxxx.teamsystem.io",
"company": "1"
}
This allows us to insert our access data.
Create a .ps1 file
Next, create our PowerShell file with a .ps1 extension
# Read the configuration file
$config = Get-Content -Path "config.json" | ConvertFrom-Json
# Verify that the configuration parameters are set
if (-not $config.webapi_base_url) {
Write-Output "Error: 'webapi_base_url' is not set in the configuration file."
exit
}
if (-not $config.username) {
Write-Output "Error: 'username' is not set in the configuration file."
exit
}
if (-not $config.token) {
Write-Output "Error: 'token' is not set in the configuration file."
exit
}
if (-not $config.scope) {
Write-Output "Error: 'scope' is not set in the configuration file."
exit
}
if (-not $config.grant_type) {
Write-Output "Error: 'grant_type' is not set in the configuration file."
exit
}
if (-not $config.company) {
Write-Output "Error: 'company' is not set in the configuration file."
exit
}
if (-not $config.file_path) {
Write-Output "Alert: 'file_path' is not set in the configuration file. The file will be saved in the current directory."
}
# Construct the token URL
$tokenUrl = $config.webapi_base_url + "/api/v1/auth/token"
# Obtain the OAuth2 access token
$tokenResponse = Invoke-RestMethod -Method Post -Uri $tokenUrl -Body @{
username = $config.username
token = "Bearer " + $config.token
scope = $config.scope
grant_type = $config.grant_type
} -Headers @{
"Content-Type" = "application/x-www-form-urlencoded"
"Access-Control-Allow-Origin" = "http://localhost:3000"
}
$accessToken = $tokenResponse.access_token
Write-Output "Access token received"
# Construct the web URL for the print document
$docnumber = "202500000710"
$filename = "document.pdf" # Default filename
$outputFilePath = $filename
if ($config.file_path) {
$outputFilePath = Join-Path -Path $config.file_path -ChildPath $filename
}
$webUrl = $config.webapi_base_url + "/api/v1/"+$config.scope+"/MG/Documento/"+ $docnumber +"/print?Company=" + $config.company
# Send the file via a RESTful call
Write-Output "Sending request to $webUrl"
$response = Invoke-WebRequest -Method Get -Uri $webUrl -Headers @{
"Authorization-Scope" = $config.scope
"Content-Type" = "application/pdf"
"Authorization" = "Bearer $accessToken"
}
Write-Output "Response received!"
# Check if the response contains the streamPDF field
if ($response.Content -match '"streamPDF":\s*"([^"]+)"') {
$base64String = $matches[1]
Write-Output "Base64 content found!"
# Decode the base64 content and save as a PDF file
$decodedBytes = [System.Convert]::FromBase64String($base64String)
[System.IO.File]::WriteAllBytes($outputFilePath, $decodedBytes)
Write-Output "Response saved to file: $outputFilePath"
} else {
Write-Output "Error: The streamPDF field is not present in the response."
}
In this example, we can authenticate via token and download a PDF print of a document.