2

I have a Powershell script that invokes a REST API call to get a cert from a server. The script runs just fine if I trigger it manually from the machine. However, when I setup the script to run at Start using the steps here: http://blogs.technet.com/b/heyscriptingguy/archive/2014/05/14/use-powershell-to-create-job-that-runs-at-startup.aspx, then the script does not run at all.

I setup a .bat scripts to under the same StartUp folder (as mentioned in the article above) and they run just fine.

Any pointers or suggestions to help resolve or debug the issue will be helpful.

EDIT: Here is the script

# Fake the Certificate Validation as we are using a HTTPS request without a real cert
 Add-Type @"
     using System.Net;
    using System.Security.Cryptography.X509Certificates;
     public class TrustAllCertsPolicy : ICertificatePolicy {
         public bool CheckValidationResult(
             ServicePoint srvPoint, X509Certificate certificate,
             WebRequest request, int certificateProblem) {
             return true;
         }
     }
"@
[System.Net.ServicePointManager]::CertificatePolicy = New-Object TrustAllCertsPolicy

$uri = "https://10.X.Y.Z/webapp-rest/login?&user=user1&pw=super_secure"
$res = Invoke-WebRequest -Uri $uri -Method Post

$headers = @{}
$headers.Add('Authorization', $res.Headers.Authorization)


$uri1 = "https://10.X.Y.Z/webapp-rest/certificate"   
$sm = Invoke-RestMethod -Uri $uri1 -Headers $headers -ContentType application/x-x509-ca-cert -OutFile "cert.pem"

Move-Item C:\cert.pem C:\Users\Administrator\Downloads -Force
Eris
  • 7,378
  • 1
  • 30
  • 45
user2611581
  • 166
  • 2
  • 15

1 Answers1

1

I completely forgot about this question once I found a solution to the issue. Putting out a response/answer - in case someone lands on this question or faces a similar issue.

Windows 7 has this real pain in the *** setting when setting up Scheduled Task to run at Startup. The third tab Action has three fields under the Settings section : Program/script, Add arguments(optional) and Start in(optional).

The 'Start in(optional)' field is NOT optional.

I specified the path to the script under the Program/script field (for e.g. - C:\Test\test.bat) and then set the 'Start in(optional)' field to 'C:\'.

Everything worked like a charm after.

user2611581
  • 166
  • 2
  • 15