Pass function as a parameter

I’m not sure this is the best, but: function A{ Param([scriptblock]$FunctionToCall) Write-Host “I’m calling $($FunctionToCall.Invoke(4))” } function B($x){ Write-Output “Function B with $x” } Function C{ Param($x) Write-Output “Function C with $x” } PS C:\WINDOWS\system32> A -FunctionToCall $function:B I’m calling Function B with 4 PS C:\WINDOWS\system32> A -FunctionToCall $function:C I’m calling Function C with 4 … Read more

Getting “Can’t find the drive. The drive called ‘IIS’ does not exist.”

The drive is provided by the WebAdministration module, so you need to install/import that module first. How you install the module depends on your actual system and whether you use GUI or PowerShell. On a Windows Server 2008 R2 for instance you’d install the module with the following PowerShell commands: Import-Module ServerManager Add-WindowsFeature Web-Scripting-Tools After … Read more

What is `$?` in Powershell?

It returns true if the last command was successful, else false. However, there are a number of caveats and non-obvious behaviour (e.g. what exactly is meant by “success”). I strongly recommend reading this article for a fuller treatment. For example, consider calling Get-ChildItem. PS> Get-ChildItem PS> $? True $? will return True as the call … Read more

dotnet ef scaffold Unrecognized option ‘-t firstTable -t secondTable’ – pass arguments stored in a string

If you construct a string such as -t foo and pass it via a variable to an external program, it is passed as a single, double-quoted argument (that is, donet will literally see “-t foo” on its command line) – and therefore won’t be recognized as parameter name-value combination. You must pass -t and foo … Read more

Are you able to use PtrToStringAuto to decrypt a secure string in Powershell 7 on macOS?

Note that [securestring] is not recommended for new code anymore. While on Windows secure strings offer limited protection – by storing the string encrypted in memory – via the DPAPI – and by shortening the window during which the plain-text representation is held in memory, no encryption at all is used on Unix-like platforms.[1] The … Read more