我有一个 Powershell 脚本,它调用 REST API 调用以从服务器获取证书。如果我从机器手动触发脚本,脚本运行得很好。但是,当我使用此处的步骤设置脚本以在开始时运行时:http: //blogs.technet.com/b/heyscriptingguy/archive/2014/05/14/use-powershell-to-create-job-that- runs-at-startup.aspx,则脚本根本不运行。
我在同一个 StartUp 文件夹下设置了一个 .bat 脚本(如上文所述),它们运行良好。
任何有助于解决或调试问题的指针或建议都会有所帮助。
编辑:这是脚本
# 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