0
votes

So i have fairly easy powershell script that contains following:

import-module activedirectory

Get-ADUser -Filter *

remove-module activedirectory

If i run it from powershell it runs OK, but when i try to call it from CMD nothing happens, it just opens powershell and thats it. I am using following command to run it:

powershell.exe -file "D:\test.ps1"

I noticed also following thing, 2 powershell.exe processes run after i execute this. If i exit from CMD from one powershell then i start seeing lists that this PS query should be returning. Is there a way to get this working since i am trying to run ps script as scheduled job. The crucial part here is import module when i run it over cmd which is not happening for some reason.

It's powershell 2.0 running on Windows 2008R2. I tried this script on win 2012r2, works fine from CMD... Looks like ps 2.0 limitation?

1
Seems broke, what happens if your script only contains write-host test - Andy Arismendi

1 Answers

0
votes

Could be a couple of things going on here. Since your windows opens and closes you wont get to see any errors that might be occurring. What is your ExecutionPolicy set to? Get-ExecutionPolicy

When I make scheduled tasks of my scripts I usually set up my action as such

Program/Script = %SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe

Arguments = -ExecutionPolicy Unrestricted -NoProfile -File C:\data\script.ps1

Start In = %SystemRoot%\system32\WindowsPowerShell\v1.0

Also, I don't believe it matters in this case but be sure you have the script "Running with highest privilege" if required.