0

I have a script that runs in Powershell ISE but there is a part of that script that has to run in regular Powershell. The script that needs to run in Powershell has multiple lines.

When I try running the script like this:

<#
Some code runs up here
#>

$script = {
$PW = "Password1";
$PW = $PW | ConvertTo-SecureString -AsPlainText -Force;
Add-SQLAssessmentTask -ManagementGroup "SOME_ID_NUMBER"  -SQLServerName $env:computername -WorkingDirectory C:\Temp\SQL -ScheduledTaskUsername domain\user -ScheduledTaskPassword $PW -Verbose;
}

$command = $script.ToString()

#Start-Process powershell -argumentlist $command

Start-Process powershell -argumentlist $script

I get the follow error: enter image description here

When I run the script like this:

<#
Some code runs up here
#>

$arguments = "$PW = ""Password1""","$PW = $PW | ConvertTo-SecureString -AsPlainText -Force","Add-SQLAssessmentTask -ManagementGroup ""SOME_ID_NUMBER""  -SQLServerName $env:computername -WorkingDirectory C:\Temp\SQL -ScheduledTaskUsername domain\user -ScheduledTaskPassword $PW -Verbose" 

Start-Process powershell -argumentlist $arguments

I get this error: enter image description here

If I run each line in regular Powershell, one at a time, it works fine.

Any suggestions?

1 Answer 1

1

$Arguments is supposed to be a script block separated by semi-colons if you want to run multiple commands.

$arguments = {"$PW = ""Password1""";"$PW = $PW | ConvertTo-SecureString -AsPlainText -Force";"Add-SQLAssessmentTask -ManagementGroup ""SOME_ID_NUMBER""  -SQLServerName $env:computername -WorkingDirectory C:\Temp\SQL -ScheduledTaskUsername domain\user -ScheduledTaskPassword $PW -Verbose" }

Start-Process powershell -argumentlist $arguments
Sign up to request clarification or add additional context in comments.

1 Comment

I had to create additional variables to hold the "SOME_ID_NUMBER" and working directory, but that did work!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.