Timeout error when running Invoke-RestMethod - asp.net-web-api

I'm running the Invoke-RestMethod on PowerShell to invoke a Web API method. It works but after a minute or so, I get a timeout error. I'd really like for PowerShell to wait until the invoked method has completed.
How do I do this?
Thanks for any help.
John
PS C:\Test\TestScripts> .\Run_Automation 5.5.4.382.1
VERBOSE: GET http://server/api/Automation/GetAutomation?testName=5.5.4.382.1 with 0-byte payload
Invoke-RestMethod : The operation has timed out.
At C:\TeamCity\TeamCityScripts\Run_Automation.ps1:20 char:5
+ Invoke-RestMethod -Uri http://corloclaf2/api/Automation/GetAutomation?testNa ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebException
+ FullyQualifiedErrorId :WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand

According to Technet documentation for the Invoke-RestMethod cmdlet, there is a time-out argument you can append to your call. It is supposed to default to indefinite but there is a user complaining that it defaults to 100 seconds.
I can't see all of your code but, to specify a time-out of 2 minutes, your call should look like this:
Invoke-RestMethod -Uri "http://corloclaf2/api/Automation/GetAutomation?test" -TimeoutSec 120

Related

How to use Powershell to download and pipe to execution with arguments

I am trying to use powershell to download and execute a file with arguments:
. { iwr -useb https://github.com/int0x33/nc.exe/blob/master/nc64.exe?raw=true } | iex; <IP> 9001
I get this error:
Unexpected token '9001' in expression or statement.
+ CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : UnexpectedToken
Any help appreciated.
Invoke-Expression (ie) is for interpreting and executing text as PowerShell code[1] - you can't use it to execute a binary download directly (which PowerShell fundamentally doesn't support).
Instead, use Invoke-WebRequest's (iwr's) -OutFile parameter to download the binary content to a local file and execute the latter:
iwr -useb https://github.com/int0x33/nc.exe/blob/master/nc64.exe?raw=true -OutFile ./nc64.exe
./nc64.exe $someIp 9001
[1] The obligatory warning: Invoke-Expression (iex) should generally be avoided and used only as a last resort, due to its inherent security risks. Superior alternatives are usually available. If there truly is no alternative, only ever use it on input you either provided yourself or fully trust - see this answer.

The Win32 internal error "A device attached to the system is not functioning" 0x1F occurred while setting 2 the console window title

I have a PowerShell script that get a response from an API:
$test = Invoke-RestMethod -Uri $url -Method Get -ContentType application/json -UseDefaultCredentials
Then, iterate the response with foreach:
$test.value | foreach {
# Do something
}
In my PC it works great, but I want it will run automatically so I created a TFS build definition and it runs in our build servers.
During the build I get the following error in the middle of the foreach:
foreach : The Win32 internal error "A device attached to the system is not functioning" 0x1F occurred while setting
the console window title. Contact Microsoft Customer Support Services.
At D:\AGT\Test\01\_temp\test.ps1:9 char:17
+ $builds.value | foreach {
+ ~~~~~~~~~
+ CategoryInfo : ResourceUnavailable: (:) [ForEach-Object], HostException
+ FullyQualifiedErrorId : SetConsoleWindowTitle,Microsoft.PowerShell.Commands.ForEachObjectCommand
If I have log-in to the server and run the script, it works. only during the TFS build it doesn't work.
I googled it a lot but couldn't solve it. any idea?

Unable to curl from Windows 10 Powershell with X-API-KEY

I have been trying to curl from windows powershell but no luck.
curl -O -J -H #{'X-Api-Key' = 'abcdefghijk'} https://my.repo.com/abc/xyz
I am getting the following error
curl : The underlying connection was closed: An unexpected error occurred on a send.
At line:1 char:1
+ curl -O -J -H #{'X-Api-Key' = 'abcdefgh'} ht ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-WebRequest], WebExc
eption
+ FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand
As #Robin pointed out in his comment; curl is an alias for Invoke-WebRequest (see your exception details for more information!
So basically... it's not curl!
This means that the switches you are providing might not map as you'd expect.
First thing to mention is that whilst PowerShell allows you to use shortened parameter names, you really, really shouldn't use them!
-O = -OutFile (probably not what you want...)
-J = ...there is none!
-H = -Headers (looks right for you)
I reckon that's the root cause of your issues - you need to look at the documentation for the CmdLet
Get-Help Invoke-WebRequest
Get-Help Invoke-WebRequest -Online ## this will open in browser with full details

GetScheduled-Job doesn't return any job

I'm trying to use PowerShell to add a trigger to an existing scheduled task.
I'm using Windows 10 & PowerShell 5
When I run:
Get-Scheduled-Job -Name TASK_NAME
I receive the error:
>Get-ScheduledJob : A scheduled job definition with Name sanityInstaller could not be found.
At line:1 char:1
+ Get-ScheduledJob sanityInstaller
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (:) [Get-ScheduledJob], RuntimeException
+ FullyQualifiedErrorId : ScheduledJobDefinitionNotFoundByName,Microsoft.PowerShell.ScheduledJob.GetScheduledJobCommand
It seems that even when I run the same command with no parameter it's expected to return all jobs but it returns an empty result.
In the Task Scheduler there's no such folder as /Microsoft/Windows/PowerShell/ScheduledJobs and even after I created it and a new task inside it doesn't return it.
What am I missing here ?
Get-ScheduledJob gets only scheduled jobs that are created by the
current user using the Register-ScheduledJob cmdlet.
Source.
You are looking for the Get-ScheduledTask cmdlet.

PowerShell ISE vs Script

I am playing around powershell the idea is simple:
I want to verify if certain TCP port is open.
Now, I can run this as PowerShell script or I can run it in ISE.
Now, in ISE everything is fine, the script runs as supposed to.
When I run it as PowerShell Script however, I am getting error message:
Method invocation failed because [System.Net.Sockets.TcpClient] does not contain a method named 'ReceiveTimeout'.
At P:\checkTCP80.ps1:7 char:1
+ $tcpClient.ReceiveTimeout(5)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : MethodNotFound
Code:
$servery = gc .\servers.txt
foreach ($server in $servery)
{
$tcpClient = New-Object System.Net.Sockets.TCPClient
$tcpClient.ReceiveTimeout(5)
$tcpClient.Connect($server,80)
Write-Host ($server, $tcpClient.Connected)
}
I have 2 questions:
How come, that the output parameter works just fine from ISE but does not work when this is launched as a script?
How to fix it?
According to the MS documentation on this class ReceiveTimeout is a property and not a method.
Try changing $tcpClient.ReceiveTimeout(5) to $tcpClient.ReceiveTimeout = 5

Resources