Search code examples
powershellbamboo

Unable to find type when running powershell -command


I have a PowerShell script which I intend to use as a deployment step in Bamboo. Opening PowerShell and running the script with ./script.ps1 works fine, but using powershell.exe -command ./script.ps1 fails with error Unable to find type [Microsoft.PowerShell.Commands.WebRequestMethod].

What is the difference between running the script directly from PowerShell and by using powershell.exe -command? What am I missing?

MWE for the issue in question:

function Test-RestMethod {
    param([string]$Uri, [Microsoft.PowerShell.Commands.WebRequestMethod] $Method = 'Get')

    $result = Invoke-RestMethod $uri -Method $Method
    return $result
}

Test-RestMethod -Uri https://blogs.msdn.microsoft.com/powershell/feed/ -Method 'Get' | Format-Table -Property Title, pubDate

Solution

  • To make a type available, if PowerShell does not load it already automatically, just add the corresponding module or assembly manually by using Import-Module or Add-Type. In your case, you have to load an Assembly as can be derived from the docs (Microsoft.PowerShell.Commands.WebRequestMethod):

    Add-Type -AssemblyName Microsoft.PowerShell.Commands.Utility