XPath code Generator – Web Tools

Background

Wanted to quickly review freely available online tools for generating Xpath from XML documents.

Sample XML Documents

Will use these XML documents as samples :-

  1. Microsoft
    • Docs. / NET / C# Guide / Programming guide / Programming concepts / LINQ
      • Sample XML File: Customers and Orders in a Namespace
        Link

 

Tools

  1. Xmltoolbox
    • XPath Generator
  2. Qutoric
  3. XPather.com

 

In depth

Xmltoolbox

XPath Generator

Author

Xmltoolbox is authored by Ole Bille.

Usage

Outline
  1. Launch web browser
  2. And, go here ( http://xmltoolbox.appspot.com/xpath_generator.html )
  3. Paste the XML text into the canvas
  4. In the loaded canvas, click on the elements you want evaluated
  5. Please review and capture the generated Xpath results

 

Screenshots

xpathgenerator.20181217.0344AM

qutoric

PathEnq

Usage

Outline
  1. Launch web browser
  2. And, go here ( http://www.qutoric.com/xslt/analyser/xpathtool.html )
  3. Invoke the Choose File button at the bottom of the screen
  4. Select the file to be evaluated
  5. In the loaded canvas, click on the elements you want evaluated

 

Screen Shots

xmlSource.20181217.0319AM

xmlSource.20181217.0322AM

 

XPather.com

Author’s Guideline

  1. This web app enables you to query XML/HTML documents with your browser in real time. It can generate queries for you too!
    • You can enter your xpath query in the top-left panel and it will be instantly executed against this document.
    • To generate an xpath query for a specific element, please hold CTRL and hover over it. An xpath is generated.

Usage

To generate an XPath, please paste the XML into the canvas.

Once the XML is available and accessible, please move your cursor to the screen area, hold down the Ctrl button while hovering or selecting the XML tag or body.

Samples

Sample #1

xpather.com.20181217.0308AM

Deficiency

  1. Supports Element, but not attribute

 

 

 

 

 

.Net Frameworks – Installed Products

Background

Wanted to get a list of Microsoft .Net Frameworks installed on a machine.

Windows Management Interface ( WMI )

Introduction

In this post, we will use Windows Management Interface, WMI.

 

Powershell

Outline

  1. Get-WmiObject
    • Class :- Win32_Product
    • Filter :- Microsoft .Net Framework
    • Columns :-
      • Name
      • Version

Code


[char] $CHAR_WILDCARD="*"

$computer=$env:computername

$filter="Microsoft .Net Framework"

$filterWildcard= $filter + $CHAR_WILDCARD

Get-WmiObject -Class Win32_Product -Computer $computer | `

	Select @{"Name"="Name"; "Expression"={($_.Name.trim())}}, Version | `

	Where-Object -FilterScript {$_.Name -like $filterWildcard} | `

	Sort-Object Name, Version -Descending

Images

getVersionNumber_self_20181207_1209PM

Powershell

Outline

  1. Get-WmiObject
    • Class :- Win32_Product
    • Filter :- Microsoft .Net Framework
    • Columns :-
      • Name
      • Version

Code


set "_wildcard=%%"

wmic product where "Name like 'Microsoft .Net %_wildcard%' " get Name, Version

Images

MSWindowsBatch.20181207.1229PM

Win OS RecycleBin – Access via Powershell

Background

Over the Thanksgiving weekend spent time tackling a couple of machines that were running low on storage.

There are so many ways that one can lose space in MS Windows.

And, so we look between the white spaces.

in the past

  1. Recycle Bin Emptying Using Powershell
    Link

Recycle Bin

For those who want to programmatically look and repair, here is a bit of code to look in the Recycle Bin.

Code


Set-StrictMode -Version 2

$userRecycleBin_ID = 0x0a

Function Get-RecycleBin
{

	# Instanciate Shell Application
  	$objShell = New-Object -ComObject Shell.Application;

	#Get Recycle Bin
	$objShellRecycleBin = $objShell.NameSpace($userRecycleBin_ID)

	#Get Recycle Bin Items
	$objItems = $objShellRecycleBin.Items();

	#Get Recycle Bin Items
	#Only interested in explicitly listed attributes
	#$objItems | Select-Object * | Out-GridView -Wait
	$objItems | Select-Object Path, Name, IsFolder, Type, Size | Out-GridView -Wait

	return ( $objItems );

}

Function Stat-RecycleBin
{
	$objShell = New-Object -ComObject Shell.Application;

	$objItems = $objShell.NameSpace($userRecycleBin_ID).Items();

	#| measure
	$lNumberofEntries = $objItems.Count

	return ( $lNumberofEntries);

}

$objItems = Get-RecycleBin

$lNumberofEntries = $objItems.Count

$log = "Number of entries in reccyle bin {0}" -f $lNumberofEntries

Write-Host $log

Invoke


powershell  -noprofile -executionpolicy bypass -file ./recycleBinBrowse.ps1

Dedicated

As always most of this is somebody else.

He wrote it in two lines.

There were things I was missing and so I had to break it into single statements.

If you find find yourself having to do so same, don’t feel bad nor lonesome for the handicap.

 

Referenced Work

  1. Windows Dev Center
    • Docs > WindowsDesktop > The Windows Shell > Shell Reference > Shell Objects for Scripting and Microsoft Visual Basic > Shell > NameSpace
      • Shell.NameSpace method
        Link
    • Docs / Windows / Desktop / API / The Windows Shell / Shldisp.h / ShellSpecialFolderConstants enumeration
      • ShellSpecialFolderConstants Enumeration
        Link

Recycle Bin Emptying Using Powershell

Background

One might find that the Recycle Bin is taking up a good chuck of storage.

Remediation

Powershell

Let us use Powershell to dump our recycle bins.

Cmdlet :- Clear-RecycleBin

Syntax

Here is the Syntax

Clear-RecycleBin-Syntax

Sample

All Drives
Script
Clear-RecycleBin  -Force
Output

force.20181124.1008AM.PNG

Drive Letters Specified
Script
Clear-RecycleBin  -DriveLetter "C", "D","E"
Output

foldersSpecified.20181124.0950AM.PNG

Confirm
Confirm – True
Script
Clear-RecycleBin  -Confirm:$true
Output

confirmTrue.20181124.0955AM

Explanation
  1. Prompted
Confirm – False
Script
Clear-RecycleBin  -Confirm:$false
Output

confirmFalse.20181124.0957AM.PNG

Explanation
  1. Not Prompted

Errors

Error :- Clear-RecycleBin : The system cannot find the file specified
Sample

Clear-RecycleBin -Confirm:$false -DriveLetter "C", "D","E"

Output – Image

error.cannotFindtheFileSpecified.20181124.1030AM

Output – Textual

Clear-RecycleBin : The system cannot find the file specified
At line:1 char:1
+ Clear-RecycleBin -Confirm:$false -DriveLetter "C", "D","E"
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (RecycleBin:String) [Clear-RecycleBin], Win32Exception
+ FullyQualifiedErrorId : FailedToClearRecycleBin,Microsoft.PowerShell.Commands.ClearRecycleBinCommand

Overlook

Try to use “-ErrorAction SilentlyContinue


Clear-RecycleBin -Confirm:$false -DriveLetter "C", "D","E"  -ErrorAction SilentlyContinue

Dedicated

Dedicated to Gee Law

Gee Law

Blog Posts & Git Hub Issues

  1. GeeLaw.blog
    • PowerShell codebase misuses SHEmptyRecycleBin function in Clear-RecycleBin cmdlet
      Link
  2. GitHub
    • PowerShell
      • Misuse of `SHEmptyRecycleBin` in `Clear-RecycleBin` cmdlet #6743Link

Sidenotes

Link
Sidenote
 I wish Raymond Chen could write a blog entry on SHEmptyRecycleBin if there’s something interesting to add. He’s an expert of Windows shell programming. However, his suggestion box is currently closed and there’s no prescribed way to make a suggestion (though I could have sent him an email, but that would be too impolite), so I can only make wishes.

PowerShell – Get Computer name

Background

Wanted to highlight a couple of APIs for retrieving the computer name in Powershell.

Platform

Linux/Cent OS.

 

Outline

  1. Environment Variable
    • Get-ChildItem Env:HOSTNAME
  2. system.environment
    • machineName
  3. system.net.Dns
    • GetHostName
  4. Get-WmiObject ( only works on MS Windows )
    • Win32_ComputerSystem
      • Name

 

Environment Variables

Get-ChildItem Env:

Syntax


pwsh -Command "Get-Childitem Env:"

Output

Get-ChildItem.Env.20181112.0729PM

 

Get-ChildItem Env:HOSTNAME

Syntax


pwsh -Command "Write-Host( (Get-ChildItem Env:HOSTNAME).Value )"

Output

Get-ChildItem.Env.HostName.20181112.0723PM

system.environment

machineName

Syntax


pwsh -Command "Write-Host([system.environment]::machineName)"

Output

system.environment.machineName.20181112.0708PM

system.net.Dns

GetHostName

Syntax


pwsh -Command "Write-Host([system.net.Dns]::GetHostName())

Output

system.net.Dns.GetHostName.20181112.0706PM

 

Get-WmiObject.Win32_ComputerSystem.Name.20181113.0159AM.PNG

Win32_ComputerSystem

Syntax


powershell -Command "Write-Host((Get-WmiObject Win32_ComputerSystem).Name)"

Output

OS – MS Windows

Get-WmiObject.Win32_ComputerSystem.Name.20181113.0159AM.PNG

OS – Linux

Image

Get-WmiObject.Win32_ComputerSystem.Name.Linux.20181113.0203AM

Textual


>pwsh -Command "Write-Host( Write-Host((Get-WmiObject Win32_ComputerSystem).Name))"
Get-WmiObject : The term 'Get-WmiObject' is not recognized as the name of a cmdlet, function, script file, or operable program.
Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:25
+ Write-Host( Write-Host((Get-WmiObject Win32_ComputerSystem).Name))
+ ~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (Get-WmiObject:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException

PowerShell Install On Linux / CentOS

Background

Still preparing our CentOS computer.

Our current step is to Install Microsoft’s PowerShell.

Guide

Our guide is :-

Installing PowerShell Core on Linux
Link

Outline

  1. Install
    • yum
      • Install
        • Install Powershell
      • Registered Repositories – Review
        • yum repolist
        • ls /etc/yum.repos.d
      • Registered Repositories – Update
        • Curl/tee
      • Install
        • Install Powershell
  2. Run Sample Powershell Scripts

Install

Install Powershell

yum install powershell

Syntax


yum install powershell

Output

Output – Image

yum.install.powershell.20181112.1030AM.PNG

Output – Text

>sudo yum install powershell
[sudo] password for dadeniji:
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
* base: mirror.hostduplex.com
* extras: mirror.keystealth.org
* updates: mirror.keystealth.org
No package powershell available.
Error: Nothing to do
>

Explanation

Microsoft’s repository not registered on machine.

Registered Repositories – Review

Let us review the yum repositories that we have registered.

yum repolist

Syntax


yum repolist

Output

Output – Image

yum.repolist.20181112.1028AM.PNG

Output – Textual

>yum repolist
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
* base: mirrors.sonic.net
* extras: mirrors.usc.edu
* updates: sjc.edge.kernel.org
repo id repo name status
base/7/x86_64 CentOS-7 - Base 9,911
extras/7/x86_64 CentOS-7 - Extras 434
updates/7/x86_64 CentOS-7 - Updates 1,614
repolist: 11,959

 

/etc/yum.repos.d :- List Files

Syntax


ls -la /etc/yum.repos.d

Output

Output – Image

ls.etc.yum.repos.d.20181112.1033AM.PNG

Explanation

  1. The only repositories registered are CentOS

 

Registered Repositories – Update

Create File /etc/yum.repos.d/Microsoft.repo

Let us add Microsoft’s Redhat repository to the list of registered repositories.

Syntax


curl https://packages.microsoft.com/config/rhel/7/prod.repo | sudo tee /etc/yum.repos.d/microsoft.repo

Output

Output – Image

curl.tee.20181112.1035AM.PNG

Output – Textual

>curl https://packages.microsoft.com/config/rhel/7/prod.repo | sudo tee /etc/yum .repos.d/microsoft.repo
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 193 100 193 0 0 504 0 --:--:-- --:--:-- --:--:-- 505
[packages-microsoft-com-prod]
name=packages-microsoft-com-prod
baseurl=https://packages.microsoft.com/rhel/7/prod/
enabled=1
gpgcheck=1
gpgkey=https://packages.microsoft.com/keys/microsoft.asc
>

 

yum install

Using yum, let us install powershell.

yum install powershell

Syntax


sudo yum install -y powershell

Output

Output – Image
Output – Image – #1

yum.install.powershell.20181112.1037AM.PNG

Output – Image – #2

yum.install.powershell.20181112.1039AM.PNG

Output – Image – #3

yum.install.powershell.20181112.1041AM.PNG

Output – Image – #4

yum.install.powershell.20181112.1043AM.PNG

Output – Image – #5

yum.install.powershell.20181112.1045AM.PNG

 

 

Sample

Run Sample Scripts

Sample #1

Get PowerShell Version Number

Syntax


pwsh -Command "Write-Host((Get-Host).version) "

Output

Output – Image

powerShell.version.20181112.1218PM

Sample #2

Get Username for current user.

Syntax


pwsh -Command "Write-Host( 'Hello ' +  [Environment]::Username) "

Output

Output – Image

helloWorld.20181112.1137AM

 

Summary

Couple of quick points :-

  1. Installation Errors
    • Error :- Repository not available
      • Message
        • No package powershell available.
          Error: Nothing to do
      • Remediation
        • Update Repository – Add Microsoft
  2. In MS Windows, the Powershell executable is powershell.
    In Linux, is is pwsh.

 

 

Echo Data to Powershell

Background

Trying to automate a little app.

As Powershell is a modern development tool and I do not need to compile the source code wanted to try it out first.

Exercise

Our need is very narrow.

The app is requesting that we enter our full name.

Really did not want to engage with the app so intimately and will rather feed its input through another channel.

Code

Powershell


$fullname = Read-Host -Prompt 'Please enter first and last name'

$line = "The name you entered is {0}" -f $fullname

Write-Host $line

CommandShell


@echo off

setlocal

    set "_fullname=Daniel Adeniji"

    set "_app=acceptKeyStroke.ps1"

    echo %_fullname% | powershell -f %_app%

endlocal

Output

echoToPowershell_20181025_0810AM.PNG