rest - 带有REST API调用的Powershell脚本无法在启动时运行

标签 rest powershell startupscript

我有一个Powershell脚本,该脚本调用REST API调用以从服务器获取证书。如果我从计算机手动触发脚本,脚本运行得很好。但是,当我使用以下步骤将脚本设置为在“开始”时运行时:
http://blogs.technet.com/b/heyscriptingguy/archive/2014/05/14/use-powershell-to-create-job-that-runs-at-startup.aspx,那么脚本根本不会运行。

我将.bat脚本设置为在同一StartUp文件夹下(如上文中所述),它们运行良好。

任何有助于解决或调试问题的指针或建议都会有所帮助。

编辑:这是脚本

# 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

最佳答案

找到问题的解决方案后,我完全忘记了这个问题。提出答案/答案-以防有人落入此问题或面临类似问题。

当设置计划任务以在启动时运行时,Windows 7在***设置中确实遇到了这种麻烦。第三个选项卡的“操作”在“设置”部分下具有三个字段:“程序/脚本”,“添加参数”(可选)和“开始于”(可选)。

“开始于(可选)”字段不是可选的。

我在Program / script字段下指定了脚本的路径(例如-C:\ Test \ test.bat),然后将“Start in(可选)”字段设置为“C:\”。

之后,一切都像魅力一样。

关于rest - 带有REST API调用的Powershell脚本无法在启动时运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31794844/

相关文章:

php - PayPal Rest API - 实时支付失败,沙盒工作

c# - ASP.NET MVC C# PayPal Rest API - UNAUTHORIZED_PAYMENT

powershell - 使用 UFormat 获取 unix 时间

linux - 只有启动脚本的第一行有效

java - NodeJS 和 Java 创建 REST API

Java应用程序返回curl : (23) Failed writing body (0 ! = 8042)

c# - 将自定义对象从 C# 传递到 Powershell

powershell - 运行 PowerShell 时跳转到集成终端底部

.net - 如何注册启动脚本?

node.js - node.js repl 的启动脚本