Fix: Calling a function with multiple parameters in PowerShell, merges all parameters in the first parameter as an array

 

When you call a PowerShell function, that expects multiple parameters, with parenthesis, all parameters will be merged in the first parameter, as an array.

The solution is, to call the function with named parameters.

So NOT JoinText($p1, $p2, $p3) but JoinText -p1 $p1 -p2 $p2 -p3 $p3

 

script1.ps1

function JoinText([string]$p1, [string]$p2, [string]$p3) {

return “$p1 $p2 $p3”

}

 

script2-wrong.ps1

$p1 = “some text 1”

$p2 = “some text 2”

$p3 = “some text 3”

$result = JoinText($p1, $p2, $p3)

write-host $result

 

script2-right.ps1

$p1 = “some text 1”

$p2 = “some text 2”

$p3 = “some text 3”

$result = JoinText -p1 $p1 -p2 $p2 -p3 $p3

write-host $result

Calling a function from another PowerShell script file in the same folder with “relative path”

 

If you want to execute a function from another PowerShell script file, you can “dot-source” the file.

The “.” is a special operator in PowerShell that can load another PowerShell script file en import all code in it.

 

Assume both PowerShell scripts are in the same folder, we can use the code in script1.ps1 in script2.ps1 in the following way:

 

PowerShell script1.ps1

 

function ReturnGivenText {

Param(

        [Parameter(Mandatory=$True)]

        [string]$text

    )

    return $text

}

 

PowerShell script2.ps1

$text = ReturnGivenText -text “Hello world”

write-host $text

 

This will output:

Hello world

 

Note: I use the PowerShell >3 global variable $PSScriptRoot, to make the relative path, absolute, so we can use the “.” operator.

 

Fix: Power BI POST request returns a 500 Internal Server Error

When posting to the Power BI api uri: https://api.powerbi.com/v1.0/myorg/groups in PowerShell version 5.1.17134.228, I got the error:

The remote server returned an error: (500) Internal Server Error.

 

I invoked the Power BI rest api with the PowerShell script you can find below.

The error was fixed, when I added -ContentType ‘application/json’ parameter to the InvokeRestMethod

 

 

PowerShell script

 

$clientId = “my-client-id”

$PowerBIUserName = “MyUserName”

$PowerBIUserPassword = “MyUserPassword”

$creds = New-Object “Microsoft.IdentityModel.Clients.ActiveDirectory.UserCredential” -ArgumentList $PowerBIUserName,$PowerBIUserPassword

$resourceAppIdURI = “https://analysis.windows.net/powerbi/api”

$authority = “https://login.microsoftonline.com/common/oauth2/authorize”;

$authContext = New-Object “Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContext” -ArgumentList $authority

 

# Acquire token

$authResult = $authContext.AcquireToken($resourceAppIdURI, $clientId, $creds)

 

# Create a new group

$auth_header = @{

‘Authorization’=$token.CreateAuthorizationHeader()

}

$uri = “https://api.powerbi.com/v1.0/myorg/groups”

$body = “{`”name`”:`”myNewGroup`”}”

$response = Invoke-RestMethod -Uri $uri -Headers $auth_header -Method POST -Body $body -ContentType ‘application/json’

$target_group_id = $response.id