I am trying to extract a build number via POWERSHELL to a TXT FILE, I have the GET request and the URI is arranged.
I have a format with PAT TOKEN, but I can't do it, how do I do OutFile to a TXT file?
# Build Auth header
$MyPat = "XXXX"
$B64Pat = [Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes(":$MyPat"))
$h = #{'Authorization' = 'Basic ' + $B64Pat}
$BuildNumberInfo = Invoke-WebRequest -Uri "https://dev.azure.com/$($Company)/$($Project)/_apis/build/builds/$($BuildID)?api-version=6.0-preview.6" -Method 'GET'
Use Invoke-RestMethod instead of Invoke-WebRequest:
# Build Auth header
$MyPat = "XXX"
$B64Pat = [Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes(":$MyPat"))
$h = #{'Authorization' = 'Basic ' + $B64Pat}
$BuildNumberInfo = Invoke-RestMethod -Uri "https://dev.azure.com/$($Company)/$($Project)/_apis/build/builds/$($BuildID)?api-version=6.0-preview.6" -Method 'GET' -Headers $h
$BuildNumberInfo.buildNumber | Out-File buildnumber.txt
Related
I need to import some files to Power BI using API and powershell
First I create a temporary upload with: groups/group_id/imports/createTemporaryUploadLocation
Then I push my file to the URL
Invoke-RestMethod -Method PUT -Uri \"${url}\" -Header #{ \'x-ms-blob-type\' = \'BlockBlob\'; \'Content-Length\' = $len } -InFile '$myFile' -TimeoutSec 3600
Finally, I try to import that URL to PowerBi using:
Invoke-WebRequest -Method POST -Uri "https://api.powerbi.com/v1.0/myorg/groups/group_id/imports?datasetDisplayName=$myFile&nameConflict=O..." -Header #{Authorization = '$Token'; 'Content-Length' = $len} -Body '{ "fileUrl"= "url"}' | ConvertTo-Json -Depth 5
I am getting the following error:
Invoke-WebRequest : You must write ContentLength bytes to the request stream before calling [Begin]GetResponse.
What am I doing wrong?
I added the content-length because I was getting this error:
{"error":{"code":"InvalidFileSizeError","pbi.error":{"code":"InvalidFileSizeError","parameters":{"FileSize":"1839362"},"details":[],"exceptionCulprit":1}}}
According to this page, we had to add content length in request https://community.powerbi.com/t5/Developer/Post-in-Group-API-using-Nodejs/m-p/1054892
I'm using below script to call a RestAPi using powershell:
$cd = 12000
$Url = 'https://exampleSet/Set'
$Body = #{
OperationType = 'new'
Number = "'$($cd)'"
CdAspId = 'Z_CK_BUGFIX'
CdType = 'CA_106_4'
} | ConvertTo-Json
$headers = #{
'ContentType' = 'application/json'
'Accept' = 'application/json'
'APIKey' = 'abcdef'
}
Invoke-RestMethod -contentType "application/json" -Uri $url -Method Post -body $Body -Headers $headers
While execution, powershell task in Azure is showing me internal server error.
I need help in passing the value in the body as:
Number = '12000'
How can I get the values in body in single quotes. Please help in this.
As mentioned in the comments, JSON exclusively uses double-quoted string literals, so trying to create a JSON document with a property entry Number = '12000' doesn't make any sense - it wouldn't be valid JSON.
To force string encoding rather than a numerical value, simply surround the value with double-quotes in PowerShell:
$Body = #{
OperationType = 'new'
Number = "$cd"
CdAspId = 'Z_CK_BUGFIX'
CdType = 'CA_106_4'
} | ConvertTo-Json
I want to know how to create several repos with their respective branches, but the branches created do not respect the hierarchy example:
DEV based on PRD(master)
FTR based on DEV
DEV-based REL
EXCUSE THE ENGLISH, USE A TRANSLATOR
$repository = "ECOMP_CORE_LG"
$newBranch = "REL/ECOMP_CORE_PDF/ECOMP_CORE_PDF-4080-re"
$baseBranch = "DEV/ECOMP_CORE_PDF/ECOMP_CORE_PDF-dev"
$organization = "XXXXXXX"
$project = "XXXXXX"
$pat = "TOKEN"
$base64AuthInfo =
[System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$pat"))
$headers = #{ Authorization = "Basic $base64AuthInfo" }
# Get ID of the base branch
$url = "https://dev.azure.com/$organization/$project/_apis/git/repositories/$repository/refs?
filter=heads/$baseBranch&api-version=5.1"
$baseBranchResponse = Invoke-RestMethod -Uri $url -ContentType "application/json" -headers
$headers -Method GET
# Create a new branch
$url = "https://dev.azure.com/$organization/$project/_apis/git/repositories/$repository/refs?
api-version=5.1"
$body = ConvertTo-Json #(
#{
name = "refs/heads/$newBranch"
newObjectId = $baseBranchResponse.value.objectId
oldObjectId = "0000000000000000000000000000000000000000"
})
$response = Invoke-RestMethod -Uri $url -ContentType "application/json" -Body $body -headers
$headers -Method POST
I am using this invoke-restmethod so I can get a token so I can do some sql work. the variables come from Azure Key Vault. I have tried to write the variables as
$($SPNAppid)
$SPNAppid
${$SPNAppid} etc
Here is the code :
$request = Invoke-RestMethod -Method POST -Uri
"https://login.microsoftonline.com/${$TenantId}"/oauth2/token" -Body
#{ resource="https://database.windows.net/";
grant_type="client_credentials"; client_id=${$SPNAppid};
client_secret=${$SPNValue} } -ContentType
"application/x-www-form-urlencoded"
Getting this error below. What is the best way to do this - whatever i do i am getting the errors below.
Variable reference is not valid. ':' was not followed by a valid variable name character. Consider using ${} to
delimit the name.
At C:\agent01_2\_work\_temp\b3f54d23-b7b6-4cc3-96ec-8b4b534be571.ps1:20 char:319
+ ... ervicePrincipalKey } -ContentType "application/x-www-form-urlencoded"
+ ~
The string is missing the terminator: ".
The code you posted, has an extra " and given the ambiguity of the long line I would suggest to use splatting like this :
$header = #{
"Content-type" = "application/x-www-form-urlencoded"
"Authorization" = "Bearer $token"
}
$body = #{
resource = "https://database.windows.net/"
grant_type = "client_credentials"
client_id = $SPNAppid
client_secret = $SPNValue
}
$params = #{
Method = 'Post'
Uri = "https://login.microsoftonline.com/$($TenantId)/oauth2/token"
Body = $body
ContentType = $header
}
$request = Invoke-RestMethod #params
I do not think the API call would work this way, usually clientId etc. are part of the URL, you can read more about it here - https://learn.microsoft.com/en-us/azure/active-directory/develop/v2-oauth2-auth-code-flow
I have a code in python and I would like to do the same in powershell.
This code works to make inquiries in the phpipam api.
#!/usr/bin/env python3.6
import requests
import json
import pandas as pd
server = "http://phpipam.local"
appid = "nuevoid"
username = "admin"
password = "password"
baseurl = server + "/api/" + appid
res = requests.post(baseurl + '/user/', auth=(username, password))
token = json.loads(res.content)['data']['token']
res = requests.get(baseurl + '/addresses/search/192.168.10.10', headers={'token': token})
dip = json.loads(res.content)['data']
df = pd.DataFrame(dip)
print(df[['ip','hostname','custom_dns_name','editDate','is_gateway','owner']])
Invoke-WebRequest
$url = "http://phpipam.local/api/nuevoid/addresses/search/192.168.10.10"
$bearer_token = "asdasdasdZZZZZZZZZZZZZZ"
$headers = #{token = "$bearer_token"}
#$response = Invoke-RestMethod -Uri $url -Headers $headers -UseBasicParsing
$headers=#{ Authorization = "Bearer $bearer_token";"Content-Type"="text/xml" }
Invoke-WebRequest -Headers $headers -Uri $url -Method GET
I don't know how I could do the same type of authorization in powershell. In Python Works OK!
The error in the query is: {"code":401,"success":false,"message":"Unauthorized","time":0.002} –