且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Powershell 脚本计划任务输出到日志文件

更新时间:2022-12-02 18:28:38

脚本有 2 个问题,第一个是我的主要问题:

There were 2 problems with the script and the 1st was my main problem:

  1. 如果它未签名,则默认行为将被任务计划程序跳过(或者至少我没有在事件查看器中看到错误).
  2. 我不得不使用写输出


检查脚本是否使用 签名获取 AuthenticodeSignature.在这里,您将收到 SignatureStatus 作为字符串:


Check if the script is signed with Get-AuthenticodeSignature. Here you are going to receive SignatureStatus as string:

$isSigned = Get-AuthenticodeSignature -FilePath $FilePath | select -ExpandProperty Status

如果它没有签名,你可以通过添加 -ExecutionPolicy Bypass 来启用它的执行,就像在这个 spiceworks 操作方法.为了安全起见,为您自己的脚本执行此操作!

If it is not signed, you can enable the execution of it by adding -ExecutionPolicy Bypass like it is described in this spiceworks how-to. Do this for your own scripts, because of security !

对于写作部分,我使用了 Write-输出外文件:

For the writing part I used Write-Output and Out-File:

(
   ...
   Write-Output "some text here"
   ...
) | Out-File -FilePath "C:\somepath\log.log