0

I have a powershell script file called test.ps1 written as follows:

in the ps1 file, contents are:

& 'C:\Invoke-Parallel.ps1'

$Sciptblock = {.....}

Invoke-Parallel -scriptblock $Scriptblock -inputobject $(get-content "C:\info.txt") -runspaceTimeout 300 -throttle 30 -NoCloseonTimeOut -quiet

I have a problem running this in windows command prompt because '&' is not invoking the script that I am using (Invoke-Parallel.ps1) for the new script that I created.

I could get this to work when the script runs inside Powershell window or even in Powershell ISE but not in the command line.

Please let me know what command line I need to use to run this successfully or it is not possible to do this in windows commandline. Does anyone have any idea how to fix this? Thanks.

Error I got when I ran this:

The term 'Invoke-Parallel' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. At C:\test.ps1:9 char:16 + Invoke-Parallel <<<< -scriptblock $Scriptblock -inputobject $(get-content "C:\info.txt") -runspaceTimeout 300 -throttle 30 -NoCloseonTimeOut -quiet + CategoryInfo : ObjectNotFound: (Invoke-Parallel:String) [], Com mandNotFoundException + FullyQualifiedErrorId : CommandNotFoundException

8
  • Dont you want to Import-Module C:\Invoke-Parallel.ps1? Commented Mar 24, 2016 at 8:34
  • If I use Import-module, won't I have to do more than just convert ps1 extension to psm file? Commented Mar 24, 2016 at 9:21
  • no. you can even import a ps1 afaik Commented Mar 24, 2016 at 9:22
  • The motivation to have this work in Windows Command prompt is so that I can use Windows Task Scheduler to run the powershell script at regular time of the day. Commented Mar 24, 2016 at 9:23
  • jisaak, I tried but it says: Import-Module : The specified module 'invoke-parallel.ps1' was not loaded because no valid module file was found in any module directory. At line:1 char:14 + import-module <<<< invoke-parallel.ps1 + CategoryInfo : ResourceUnavailable: (invoke-parallel.ps1:String) [Import-Module], FileNotFoundException + FullyQualifiedErrorId : Modules_ModuleNotFound,Microsoft.PowerShell.Commands.ImportModuleCommand Commented Mar 24, 2016 at 9:27

1 Answer 1

0

based on the comments above I think your looking to make a batch file that you then call via task scheduler that then launches your PowerShell script

Example Batch file to launch a script:
@echo off powershell -command "start-process cmd -argumentlist /C,powershell,'-ExecutionPolicy RemoteSigned',%~dp0\<your script name>.ps1 -verb RunAs -WindowStyle Hidden"
How this works and what it does: echo off is just so you don't see the command prompt code when it is launched next starts a Windows Command Prompt session which launches PowerShell (/C just indicates to cmd that after its done to terminate and exit its session)
set excutionpolicy is there in case this is on machine where remotesigned hasn't been done and because its only for this instance it will revert back to default after
-verb RunAs is run in admin mode
windowstyle hidden is so that the whole thing happens in the background remove this if this is not wanted
%~dp0 is code for cmd to use its own location for the path so if you use this make sure the batch file is always in the same folder as your script

I should add to make a batch file create a txt file write in the code and save as and change the extension to bat instead of txt then use Task Scheduler to launch the batch file to accomplish your goal

Sign up to request clarification or add additional context in comments.

Comments

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.