Currently browsing: PowerShell

Get values of all the properties of a PowerShell object

Lets say you want to list all properties of an object but you don’t know all of them by name. Here is comming in help the Get-Member cmdlet. I find it very useful especially when working with new objects that I’m not familiar with.

I’m going to give an example with the list of network adapters listed from the WMI.

 So, lets list all of the members.

Get-Wmiobject Win32_NetworkAdapter | where { $_.Speed -ne $null -and $_.MACAddress -ne $null } 
Read more

XSLT processor with PowerShell

Here is a simple way to transform XML document with specified XSL in PowerShell. It is easy achieavable using the System.Xml.Xsl namespace of the .NET.

PARAM
(
    [Parameter(Mandatory = $true)]
    [ValidateNotNullOrEmpty()]
    [String]$XmlPath,
     
    [Parameter(Mandatory = $true)]
    [ValidateNotNullOrEmpty()]
    [String]$XslPath,
     
    [Parameter(Mandatory = $true)]
    [ValidateNotNullOrEmpty()]
    [String]$HtmlOutput
)
 
try
{
    $XslPatht = New-Object System.Xml.Xsl.XslCompiledTransform
    $XslPatht.Load($XslPath)
    $XslPatht.Transform($XmlPath, $HtmlOutput)
     
    Write-Host "Generated output is on path: $HtmlOutput"
}
catch
{
    Write-Host $_.Exception -ForegroundColor Red
}

We pass the path to the XML file and the path to the …

Read more

PowerShell 2.0 and Write-Verbose strange behaviour

I was writting a script for work wich was invoked on Windows 2012 R2 with PowerShell 4.0 as well on Windows 2008 R2 with PowerShell 2.0. This script was calling some sg utils and comparing the results. There is option to call it and to get just the result or to call it with more messges to see what actually is doing. So Write-Verbose seemed as a perfect option. In the first case all works fine with PowerShell (PS) 4.0. …

Read more