Home > OS >  python\powershell - How to execute powershell multiple line script as a single command?
python\powershell - How to execute powershell multiple line script as a single command?

Time:10-10

I'm trying to execute a powershell script, trying to avoid creating a .ps1 file, I want to run it in a single command, I'm trying in this way:

import os

command='''$jobname = "Recurring PowerShell Task"
$script =  "w32tm /resync"
$action = New-ScheduledTaskAction –Execute "$pshome\powershell.exe" -Argument  "$script"
$duration = ([timeSpan]::maxvalue)
$repeat = (New-TimeSpan -hours 3)
$trigger =New-ScheduledTaskTrigger -Once -At (Get-Date).Date -RepetitionInterval $repeat -RepetitionDuration $duration

 
$msg = "Enter the username and password that will run the task"; 
$credential = $Host.UI.PromptForCredential("Task username and password",$msg,"$env:userdomain\$env:username",$env:userdomain)
$username = $credential.UserName
$password = $credential.GetNetworkCredential().Password
$settings = New-ScheduledTaskSettingsSet -AllowStartIfOnBatteries -DontStopIfGoingOnBatteries -StartWhenAvailable -RunOnlyIfNetworkAvailable -DontStopOnIdleEnd
 Register-ScheduledTask -TaskName $jobname -Action $action -Trigger $trigger -RunLevel Highest -User $username -Password $password -Settings $settings'''

os.system("powershell " command)

but I get error:

  $jobname = Recurring PowerShell Task
             ~~~~~~~~~
      CategoryInfo          : ObjectNotFound: (Recurring:String) [], CommandNotFoundException
      FullyQualifiedErrorId : CommandNotFoundException

do you have any advice on how to achieve this? Thank you

CodePudding user response:

command = '''$jobname = "Recurring PowerShell Task"
$script =  "w32tm /resync"
$action = New-ScheduledTaskAction –Execute "$pshome\powershell.exe" -Argument  "$script"
$duration = ([timeSpan]::maxvalue)
$repeat = (New-TimeSpan -hours 3)
$trigger =New-ScheduledTaskTrigger -Once -At (Get-Date).Date -RepetitionInterval $repeat -RepetitionDuration $duration


$msg = "Enter the username and password that will run the task"; 
$credential = $Host.UI.PromptForCredential("Task username and password",$msg,"$env:userdomain\$env:username",$env:userdomain)
$username = $credential.UserName
$password = $credential.GetNetworkCredential().Password
$settings = New-ScheduledTaskSettingsSet -AllowStartIfOnBatteries -DontStopIfGoingOnBatteries -StartWhenAvailable -RunOnlyIfNetworkAvailable -DontStopOnIdleEnd
 Register-ScheduledTask -TaskName $jobname -Action $action -Trigger $trigger -RunLevel Highest -User $username -Password $password -Settings $settings'''
import subprocess
subprocess.call(["powershell","/C",command])
# os.system("powershell -EncodedCommand "   b64_command)

this uses subprocess.call with vectorized arguments (as a list) ... this guarantees that the 3rd argument will be complete ... when you call it normally it messes up on spaces

I struggle to understand why you would prefer this over making a ps1 command script though...

CodePudding user response:

Is there a reason you want to avoid creating a .ps1 file?

There’s many different ways you can run this all.

  1. You can use a code editor like ISE or VSCode where you can type everything out and then click the run button

  2. You copy and paste the code into the console window.

  3. In the console you can use invoke-command -ScriptBlock {<your code here>} https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_script_blocks?view=powershell-7.2

  • Related