PWSH – ValidateScript as reference to external ps1 script

I would like to validate my powershell function parameters with ValidateScript. Example param ( [ValidateScript({ $response = (Invoke-webrequest -URI $_) if ($response.StatusCode -ne 200) { throw " ❌ API Nedpoint $($_) is not reachable" } return $true })] [Parameter(Mandatory)] [string]$buildURL ) This is valid code, but I would prefere to reference external ps1 file like… Read More PWSH – ValidateScript as reference to external ps1 script

Accessing object property named with dollar sign in PowerShell

I have a JSON (array of objects) obtained via Invoke-RestMethod and I try to iterate trough them using foreach. The property I’m interested to use as a filter is named "$value" and I cannot change this, being a COTS application. foreach($item in $Result.value) { if ($item.properties.threadType) { Write-Host $item.properties.threadType if ($item.properties.threadType.$value -eq "1234567") { Write-Host… Read More Accessing object property named with dollar sign in PowerShell

Assignment of version-like value (numbers with two or more dots) in Powershell

Recently I discover some nasty behavior about assigning some version-like value to variable in Powershell (at least in 7.2.5). At first I tried: > $version = 1.2.3 > echo $version I quickly find out that I have to escape value with quotes to make it string explicitly. This one works well: > $version = "1.2.3"… Read More Assignment of version-like value (numbers with two or more dots) in Powershell

Generate CLIXML string from a PowerShell object without serializing to disk first

I have the following code which exports an object to an XML file, then reads it back in and prints it on the Information stream. try{ # Sample object $Person = @{ Name = ‘Bender’ Age = ‘At least 1074’ } $Person | Export-CliXml obj.xml $cliXml = Get-Content -Raw ./obj.xml Write-Host $cliXml } finally {… Read More Generate CLIXML string from a PowerShell object without serializing to disk first