Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
385 views
in Technique[技术] by (71.8m points)

windows - How to condense PowerShell script to fit on a single line

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.Application
$wb1 = $app.Workbooks.Open("C:xamppupload_filesLaunchpad.xlsm")
$app.Run("Refresh")
$wb1.Close($false)
$app.Quit()

The pseudo-code would look something like this:

$app = New-Object -comobject Excel.Application AND $wb1 = $app.Workbooks.Open AND "C:xamppupload_filesLaunchpad.xlsm") AND $app.Run("Refresh") AND $wb1.Close($false) AND $app.Quit()

The reason I want to fit on a line is because I would like to insert the arguments directly in the 'arguments' box of Windows Task Scheduler. The reason for this is that for some reason scripts have been disabled (e.g. I cannot call a .ps1 file...)

I know this will still work, as I already have a "one liner" PS script running. What would the syntax look like??

Kind regards, G.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...