Question: ```
$Auth = "admin:password"
$JenkinsURL = "http://$Auth@172.24.235.27:8080/"
$JobName = "TestItem1"
$JobToken = "token"
$FullURL = "$JenkinsURL/job/$JobName/build?token=$JobToken"
Invoke-WebRequest -UseBasicParsing $FullURL

```

Above is the PowerShell code used for triggering Jenkins job. But while executing this I am facing "Authentication required" error. But the same command from `curl` is working fine.

I am not sure whether I am missing something in URL or missing some Jenkins plugin to provide access from PowerShell.

Answer: 
The reason you are getting an authentication error is that you will need to convert the authentication to base 64 string. Below is the script that you can use if you have not enabled the [CSRF](https://wiki.jenkins.io/display/JENKINS/CSRF+Protection) in Jenkins.

```
$UserName = "admin"
$Password = "password"
$API_URL = "jenkinsservername"
$JobName = "TestItem1"
$JobToken = "token"
$header = @{}
$Params = @{}
$header.Add('Authorization', 'Basic ' + [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes("$(${UserName}):$(${Password})")))
$Params['uri'] = "http://jenkinsservername/$JobName/build?token=$JobToken"
$Params['Method'] = 'Post'
$Params['Headers'] = $header
Invoke-RestMethod @Params 

```

But If you have [CSRF](https://wiki.jenkins.io/display/JENKINS/CSRF+Protection) Enabled in Jenkins then use below script

```
$UserName = "admin"
$Password = "password"
$API_URL = "jenkinsservername"
$JobName = "TestItem1"
$JobToken = "token"
$header = @{}
$header.Add('Authorization', 'Basic ' + [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes("$(${UserName}):$(${Password})")))
$Params = @{uri = "http://${API_URL}:${API_Port}/crumbIssuer/api/json";
        Method = 'Get';
        Headers = $header;}
$API_Crumb = Invoke-RestMethod @Params
write-host $API_Crumb
$h.Add('Jenkins-Crumb', $API_Crumb.crumb)
$Params['uri'] = "http://jenkinsservername/$JobName/build?token=$JobToken"
$Params['Method'] = 'Post'
$Params['Headers'] = $header
Invoke-RestMethod @Params

```
@Mike correctly described the instructions for working with the jenkins api when using CSRF. But I would like to add that when creating crumbs, the session and cookies are also taken into account.

The code that works for me is as follows:

```
$UserName = "admin"
$Password = "password"
$API_URL = "jenkinsservername"
$JobName = "TestItem1"
$JobToken = "token"
$header = @{}
$header.Add('Authorization', 'Basic ' + [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes("$(${UserName}):$(${Password})")))
$Params1 = @{uri = "http://${API_URL}:${API_Port}/crumbIssuer/api/json";
            Method = 'Get';
            SessionVariable = 'Session';
            Headers = $header;}
$API_Crumb = Invoke-RestMethod @Params1
write-host $API_Crumb
$header.Add('Jenkins-Crumb', $API_Crumb.crumb)
$Params2 = @{}
$Params2['uri'] = "http://jenkinsservername/$JobName/build?token=$JobToken"
$Params2['Method'] = 'Post'
$Params2['Headers'] = $header
Invoke-RestMethod @Params2 -WebSession $Session

```

Also consider if the job is in a folder, then the uri will be different.
For example, if MyJob is in MyFolder then the uri will be:

```
http://jenkinsservername/job/MyFolder/job/MyJob/build?token=JobToken

```

You can see this path in the place where you assigned the token for the job