PowerShell Classifier with File Server Resource Manager - powershell

I'm trying to use the Windows PowerShell Classifier in FSRM on Server 2019. I need it to look for files that start with "~$" and classify them with a Yes or No property I created.
I would also be fine with a REGEX code as well.
This is what I have but it's not working:
# Global variables available:
# $ModuleDefinition (IFsrmPipelineModuleDefinition)
# $Rule (IFsrmClassificationRule)
# $PropertyDefinition (IFsrmPropertyDefinition)
#
# And (optionally) any parameters you provide in the Script parameters box below,
# i.e. "$a = 1; $b = 2" . The string you enter is treated as a script and executed so the
# variables you define become globally available
# optional function to specify when the behavior of this script was last modified
# if it consumes additional files. emit one value of type DateTime
#
# function LastModified
# {
# }
# required function that outputs a value to be assigned to the specified property for each file classified
# emitting no value is allowed, which causes no value to be assigned for the property
# emitting more than one value will result in errors during classification
# begin and end are optional; process is required
#
function GetPropertyValueToApply
{
# this parameter is of type IFsrmPropertyBag
# it also has an additional method, GetStream, which returns a IO.Stream object to use for
# reading the contents of the file. Make sure to close the stream after you are done reading
# from the file
param
(
[Parameter(Position = 0)] $PropertyBag
)
Process
{
$FileName = $_.Name
If($FileName -like "~$*")
{
$True
}
Else
{
$False
}
}

Related

Defining parameters common to all functions within a PowerShell module

I am writing a PowerShell module, the functions inside this module have some parameters which will be re-used across all functions. Rather than copy-pasting the function definition each time I add a new function, I would like to define them at the top like a script variable and then insert them into each function, giving me a single place to update if they need to be changed.
Looking at how dynamic parameters are defined it seems like I should be able to define an object of that type and then reference it in the function definitions, but I can't find anything online giving me the correct syntax to do this.
Using PowerShell version 7.2
$Script:ReUsedParameters = param(
[Parameter()]
[String]$Name,
[Parameter()]
[Int]$Id
)
Function New-Command {
Param ($ReUsedParameters)
Write-Output "Name: $Name, ID: $ID"
}
For the sake of answering, you can store the runtime parameters definitions in a script block and then call it & inside the function's dynamicparam block.
I do not think this is a good idea nor I recommend using this. All functions should have their own repeated param blocks if needed.
$reusedParameters = {
$paramDictionary = [System.Management.Automation.RuntimeDefinedParameterDictionary]::new()
# Since both parameters don't have any arguments (Mandatory, Position, ValueFromPipeline, etc..)
# you can use this one for both, otherwise, each dynamic parameter should have their own
# Parameter Declaration
[Parameter[]] $paramAttribute = [Parameter]::new()
$paramDictionary['Name'] = [System.Management.Automation.RuntimeDefinedParameter]::new('Name', [string], $paramAttribute)
$paramDictionary['Id'] = [System.Management.Automation.RuntimeDefinedParameter]::new('Id', [int], $paramAttribute)
return $paramDictionary
}
Function New-Command {
[CmdletBinding()] # `CmdletBinding` is Mandataroy here
param() # if the `param` block is empty
dynamicparam {
& $reusedParameters
}
end {
# Caveat: you can reference these parameters via $PSBoundParameters
# $Name and $Id are not part of the `param` block
# hence that wouldn't work here
"Name: {0}, ID: {1}" -f $PSBoundParameters['Name'], $PSBoundParameters['ID']
}
}
New-Command -Name asd -Id 123
As a declarative approach, you may turn the common parameters into class properties and have a single function parameter of the class type.
class MyReUsedParameters {
[String] $Name
[Int] $Id = 23
}
Function New-Command {
Param (
[MyReUsedParameters] $ReUsedParameters,
$AnotherParam
)
Write-Output "Name: $($ReUsedParameters.Name), ID: $($ReUsedParameters.ID)"
}
# Pass the common parameters as a hashtable which gets converted to
# MyReUsedParameters automatically.
New-Command -ReUsedParameters #{ Name = 'foo'; Id = 42 } -AnotherParam bar
# Alternatively pass the common parameters as a (typed) variable.
# PowerShell is able to deduce the argument name from the type.
$commonArgs = [MyReUsedParameters] #{ Name = 'Foo'; Id = 42 }
New-Command $commonArgs -AnotherParam bar
When passing a hashtable or PSCustomObject that has matching properties, it will automatically be converted to the class type.
You may even validate class properties similar to regular parameters. Most parameter validation attributes can be specified for class properties as well.
class MyReUsedParameters {
[ValidateNotNullOrEmpty()] [String] $Name
[Int] $Id = 23
# Constructor - required to apply validation
MyReUsedParameters( [Hashtable] $ht ) {
$this.Name = $ht.Name
$this.Id = $ht.Id
}
}
Function New-Command {
Param (
[Parameter(Mandatory)]
[MyReUsedParameters] $ReUsedParameters
)
Write-Output "Name: $($ReUsedParameters.Name), ID: $($ReUsedParameters.ID)"
}
# Causes an error (as expected), because Name property is missing
New-Command -ReUsedParameters #{ Id = 42 }

Reuse parameters among multiple powershell functions

Consider two powershell functions:
function A {
params(
[Parameter()]$x,
[Parameter()]$y
)
Write-Host $x $y
}
function B {
params(
[Parameter()]$x,
[Parameter()]$z
)
Write-Host $x $z
}
I'd like to define a single parameter $x once (which could have fairly complex attributes that must be kept identical for both functions) and re-use it in both functions, so something like:
$x = {[Parameter()]$x}
function A {
params(
$x,
[Parameter()]$y
)
Write-Host $x $y
}
function B {
params(
$x,
[Parameter()]$z
)
Write-Host $x $z
}
(How) is this possible?
To reuse parameter declarations across functions - as requested in your question - see the following section.
To reuse parameter values (arguments) across functions, by way of presets (default value), see the bottom section.
In order to reuse parameter declarations - short of using design-time templating to generate source code - you need to to define a script block that creates a dynamic parameter that can be passed to the dynamicparam block of multiple advanced functions:
using namespace System.Management.Automation
# The script block that declares the dynamic parameter to be shared
# across functions.
$sharedDynParam = {
# Define the -x parameter dynamically.
$paramName = 'x'
$dict = [RuntimeDefinedParameterDictionary]::new()
$dict.Add(
$paramName,
[RuntimeDefinedParameter]::new(
$paramName,
[datetime], # Type the parameter [datetime]. for instance.
[ParameterAttribute] #{
Mandatory = $true # Make the parameter mandatory, for instance.
# ParameterSetName = 'default' # Assign it to a parameter set, if neeeded.
}
)
)
# Return the dictionary
return $dict
}
function A {
[CmdletBinding()]
param(
$y
)
# Assign the shared dynamic parameter.
dynamicparam { & $sharedDynParam }
# The use of `dynamicparam { ... }` requires use of an explicit
# `process { ... }` block (and optionally `begin { ... }` and
# `end { ... }`, as needed).
process {
# Note: A dynamic -x parameter cannot be accessed as $x
# Instead, it must be accessed via the $PSBoundParameters dictionary.
"[$($PSBoundParameters['x'])] - [$y]"
}
}
function B {
[CmdletBinding()]
param(
$z
)
# Assign the shared dynamic parameter.
dynamicparam { & $sharedDynParam }
process {
"[$($PSBoundParameters['x'])] - [$z]"
}
}
# Sample calls,
A -x '1970-01-01' -y yval
B -x '1970-01-02' -z zval
Output:
[01/01/1970 00:00:00] - [yval]
[01/02/1970 00:00:00] - [zval]
To preset the value of a parameter by a given name across commands, use the $PSDefaultParameterValues preference variable:
# Preset a parameter value for all commands ('*') that have
# an -x ('x') parameter.
$PSDefaultParameterValues = #{ '*:x' = [pscustomobject] #{ foo = 1; bar = 2 } }
function A {
[CmdletBinding()] # This makes the function an *advanced* one, which respects
# $PSDefaultParameterValues; similarly, at least one
# parameter-individual [Parameter()] attribute does the same.
param(
$x,
$y
)
"[$x] - [$y]"
}
function B {
[CmdletBinding()]
param(
$x,
$z
)
"[$x] - [$z]"
}
# Sample calls, without an -x argument, relying on
# $PSDefaultParameterValues to provide it automatically.
A -y yval
B -z zval
Output, showing that parameter -x was automatically bound via $PSDefaultParameterValues:
[#{foo=1; bar=2}] - [yval]
[#{foo=1; bar=2}] - [zval]

Automatically retrieve Allowed Types for Constrained Language mode

For my hobby project ConvertTo-Expression, I would like the output expression of my cmdlet (by default) compliant with the Constrained Language mode. For this, I might include a hardcoded list with Allowed Types:
$AllowedTypes = # https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_language_modes?view=powershell-7
[Array],
[Bool],
[byte],
[char],
[DateTime],
[decimal],
...
but I rather automatically retrieve the list of Allowed Types from PowerShell itself, knowing that would be most up-to-date version (e.g. the [Ordered] type isn't listed on the website but does appear to be allowed in Constrained Language mode).
Is there a way to do this?
Or:
How can I check (in full language mode) if a specific type is compliant with constrained language mode?
How can I check (in full language mode) if a specific type is compliant with constrained language mode?
You can use something like the following, based on the Test-TypePermitted function defined further below:
PS> [System.IO.FileInfo], [int] | Test-TypePermitted -Mode Constrained
TypeName Permitted Message
-------- --------- -------
System.IO.FileInfo False Cannot create type. Only core types are supported in this language mode.
System.Int32 True
Official docs re PowerShell language modes (which you also link to from the question): about_Language_Modes
Test-TypePermitted function:
function Test-TypePermitted {
[CmdletBinding(PositionalBinding = $false)]
param(
[Parameter(ValueFromPipeline, Position = 0)]
[Type[]] $Type
,
[Parameter(Position = 1)]
[Alias('Mode')]
[ValidateSet('Restricted', 'Constrained', 'FullLanguage')]
$LanguageMode = 'Constrained'
)
begin {
try {
$ps = [powershell]::Create()
$ps.Runspace.SessionStateProxy.LanguageMode = $LanguageMode
} catch { Throw }
}
process {
foreach ($t in $type) {
$expression = switch -Wildcard ($LanguageMode) {
'Restricted*' {
# In 'RestrictedLanguage' mode, seemingly just referencing the *type*
# of a non-permitted type causes an error.
'[{0}]' -f $t.FullName
}
Default {
# In 'ConstrainedLanguage' mode, whether a type is permitted or not
# only surfaces when you try to *construct an instance*.
# Note: New-Object can construct value types even without argument.
# For reference types, it succeeds only if there is a (public)
# parameterless constructor.
# However, fortunately, construction isn't even attempted if
# the type isn't permitted.
'New-Object ''{0}''' -f $t.FullName
}
}
$message = $null
$permitted = try {
if ($ps.AddScript($expression).Invoke().Count -ne 0) {
$true
} elseif ($ps.Streams.Error[0].FullyQualifiedErrorId -Match 'CannotFindAppropriateCtor') {
# Type is permitted in principle, but couldn't be constructed due to not having a parameterless constructor.
$true
} else {
# Type is not permitted.
$message = $ps.Streams.Error[0].ToString()
$false
}
} catch { # Happens in RestrictedLanguage mode.
# Type is not permitted.
$message = ($_.ToString() -split '\r?\n')[-1].TrimEnd('"')
$false
}
}
[pscustomobject] #{
TypeName = $t.FullName
Permitted = $permitted
Message = $message
}
# Prepare for next iteration.
$ps.Commands.Clear(); $ps.Streams.ClearStreams()
}
end {
$ps.Dispose()
}
}

PowerShell: DynamicParam: get list of passed parameters?

Good afternoon
Unfortunately, PowerShell is not able to detect the ParameterSet by the Parameter Types, for example: If the 2nd Parameter is passed as a Int, then select ParameterSet1, otherwise use ParameterSet2.
Therefore I would like to manually detect the passed Parameter-Combinations.
Is it possible to get the list of passed parameters in DynamicParam, something like this?:
Function Log {
[CmdletBinding()]
Param ()
DynamicParam {
# Is is possible to access the passed Parameters?,
# something like that:
If (Args[0].ParameterName -eq 'Message') { … }
# Or like this:
If (Args[0].Value -eq '…') { … }
}
…
}
Thanks a lot for any help and light!
Thomas
This first finding was wrong!:
"I've found the magic, by using $PSBoundParameters we can access the passed parameters."
This is the correct but very disappointing answer:
It's very annoying and unbelievable, but it looks like PowerShell does not pass any information about the dynamically passed arguments.
The following example used the New-DynamicParameter function as defined here:
Can I make a parameter set depend on the value of another parameter?
Function Test-DynamicParam {
[CmdletBinding()]
Param (
[string]$FixArg
)
DynamicParam {
# The content of $PSBoundParameters is just
# able to show the Params declared in Param():
# Key Value
# --- -----
# FixArg Hello
# Add the DynamicParameter str1:
New-DynamicParameter -Name 'DynArg' -Type 'string' -HelpMessage 'DynArg help'
# Here, the content of $PSBoundParameters has not been adjusted:
# Key Value
# --- -----
# FixArg Hello
}
Begin {
# Finally - but too late to dynamically react! -
# $PSBoundParameters knows all Parameters (as expected):
# Key Value
# --- -----
# FixArg Hello
# DynArg World
}
Process {
…
}
}
# Pass a fixed and dynamic parameter
Test-DynamicParam -FixArg 'Hello' -DynArg 'World'

Powershell determine new URL of a permanently moved (redirected) resource

I'm using Powershell Core v6-beta.5 using AppImage on Linux. Is there a way to find out the "new" location of a 301 redirect?
Invoke-WebRequest -Method HEAD http://SomethingThatThrows301.com/ -MaximumRedirection 0 throws an error (Response status code does not indicate success: 301 (Moved Permanently)).
While the error does mention that the move is a 301, I'd still like a proper object telling me that, and the new address.
Is there a way to do so?
Note: All code below works in both Windows PowerShell and PowerShell Core, on all supported platforms, with up to 50 redirections by default.
Assuming:
that you don't care about the specific 3xx redirection status code and
that you only need to know the ultimate target URL (there could be a chain of redirections)
use the following:
[System.Net.HttpWebRequest]::Create('http://cnn.com').GetResponse().ResponseUri.AbsoluteUri
This yields (note how the target URL has www.):
http://www.cnn.com
Below is the source code for advanced convenience function Get-UrlRedirection, which packages the functionality, offering both resolution to the ultimate target URL and an enumeration of the chain of redirection URLs.
Example calls:
> Get-UrlRedirection http://cnn.com
http://www.cnn.com
> Get-UrlRedirection -Enumerate http://microsoft.com/about
http://microsoft.com/about
https://microsoft.com/about
https://www.microsoft.com/about
https://www.microsoft.com/about/
https://www.microsoft.com/about/default.aspx
https://www.microsoft.com/en-us/about/
Function Get-UrlRedirection {
[CmdletBinding()]
Param (
[Parameter(Mandatory, ValueFromPipeline)] [Uri] $Url,
[switch] $Enumerate,
[int] $MaxRedirections = 50 # Use same default as [System.Net.HttpWebRequest]
)
process {
try {
if ($Enumerate) { # Enumerate the whole redirection chain, from input URL to ultimate target,
# assuming the max. count of redirects is not exceeded.
# We must walk the chain of redirections one by one.
# If we disallow redirections, .GetResponse() fails and we must examine
# the exception's .Response object to get the redirect target.
$nextUrl = $Url
$urls = #( $nextUrl.AbsoluteUri ) # Start with the input Uri
$ultimateFound = $false
# Note: We add an extra loop iteration so we can determine whether
# the ultimate target URL was reached or not.
foreach($i in 1..$($MaxRedirections+1)) {
Write-Verbose "Examining: $nextUrl"
$request = [System.Net.HttpWebRequest]::Create($nextUrl)
$request.AllowAutoRedirect = $False
try {
$response = $request.GetResponse()
# Note: In .NET *Core* the .GetResponse() for a redirected resource
# with .AllowAutoRedirect -eq $False throws an *exception*.
# We only get here on *Windows*, with the full .NET Framework.
# We either have the ultimate target URL, or a redirection
# whose target URL is reflected in .Headers['Location']
# !! Syntax `.Headers.Location` does NOT work.
$nextUrlStr = $response.Headers['Location']
$response.Close()
# If the ultimate target URL was reached (it was already
# recorded in the previous iteration), and if so, simply exit the loop.
if (-not $nextUrlStr) {
$ultimateFound = $true
break
}
} catch [System.Net.WebException] {
# The presence of a 'Location' header implies that the
# exception must have been triggered by a HTTP redirection
# status code (3xx).
# $_.Exception.Response.StatusCode contains the specific code
# (as an enumeration value that can be case to [int]), if needed.
# !! Syntax `.Headers.Location` does NOT work.
$nextUrlStr = try { $_.Exception.Response.Headers['Location'] } catch {}
# Not being able to get a target URL implies that an unexpected
# error ocurred: re-throw it.
if (-not $nextUrlStr) { Throw }
}
Write-Verbose "Raw target: $nextUrlStr"
if ($nextUrlStr -match '^https?:') { # absolute URL
$nextUrl = $prevUrl = [Uri] $nextUrlStr
} else { # URL without scheme and server component
$nextUrl = $prevUrl = [Uri] ($prevUrl.Scheme + '://' + $prevUrl.Authority + $nextUrlStr)
}
if ($i -le $MaxRedirections) { $urls += $nextUrl.AbsoluteUri }
}
# Output the array of URLs (chain of redirections) as a *single* object.
Write-Output -NoEnumerate $urls
if (-not $ultimateFound) { Write-Warning "Enumeration of $Url redirections ended before reaching the ultimate target." }
} else { # Resolve just to the ultimate target,
# assuming the max. count of redirects is not exceeded.
# Note that .AllowAutoRedirect defaults to $True.
# This will fail, if there are more redirections than the specified
# or default maximum.
$request = [System.Net.HttpWebRequest]::Create($Url)
if ($PSBoundParameters.ContainsKey('MaxRedirections')) {
$request.MaximumAutomaticRedirections = $MaxRedirections
}
$response = $request.GetResponse()
# Output the ultimate target URL.
# If no redirection was involved, this is the same as the input URL.
$response.ResponseUri.AbsoluteUri
$response.Close()
}
} catch {
Write-Error $_ # Report the exception as a non-terminating error.
}
} # process
}
In order to focus on the code, I've omitted the comment-based help above; here it is - simply paste it directly above the function definition:
<#
.SYNOPSIS
Gets a URL's redirection target(s).
.DESCRIPTION
Given a URL, determines its redirection target(s), as indicated by responses
with 3xx HTTP status codes.
If the URL is not redirected, it is output as-is.
By default, the ultimate target URL is determined (if there's a chain of
redirections), but the number of redirections that are followed is limited
to 50 by default, which you may change with -MaxRedirections.
-Enumerate enumerates the redirection chain and returns an array of URLs.
.PARAMETER Url
The URL whose redirection target to determine.
You may supply multiple URLs via the pipeline.
.PARAMETER MaxRedirections
Limits the number of redirections that are followed, 50 by default.
If the limit is exceeded, a non-terminating error is reported.
.PARAMETER Enumerate
Enumerates the chain of redirections, if applicable, starting with
the input URL itself, and outputs it as an array.
If the number of actual redirections doesn't exceed the specified or default
-MaxRedirections value, the entire chain up to the ultimate target URL is
enumerated.
Otherwise, a warning is issued to indicate that the ultimate target URL wasn't
reached.
All URLs are output in absolute form, even if the targets are defined as
relative URLs.
Note that, in order to support multiple input URLs via the pipeline, each
array representing a redirection chain is output as a *single* object, so
with multiple input URLs you'll get an array of arrays as output.
.EXAMPLE
> Get-UrlRedirection http://cnn.com
http://www.cnn.com
.EXAMPLE
> Get-UrlRedirection -Enumerate http://microsoft.com/about
http://microsoft.com/about
https://microsoft.com/about
https://www.microsoft.com/about
https://www.microsoft.com/about/
https://www.microsoft.com/about/default.aspx
https://www.microsoft.com/en-us/about/
.NOTES
This function uses the [System.Net.HttpWebRequest] .NET class and was
inspired by http://www.powershellmagazine.com/2013/01/29/pstip-retrieve-a-redirected-url/
#>
If you ignore the error being thrown, you will be able to examine the HTTP response. The new URL will be in the Location header.
Try something like the below.
$url="https://jigsaw.w3.org/HTTP/300/301.html"
$resp = Invoke-WebRequest -Method HEAD $url -MaximumRedirection 0 -ErrorAction Ignore
$code = $resp.StatusCode
Write-Output "URL: $url"
Write-Output "ErrorCode: $code"
if($code -eq 301) {
$loc = $resp.Headers.Location
Write-Output "New URL: $loc"
}