简体   繁体   中英

Unable to pass the parameter while creating the scheduled task using powershell. I should get -version as “sql server 2016” instead of “sql”

I am trying to create a scheduled task using powershell.

able to create and execute the task properly

```
$servername ="myservername"
# Issue with the version, unable to pass. Splitting in the scheduler argument.
$version="sql server 2016" 
$edition="enterprise"

$action = New-ScheduledTaskAction -Execute Powershell.exe -Argument "-File G:\ForAPI\MainFunctions_All.ps1 -Servername $servername -Version $version -Edition $edition"

$trigger =  New-ScheduledTaskTrigger -Once -At 12:00PM
Register-ScheduledTask -Action $action -Trigger $trigger -TaskName "MSSQL" -Description "SQL Deployment"
```

I should get -version as "sql server 2016" instead of "sql"

You need to change the parameters as positional and make it in order like 1st servername ,then version followed by edition in the function where you are using it.

You should use single quotes for the parameters inside while the value is being held by them else you will observe partial record(exactly what you are getting now); Change the quotation like this below:

$action = New-ScheduledTaskAction -Execute "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" -Argument "-ExecutionPolicy ByPass -File G:\ForAPI\MainFunctions_All.ps1 '$servername' '$version' '$edition'" 

If the parameters are all positional then you can completely avoid the -Servername , -Version and -Edition unnecessarily.

This will do the trick for you. Hope it helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM