I have the following code:
$xmlFile = 'C:\Users\kraer\Desktop\bom.xml'
[xml]$xml = Get-Content $xmlFile
$xml.bom.components.component | ForEach-Object {
$finalObject = [PSCustomObject]#{
'Name' = $_.name
'Version' = $_.version
'License' = $_.licenses.license.id
}
Write-Output $finalObject
}
Now I would like to convert my $finalObject to a MarkDown Table. Are there any possibilities here?
For another question I received this answer but right now it doesn't work for my code.
function ConvertTo-MarkDownTable {
[CmdletBinding()] param(
[Parameter(Position = 0, ValueFromPipeLine = $True)] $InputObject
)
Begin { $Init = $True }
Process {
if ( $Init ) {
$Init = $False
$_.PSObject.Properties.Name -Join '|'
$_.PSObject.Properties.ForEach({ '-' }) -Join '|'
}
$_.PSObject.Properties.Value -Join '|'
}
}
Do you have another solution?
Thanks for your help
Not knowing the contents of your bom.xml, you might try this slightly adapted version of the function:
function ConvertTo-MarkDownTable {
[CmdletBinding()] param(
[Parameter(Mandatory = $true, ValueFromPipeline = $true, Position = 0)]
$InputObject
)
Begin {
$headersDone = $false
$pattern = '(?<!\\)\|' # escape every '|' unless already escaped
}
Process {
if (!$headersDone) {
$headersDone = $true
# output the header line and below that a dashed line
# -replace '(?<!\\)\|', '\|' escapes every '|' unless already escaped
'|{0}|' -f (($_.PSObject.Properties.Name -replace $pattern, '\|') -join '|')
'|{0}|' -f (($_.PSObject.Properties.Name -replace '.', '-') -join '|')
}
'|{0}|' -f (($_.PsObject.Properties.Value -replace $pattern, '\|') -join '|')
}
}
Usage:
# load the xml from file
$xml= New-Object System.XML.XMLDocument
$xml.Load('C:\Users\kraer\Desktop\bom.xml')
$finalObject = $xml.bom.components.component | ForEach-Object {
[PSCustomObject]#{
'Name' = $_.name
'Version' = $_.version
'License' = $_.licenses.license.id
}
}
# convert to markdown
$finalObject | ConvertTo-MarkDownTable
P.S. $_.licenses.license.id might be wrong, because it looks like licenses is an array of licences. You would probably want to do something like this here:
($_.licenses | ForEach-Object { $_.license.id }) -join '; '
Related
My script read a path from TFS and I add a string to it but before I need to verify that the path contains or not contains a sign
Example1:
This is the path, in this case I need to add '/database/'
$/Idu Client-Server/CoreBranches/V6.4/Patches/V8.6.22
Example2: I need to add only 'database/'
$/Idu Client-Server/CoreBranches/V6.4/Patches/V8.6.22/
Example3: I need to add '/'
$/Idu Client-Server/CoreBranches/V6.4/Patches/V8.6.22/database
The goal is to continue with the script with the path/database/
so I need to check first the path and then to add or remove the 'database' string
Anyone can help me with that please?
If I understand the question properly, you want to check if the path from TFS ends with a forward slash or not, so you would know what to append to it and for thast you could use a small helper function like this:
function Join-TFSPath {
[CmdletBinding()]
param (
[parameter(Mandatory = $true, Position = 0, ValueFromPipeline = $true)]
[ValidateNotNullOrEmpty()]
[string] $Path,
[parameter(Mandatory = $false, Position = 1)]
[string[]] $ChildPath,
[char]$Separator = '/'
)
if ($ChildPath.Count) {
"{0}$separator{1}$Separator" -f $Path.TrimEnd("\/"),
(($ChildPath | ForEach-Object { $_.Trim("\/") } |
Where-Object { $_ -match '\S' }) -join $Separator)
}
else {
"{0}$separator" -f $Path.TrimEnd("\/")
}
}
# test if you need to add `database` or not
$tfsPath = '$/Idu Client-Server/CoreBranches/V6.4/Patches/V8.6.22/database'
$folder = 'database'
if (($tfsPath.TrimEnd("\/") -split '[\\/]')[-1] -ne $folder) {
# use the function adding the $folder as ChildPath
Join-TFSPath -Path $tfsPath -ChildPath $folder
}
else {
# use the function without specifying the ChildPath so it will only ensure it
# ends with the chosen (or in this case default) separator character
Join-TFSPath -Path $tfsPath
}
As per your comment, you could perhaps then use a more dedicated helper function like:
function Append-TFSPath {
[CmdletBinding()]
param (
[parameter(Mandatory = $true, Position = 0, ValueFromPipeline = $true)]
[ValidateNotNullOrEmpty()]
[string] $Path,
[parameter(Mandatory = $false, Position = 1)]
[string] $ChildPath = 'database',
[char]$Separator = '/'
)
$Path = $Path -replace '[\\/]+$' # trim off final slash(es)
$ChildPath = $ChildPath -replace '^[\\/]|[\\/]$' -replace '\\', $Separator
if ([string]::IsNullOrWhiteSpace($ChildPath) -or ($Path -replace '\\', $Separator) -like "*$ChildPath") {
"{0}$separator" -f $Path
}
else {
"{0}$separator{1}$Separator" -f $Path, $ChildPath
}
}
Then, just send the path as you have received it to the function and it will return the path you want
$tfsPath = '$/Idu Client-Server/CoreBranches/V6.4/Patches/V8.6.22/database'
$folder = 'V8.6.22/database'
Append-TFSPath -Path $tfsPath -ChildPath $folder
# because 'database' is the default value for the ChildPath parameter, you can leave that out:
# Append-TFSPath -Path $tfsPath
Testcases:
Append-TFSPath -Path '$/Idu Client-Server/CoreBranches/V6.4/Patches/V8.6.22'
Append-TFSPath -Path '$/Idu Client-Server/CoreBranches/V6.4/Patches/V8.6.22/'
Append-TFSPath -Path '$/Idu Client-Server/CoreBranches/V6.4/Patches/V8.6.22/database'
Append-TFSPath -Path '$/Idu Client-Server/CoreBranches/V6.4/Patches/V8.6.22/database/'
will all return $/Idu Client-Server/CoreBranches/V6.4/Patches/V8.6.22/database/
I have the following code:
$xml = 'C:\Users\jonb\Desktop\bom.xml'
$ns = #{a = 'http://cyclonedx.org/schema/bom/1.2'}
$xPath = '//a:component'
$components = Select-Xml -Xml $xml -XPath $xPath -Namespace $ns
foreach ($component in $components) {
$name = Select-Xml -Xml $component.Node -XPath './/a:name/text()'-Namespace $ns
$version = Select-Xml -Xml $component.Node -XPath './/a:version/text()'-Namespace $ns
$lic_cond = Select-Xml -Xml $component.Node -XPath './/a:license/a:id/text()'-Namespace $ns
$license = $(If ($lic_cond) {$lic_cond} Else {"NA"})
$finalObject = [pscustomobject]#{
'Name' = $name
'Version' = $version
'License' = $license
}
Write-Output $finalObject
}
Now I would like to convert my $finalObject to a MarkDown Table. Are there any possibilities here?
I have seen that there is a ConvertFrom-Markdown command. However, I did not get anywhere with this command.
Thanks for your help
Just a quick and dirty one:
function ConvertTo-MarkDownTable {
[CmdletBinding()] param(
[Parameter(Position = 0, ValueFromPipeLine = $True)] $InputObject
)
Begin { $Index = 0 }
Process {
if ( !$Index++ ) {
'|' + ($_.PSObject.Properties.Name -Join '|') + '|'
'|' + ($_.PSObject.Properties.ForEach({ '-' }) -Join '|') + '|'
}
'|' + ($_.PSObject.Properties.Value -Join '|') + '|'
}
}
usage:
$finalObject =
[pscustomobject]#{ 'Name' = 'name1'; 'Version' = 'version1'; 'License' = 'license1' },
[pscustomobject]#{ 'Name' = 'name2'; 'Version' = 'version2'; 'License' = 'license2' }
$finalObject |ConvertTo-MarkDownTable
Yields:
|Name|Version|License|
|-|-|-|
|name1|version1|license1|
|name2|version2|license2|
Converted in StackOverflow:
Name
Version
License
name1
version1
license1
name2
version2
license2
I need to use a PowerShell function to format the phone number like below:
Function Format-TelephoneNumber
{
Param (
[Parameter(ValueFromPipeline = $true, Position = 0)]
[Alias('Number')]
[string]$TelephoneNumber,
[Parameter(Position = 1)]
[string]$DefaultCountryCode = '+44'
)
Process
{
$formattedNumber = $TelephoneNumber -replace '[\x09 ]'
If ($formattedNumber -match '\A(?<CountryCode>\+[1-9]\d|0)(?<Number>\d*)\Z')
{
If ($Matches['CountryCode'] -eq '0')
{
$countryCode = $defaultCountryCode
}
Else
{
$countryCode = $Matches['CountryCode']
}
$formattedNumber = $countryCode + ' '
$formattedNumber += -join $Matches['Number'][0 .. 2] + ' '
$formattedNumber += -join $Matches['Number'][3 .. 5] + ' '
$formattedNumber += -join $Matches['Number'][6 .. 8]
$formattedNumber
}
Else
{
Write-Error "Unable to parse the string '$($number)' as telephone number!"
}
}
}
The below script is for retrieving the value of Phone Number from AD Attribute:
$sysInfo = New-Object -ComObject 'ADSystemInfo'
$userDN = $sysInfo.GetType().InvokeMember('UserName', 'GetProperty', $null, $sysInfo, $null)
$adUser = [ADSI]"LDAP://$($userDN)"
[void][Runtime.InteropServices.Marshal]::FinalReleaseComObject($sysInfo)
Write-Host $adUser.mobile.ToString() -ForegroundColor Green
How can I call the script?
I have tried below but failed:
Write-Host "This is raw from AD: $($adUser.mobile.ToString())" -ForegroundColor Yellow
$Formatted = Format-TelephoneNumber -TelephoneNumber $adUser.mobile.ToString()
Write-Host "This is processed using Function: " "$($Formatted)" -ForegroundColor Green
Personally, I'd use a different Format-TelephoneNumber function because as James C commented, your function may truncate last digit(s) from the number.
Below is my attempt:
function Format-TelephoneNumber {
Param(
[Parameter(ValueFromPipeline = $true, Position = 0)]
[Alias('Number')]
[string]$TelephoneNumber,
[Parameter(Position = 1)]
[string]$DefaultCountryCode = '+44'
)
Process {
# replace all hyphens and other possible joining characters with space and trim the result
$number = ($TelephoneNumber -replace '[._~-]', ' ').Trim()
# test if the number starts with a country code
if ($number -match '^(\+\d+)\s') {
$countryCode = $Matches[1]
$number = $number.Substring($countryCode.Length).Trim()
}
else {
$countryCode = $DefaultCountryCode
}
# remove leading zero and any non-digits
$number = $number -replace '^0|\D', ''
if ($number.Length -lt 9) {
Write-Warning "Unable to parse the string '$($TelephoneNumber)' as telephone number!"
}
else {
$parts = #($countryCode)
# split the remaining string in to 3-character parts (+ possible remainder)
$parts += $number -split '(\d{3})' | Where-Object { $_ }
return $parts -join ' '
}
}
}
Why not use the Get-ADUser cmdlet to find the mobile property? Something like:
Import-Module ActiveDirectory
# return the mobile phone number for a user as string or nothing if not found
# $userID is either the users distinguished name, the GUID, the user SID, or the SamAccountName.
$mobile = Get-ADUser -Identity $userID -Properties MobilePhone | Select-Object -ExpandProperty MobilePhone
Note: MobilePhone is the PowerShell or GUI name for the mobile attribute, but you may use either.
Then, if you have this mobile number as string format it using the Format-TelephoneNumber function:
if ($mobile) {
Write-Host "This is raw from AD: $mobile" -ForegroundColor Yellow
$formatted = Format-TelephoneNumber -TelephoneNumber $mobile
Write-Host "This is formatted: $formatted" -ForegroundColor Green
}
Hope that answers your question
I've been wanting an easy to use script that will allow me to replace multiple strings from multiple files for a while. So far I've got this code:
$replacements = #{
'bCompressDiffuseLocalPlayerCharacterTextures=True' = 'bCompressDiffuseLocalPlayerCharacterTextures=False'
'bCompressDiffuseLocalPlayerVehicleTextures=True' = 'bCompressDiffuseLocalPlayerVehicleTextures=False'
'bCompressDiffuseOtherPlayerCharacterTextures=True' = 'bCompressDiffuseOtherPlayerCharacterTextures=False'
'bCompressDiffuseOtherPlayerVehicleTextures=True' = 'bCompressDiffuseOtherPlayerVehicleTextures=False'
'bCompressNormalTextures=True' = 'bCompressNormalTextures=False'
'bDisablePhysXHardwareSupport=True' = 'bDisablePhysXHardwareSupport=False'
'bEnableMouseSmoothing=True' = 'bEnableMouseSmoothing=False'
'bInitializeShadersOnDemand=True' = 'bInitializeShadersOnDemand=False'
'MaxChannels=32' = 'MaxChannels=64'
'MotionBlur=True' = 'MotionBlur=False'
'm_bCalculateOnServer=True' = 'm_bCalculateOnServer=False'
'OneFrameThreadLag=True' = 'OneFrameThreadLag=False'
'PoolSize=140' = 'PoolSize=1024'
'UseMinimalNVIDIADriverShaderOptimization=True' = 'UseMinimalNVIDIADriverShaderOptimization=False'
'UseTextureFileCache=False' = 'UseTextureFileCache=True'
}
function Update-FileContent {
[cmdletbinding()]
param(
[Parameter(ValueFromPipeline=$true,
ValueFromPipelineByPropertyName=$true,
Mandatory=$true,
Position=0)]
[Alias('PsPath')]
$Path
)
$lines = Get-Content $Path
$lines | ForEach-Object {
foreach($rep in $replacements.Keys)
{
$_ = $_ -replace $rep, $replacements[$rep]
}
$_
} | Set-Content $Path
}
Get-ChildItem -Recurse *.ini | Update-FileContent
It works but only if a file is 1 directory deep.
I'd do something like this:
$replacements = #{
'bCompressDiffuseLocalPlayerCharacterTextures=True' = 'bCompressDiffuseLocalPlayerCharacterTextures=False'
'bCompressDiffuseLocalPlayerVehicleTextures=True' = 'bCompressDiffuseLocalPlayerVehicleTextures=False'
'bCompressDiffuseOtherPlayerCharacterTextures=True' = 'bCompressDiffuseOtherPlayerCharacterTextures=False'
'bCompressDiffuseOtherPlayerVehicleTextures=True' = 'bCompressDiffuseOtherPlayerVehicleTextures=False'
'bCompressNormalTextures=True' = 'bCompressNormalTextures=False'
'bDisablePhysXHardwareSupport=True' = 'bDisablePhysXHardwareSupport=False'
'bEnableMouseSmoothing=True' = 'bEnableMouseSmoothing=False'
'bInitializeShadersOnDemand=True' = 'bInitializeShadersOnDemand=False'
'MaxChannels=32' = 'MaxChannels=64'
'MotionBlur=True' = 'MotionBlur=False'
'm_bCalculateOnServer=True' = 'm_bCalculateOnServer=False'
'OneFrameThreadLag=True' = 'OneFrameThreadLag=False'
'PoolSize=140' = 'PoolSize=1024'
'UseMinimalNVIDIADriverShaderOptimization=True' = 'UseMinimalNVIDIADriverShaderOptimization=False'
'UseTextureFileCache=False' = 'UseTextureFileCache=True'
}
function Update-FileContent {
[cmdletbinding()]
param(
[Parameter(ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true, Mandatory=$true, Position=0)]
[Alias('PsPath')]
$Path
)
# read the ini file in as one single string
$content = Get-Content $Path -Raw
# copy that string so we can compare at the end of the loop if anything has changed at all
$newContent = $content
foreach($rep in $replacements.Keys) {
$newContent = $newContent -replace $rep, $replacements[$rep]
}
# only replace the contents of the ini file if changes are made
if ($newContent -ne $content) {
Write-Host "Replacing content of file '$Path'"
Set-Content $Path -Value $content
}
}
$rootPath = '<PATH OF THE ROOTFOLDER TO RECURSE WHERE THE INI FILES ARE LOCATED>'
Get-ChildItem -Path $rootPath -Filter '*.ini' -Recurse | ForEach-Object { Update-FileContent $_.FullName }
I've been trying to compare two hashes of hashes against each other. Unfortunately even with the great help from here I struggled to pull together what I was trying to do.
So, I've resorted to searching the internet again and I was lucky enough to find Edxi's code (full credit to him and Dbroeglin who looks to have written it originally)https://gist.github.com/edxi/87cb8a550b43ec90e4a89d2e69324806 His compare-hash function does exactly what I needed in terms of its comparisons. However, I'd like to report of the full path of the hash in the final output. So I've tried to update the code to allow for this. My thinking being that I should be able to take the Key (aka path) while the code loops over itself but I'm clearly going about it in the wrong manner.
Am I going about this with the right approach, and if not, would someone suggest another method please? The biggest problem I've found so far is that if I change the hash, my code changes don't work e.g. adding 'More' = 'stuff' So it becomes $sailings.Arrivals.PDH083.More breaks the path in the way I've set it.
My version of the code:
$Sailings = #{
'Arrivals' = #{
'PDH083' = #{
'GoingTo' = 'Port1'
'Scheduled' = '09:05'
'Expected' = '10:11'
'Status' = 'Delayed'
}
}
'Departures' = #{
'PDH083' = #{
'ArrivingFrom' = 'Port1'
'Scheduled' = '09:05'
'Expected' = '09:05'
'Status' = 'OnTime'
'Other' = #{'Data' = 'Test'}
}
}
}
$Flights = #{
'Arrivals' = #{
'PDH083' = #{
'GoingTo' = 'Port'
'Scheduled' = '09:05'
'Expected' = '10:20'
'Status' = 'Delayed'
}
}
'Departures' = #{
'PDH083' = #{
'ArrivingFrom' = 'Port11'
'Scheduled' = '09:05'
'Expected' = '09:05'
'Status' = 'NotOnTime'
'Other' = #{'Data' = 'Test_Diff'}
}
}
}
function Compare-Hashtable {
[CmdletBinding()]
param (
[Parameter(Mandatory = $true)]
[Hashtable]$Left,
[Parameter(Mandatory = $true)]
[Hashtable]$Right,
[string] $path,
[boolean] $trackpath = $True
)
write-host "PAth received as: $path"
function New-Result($Key, $LValue, $Side, $RValue) {
New-Object -Type PSObject -Property #{
key = $Key
lvalue = $LValue
rvalue = $RValue
side = $Side
}
}
[Object[]]$Results = $Left.Keys | ForEach-Object {
if ($trackpath ) {
write-host "Working on Path: " $path + $_
}
if ($Left.ContainsKey($_) -and !$Right.ContainsKey($_)) {
write-host "Right key not matched. Report path as: $path"
New-Result $path $Left[$_] "<=" $Null
}
else {
if ($Left[$_] -is [hashtable] -and $Right[$_] -is [hashtable] ) {
if ($path -ne $null -or $path -ne "/") {
$path = $path + "/" + $_
write-host "Sending Path to function as: $path"
}
Compare-Hashtable $Left[$_] $Right[$_] $path
}
else {
$LValue, $RValue = $Left[$_], $Right[$_]
if ($LValue -ne $RValue) {
$path = $path + "/" + $_
write-host "Not a hash so must be a value at path:$path"
New-Result $path $LValue "!=" $RValue
}
else {
Write-Host "Before changing path: $path "
if (($path.Substring(0, $path.lastIndexOf('/'))).length >0) {
$path = $path.Substring(0, $path.lastIndexOf('/'))
Write-Host "After changing path: $path "
}
}
}
}
# if (($path.Substring(0, $path.lastIndexOf('/'))).length >0) {
# Tried to use this to stop error on substring being less than zero
# but clearly doesnt work when you add more levels to the hash
$path = $path.Substring(0, $path.lastIndexOf('/'))
# } else { $path = $path.Substring(0, $path.lastIndexOf('/')) }
}
$Results += $Right.Keys | ForEach-Object {
if (!$Left.ContainsKey($_) -and $Right.ContainsKey($_)) {
New-Result $_ $Null "=>" $Right[$_]
}
}
if ($Results -ne $null) { $Results }
}
cls
Compare-Hashtable $Sailings $Flights
outputs
key lvalue side rvalue
--- ------ ---- ------
/Arrivals/PDH083/Expected 10:11 != 10:20
/Arrivals/GoingTo Port1 != Port
/Departures/PDH083/ArrivingFrom Port1 != Port11
/Departures/PDH083/Status OnTime != NotOnTime
/Departures/PDH083/Other/Data Test != Test_Diff
I won't do that much string manipulation on the $Path but threat $Path as an array and join it to a string at the moment you assign it as a property (key = $Path -Join "/") to the object:
function Compare-Hashtable {
[CmdletBinding()]
param (
[Parameter(Mandatory = $true)]
[Hashtable]$Left,
[Parameter(Mandatory = $true)]
[Hashtable]$Right,
[string[]] $path = #(),
[boolean] $trackpath = $True
)
write-host "Path received as: $path"
function New-Result($Path, $LValue, $Side, $RValue) {
New-Object -Type PSObject -Property #{
key = $Path -Join "/"
lvalue = $LValue
rvalue = $RValue
side = $Side
}
}
$Left.Keys | ForEach-Object {
$NewPath = $Path + $_
if ($trackpath ) {
write-host "Working on Path: " $NewPath
}
if ($Left.ContainsKey($_) -and !$Right.ContainsKey($_)) {
write-host "Right key not matched. Report path as: $NewPath"
New-Result $NewPath $Left[$_] "<=" $Null
}
else {
if ($Left[$_] -is [hashtable] -and $Right[$_] -is [hashtable] ) {
Compare-Hashtable $Left[$_] $Right[$_] $NewPath
}
else {
$LValue, $RValue = $Left[$_], $Right[$_]
if ($LValue -ne $RValue) {
New-Result $NewPath $LValue "!=" $RValue
}
}
}
}
$Right.Keys | ForEach-Object {
$NewPath = $Path + $_
if (!$Left.ContainsKey($_) -and $Right.ContainsKey($_)) {
New-Result $NewPath $Null "=>" $Right[$_]
}
}
}
cls
Compare-Hashtable $Sailings $Flights
side-note: Write Single Records to the Pipeline (SC03), see: Strongly Encouraged Development Guidelines. In other words, don't do the $Results += ... but intermediately put any completed result in the pipeline for the next cmdlet to be picked up (besides, it unnecessarily code)...