How to condense PowerShell script to fit on a single line

后端 未结 1 443
陌清茗
陌清茗 2020-12-06 20:31

Quick question. I am trying to write the following PowerShell script, but I would like it to fit on a single line:

$app = New-Object -comobject Excel.Applica         


        
相关标签:
1条回答
  • 2020-12-06 21:12

    Powershell statements can be separated with semicolons:

    $app = New-Object -COM 'Excel.Application'; $wb1 = $app.Workbooks.Open("..."); ...
    

    The PowerShell executable takes a -Command parameter that allows you to specify a command string for execution in PowerShell:

    powershell.exe -Command "stmnt1; stmnt2; ..."
    

    To run this via Task Scheduler you'd put powershell.exe into the "program" field and -Command "stmnt1; stmnt2; ..." into the "arguments" field of the task.

    However, as @alroc said: you should verify why script execution has been restricted. If it's just the default setting you can simply change it by running Set-ExecutionPolicy RemoteSigned or override it by adding -ExecutionPolicy ByPass to a PowerShell command line. However, if the setting is enforced by policy changing/bypassing the setting will fail, and you could get into quite some trouble for violating company policies.

    0 讨论(0)
提交回复
热议问题