I have this function in a powershell script to deploy a war file to a tomcat server. Similar functions for the other tomcat commands (stop, undeploy, list, etc) work fine. When I run the script from a shell it works. When I run the script from the Jenkins Powershell plugin it fails.
Function DeployTomcatWebApp([PSCredential]$cred, [string]$server, [string]$app)
{
Write-Host "Deploying $app"
$uri = $server+"/manager/text/deploy?path=/$app&update=true"
$warpath = ($pwd).path + "/$app/build/dev/$app.war"
$r = Invoke-WebRequest -Uri $uri -Credential $cred -InFile $warpath -UseBasicParsing -Verbose -Debug -Method PUT -TimeoutSec 30000 -DisableKeepAlive
Write-Host $r.Content
Return $r
}
The Console Log shows
Deploying DERF
D:\Jenkins\workspace\JenkinsTest_2_AD_DERF\DERF\build\dev\DERF.war
http://mytomcat:8080/manager/text/deploy?path=/DERF&update=true
VERBOSE: PUT http://mytomcat:8080/manager/text/deploy?path=/DERF&update=true with -1-byte payload
Invoke-WebRequest : The underlying connection was closed: An unexpected error occurred on a send.
At C:\Users\<jenkinsuser>\AppData\Local\Temp\jenkins1024175502034764284.ps1:49 char:10
+ $r = Invoke-WebRequest -Uri $uri -Credential $cred -InFile $warpa ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-WebRequest], WebExc
eption
+ FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand
The Jenkins Powershell plugin extracts the script from the jenkins job and create the .ps1 file in Local/Temp and executes the script by calling
powershell -NonInteractive -ExecutionPolicy Bypass <tempscriptname>
I am stumped and looking for any insight to debugging this.
Related
I'm trying integrating-teamcity-with-msteams by execute below powershell script from one of the Build Step in Teamcity to hit the Microsoft teams. When I executed the same powershell script from "Postman" its working fine and from teamcity its failing and suggestions to fix the issue.
$body = #{
title= "Message Title";
text= "Message content"
} | ConvertTo-Json -Depth 4
Invoke-WebRequest -Method Post -Uri "http://shiftup.webhook.office.com/webhookb2/b7f49321-8fdb-4a74-8f86-ef7cd7cffe52#d852d5cd-724c-4128-8812-ffa5db3f8507/IncomingWebhook/8f72d2f94a4c4691b303c53be43fa462/f83d02ad-eb2a-4054-8b49-0ec780812870" -Body $body -ContentType "application/json" -UseBasicParsing
Teamcity Error:
Step 1/1: Send Teams Nofitication (PowerShell)
15:02:56 PowerShell Executable: C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe
15:02:56 Working directory: G:\CIQ00\data\agent02\work\bacf322751a460f
15:02:56 Command: C:\Windows\sysnative\WindowsPowerShell\v1.0\powershell.exe
15:02:56 PowerShell arguments: -NoProfile, -NonInteractive, -ExecutionPolicy, ByPass, -File, G:\CIQ00\data\agent02\temp\buildTmp\powershell7256209341500178754.ps1
15:02:56 Invoke-WebRequest : The remote name could not be resolved: 'shiftup.webhook.office.com'
15:02:56 At G:\CIQ00\data\agent02\temp\buildTmp\powershell7256209341500178754.ps1:5 char:1
15:02:56 + Invoke-WebRequest -Method Post -Uri https://shiftup.webhook.office.co ...
15:02:56 + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
15:02:56 + CategoryInfo : NotSpecified: (:) [Invoke-WebRequest], WebException
15:02:56 + FullyQualifiedErrorId : System.Net.WebException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand
Invoke-WebRequest : The remote name could not be resolved: 'shiftup.webhook.office.com'
I am running into an strange issue building a Powershell script. I want to programatically connecto to Microsoft Teams and execute some skype for businesss online commands.
When I simply do on a terminal
Connect-MicrosoftTeam and get prompted and import my credentials, it all works perfectly, but i need to this programatically.
I did an app registration with the following permissions
And I am authenticating against Azure AD and Graph:
#Connect to Graph
$Body = #{
Grant_Type = "client_credentials"
Scope = "https://graph.microsoft.com/.default"
client_Id = $ApplicationID
Client_Secret = $AccessSecret
}
$ConnectGraph = Invoke-RestMethod -Uri "https://login.microsoftonline.com/$TenatDomainName/oauth2/v2.0/token" -Method POST -Body $Body
$token = $ConnectGraph.access_token
#Connect to AD
$uri = "https://login.microsoftonline.com/${tenant_id}/oauth2/token"
$body = #{grant_type='refresh_token';resource='https://graph.windows.net';client_id=$clientId;refresh_token=$refresh_token}
$result = Invoke-RestMethod -Method Post -Uri $uri -Body $body
$accessToken = $result.access_token
Then I run
Connect-MicrosoftTeams -AadAccessToken $accessToken -MsAccessToken $token -AccountId $account
And it doesn't throw an error, works properly. and i gen run commands like Get-Team and returns the info correctly. However when I try to run an skype command like
Get-CsOnlineVoiceUser
I get the error
Get-CsOnlineSession : Run Connect-MicrosoftTeams before running cmdlets.
At C:\Program Files\WindowsPowerShell\Modules\MicrosoftTeams\2.3.1\net472\SfBORemotePowershellModule.psm1:63 char:22
+ $remoteSession = & (Get-CsOnlineSessionCommand)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Get-CsOnlineSession], UnauthorizedAccessException
+ FullyQualifiedErrorId : UnauthorizedAccessException,Microsoft.Teams.ConfigApi.Cmdlets.GetCsOnlineSession
Invoke-Command : Cannot validate argument on parameter 'Session'. The argument is null or empty. Provide an argument that is not null or empty, and then try the command again.
At C:\Program Files\WindowsPowerShell\Modules\MicrosoftTeams\2.3.1\net472\SfBORemotePowershellModule.psm1:10006 char:38
+ ... -Session (Get-PSImplicitRemotingSession -CommandName 'Get-CsOnline ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidData: (:) [Invoke-Command], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.PowerShell.Commands.InvokeCommandCommand
Am I missing something? I am using the 2.3.1 Teams Module.
Thanks!
This is a bit tricky to do, but can be achieved.
I recommend reading this blog post for exact instructions and code examples:
https://paulobrien.co.nz/2020/06/29/s4b-online-powershell-modern-auth/
Should get you started.
I'm unable to download a .exe file from Github. Script works for different sites and downloads files without issues.
https://github.com/ShareX/ShareX/releases/tag/v13.1.0
This is the .exe I'm trying to download:
https://github.com/ShareX/ShareX/releases/download/v13.1.0/ShareX-13.1.0-setup.exe
>$DownloadUrl = "https://github.com/ShareX/ShareX/releases/download/v13.1.0/ShareX-13.1.0-setup.exe"
>$WebResponse = Invoke-WebRequest -Uri "$DownloadUrl" -Method Head
Invoke-WebRequest : The remote server returned an error: (403) Forbidden.
At line:2 char:16
+ $WebResponse = Invoke-WebRequest -Uri "$DownloadUrl" -Method Head
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-WebRequest], WebException
+ FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand
Full script:
[Net.ServicePointManager]::SecurityProtocol = "Tls, Tls11, Tls12, Ssl3"
$DownloadUrl = "https://github.com/ShareX/ShareX/releases/download/v13.1.0/ShareX-13.1.0-setup.exe"
$WebResponse = Invoke-WebRequest -Uri "$DownloadUrl" -Method Head
Write-Output "Downloading $DownloadUrl"
Start-BitsTransfer -Source $WebResponse.BaseResponse.ResponseUri.AbsoluteUri.Replace("%20", " ") -Destination "C:\Users\Pegavo\Desktop\PS\"
You can just simply use Invoke-WebRequest with -OutFile parameter.
Invoke-WebRequest https://github.com/ShareX/ShareX/releases/download/v13.1.0/ShareX-13.1.0-setup.exe -OutFile "ShareX-13.1.0-setup.exe"
This command will download the file from GitHub and store the result to a file in your current directory.
Or, you can use WebClient.
$webClient = New-Object System.Net.WebClient
$webClient.DownloadFile("https://github.com/ShareX/ShareX/releases/download/v13.1.0/ShareX-13.1.0-setup.exe", "E:\your\path\ShareX-13.1.0-setup.exe")
I tested both on my local machine. Both worked.
Moreover, if you want to understand why Start-BitsTransfer won't work, here.
Edit:
You can get the filename automatically like this one, using Split-Path:
$url = "https://github.com/ShareX/ShareX/releases/download/v13.1.0/ShareX-13.1.0-setup.exe"
$file= Split-Path $url -Leaf #file is ShareX-13.1.0-setup.exe now
Invoke-WebRequest $url -OutFile $file
Is there a reason for using HEAD? GET seems to work
I have a script that calls an internal API. It gets passed a token and a json body.
When I hard code the parameters (token and json_body) in ISE, it runs great. As soon as I run from the command line, it bombs. I get an error "Can't process argument because the argument "name" is not valid."
Just a note. The param $json_body can be very large.
Here is my script. Super simple and works as expected
PARAM($json_body, $token)
$json_body =
'{
"field1" : "1",
"field2" : "2",
"data" : [{<json data here>}]
}'
$header = #{'Token' = '1234567890* '}
Invoke-WebRequest -Uri <uri-here> -Method POST -ContentType "application/json" -Headers $header -Body $json_body
Here's the command I'm using
powershell.exe -executionpolicy bypass -inputformat none -file "C:\PowerShell\Call_Rest_API_And_Post.ps1" -token <token_text> -json_body <json_body_text>
I'm passing data from an ETL process to push data to parameters, but if I run this directly from PowerShell (not ISE) I get this error:
Invoke-WebRequest : An unknown error occurred. Please see server logs for details.
At C:\PowerShell\Call_Rest_API_And_Post.ps1:5 char:1
+ Invoke-WebRequest -Uri <uri-here> ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-WebRequest], WebException + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand
Any pointers?
I'm trying to convert this working request done in Cygwin to Powershell:
Cygwin (Working):
curl -k -u curl:Password -X PUT -F "file=$($_)" https://$($appliance)/wse/customblockpage/file/en
Powershell (not working):
Invoke-Webrequest -Uri "https://$($appliance)/wse/customblockpage/file/en" -Method Put -Infile "$homePath\$($_)" -Credential $cred
Here is the error I get:
Invoke-Webrequest : { "Error": "No file part in file", "Result": "Failure" }
At line:1 char:1
+ Invoke-Webrequest https://{IP Address Masked}/wse/customblockpage/file/en ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-WebRequest], WebException
+ FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand
The { "Error": "No file part in file", "Result": "Failure" } is actually the error response from the web server and not a specific PowerShell error message.
In your cURL invocation, you are specifying form data with the -F flag, yet you don't appear to be doing the same in PowerShell.
In PowerShell, you can specify form data using the -Body flag like this:
Invoke-Webrequest -Uri "https://example.com/" -Method Put -Body #{ "file" = "hello.txt" }
If you need to send the actual content of the file, then you can use this as your -Body argument:
-Body #{ "file" = (Get-Content hello.txt) }