Powershell script as well as inline command unable to perform task via Win Task Scheduler

OS: Win 7 Enterprise 32-bit
PS version: 2.0
Task : To copy folder from C:\backup to  a shared drive
When I schedule in Task Scheduler with following params:
Program:
C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe
Arguments : -Command "& 'C:\bkpscripts\Backupscr_1.ps1'"
, it shows "Task run successfully" .. But does not perform intended task
When run from PS ISE,it works well. and also other machines no issues.But this machine is not joined to our domain.
I have ensure that user has admin rights to folder source and also PS script.
And one more thing.. when I try out with -command &{get-process >> c:\bkpscripts\ServiceProcessBios.txt; get-service | where{$_.Status -eq 'Running'} as a sample,it works.
I am trying command line arg route since the same issue happens when I provide the .ps1 file as argument
G RamAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

David SankovskySenior SysAdminCommented:
Few things to take into account,
Powershell is in most cases part of the PATH variable, There's no need to write the entire path, also instead of running the command as an argument, You can do use the -file flag to pass the path to the file.
As to the issue itself, it's most likely the execution policy that stops the script in it's tracks.
So in the arguments field try the following:
-file "path\to\ps1.file" -ExecutionPolicy Unrestricted

Open in new window

Please make sure to include the double quotation marks
0
G RamAuthor Commented:
@David Sankovsky,
   No .. The task shows it has run successfully. But does not perform the task. As earlier,when I run thru' ISE it does the job..
0
David SankovskySenior SysAdminCommented:
Perhaps there's something in the script itself, Could you share the source while leaving out anything that's sensitive (addresses, credentials and etc,)
0
Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

G RamAuthor Commented:
Had taken out core statements and tested with them in a.ps1 file .. .. Only when scheduled it does not work.
 Any way sharing that ...

Copy-Item -Path "C:\DGS\Folders\SQL" -Destination "\\DGSnas2\DGSback\DGS_Backup\" -Recurse -force -Passthru  -ea Stop |Select Name,LastWriteTime,Mode
Start-Sleep -s 2
Rename-Item -path "\\DGSnas2\DGSback\DGS_Backup\SQL" -newName SQLBackup-TEST
0
Jose Gabriel Ortega CEE Solution Guide - CEO Faru Bonon ITCommented:
Here's an example:
https://social.technet.microsoft.com/wiki/contents/articles/38580.configure-to-run-a-powershell-script-into-task-scheduler.aspx

You are running it wrong:
in program/script: C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe
in arguments  "C:\bkpscripts\Backupscr_1.ps1" (With quotes without &. blablabla)
Start in: "if you want to run it from a particular folder for example: "C:\bkpscripts" (with quotes)

change the ps1 to include some error:
try{
Copy-Item -Path "C:\DGS\Folders\SQL" -Destination "\\DGSnas2\DGSback\DGS_Backup\" -Recurse -force -Passthru  -ea Stop |Select Name,LastWriteTime,Mode 
Start-Sleep -s 2
Rename-Item -path "\\DGSnas2\DGSback\DGS_Backup\SQL" -newName SQLBackup-TEST
}
catch{
    Write-Error "There was an error $($_.exception.Message)" | out-file ".\error.txt"
}

Open in new window

0
G RamAuthor Commented:
@Jose Gabriel Ortega C

If I put in Start in which is optional  ,the path within quotes, Task Scheduler complains of  "directory name is invalid", if not  it shows that it has run successfully.But task is not done. I had stripped off try.. catch  to find out the issue,so now  I  tried out logging exceptions, log file is empty..
0
Jose Gabriel Ortega CEE Solution Guide - CEO Faru Bonon ITCommented:
And does the directory exists?

Use it like this:

try{
Copy-Item -Path "C:\DGS\Folders\SQL" -Destination "\\DGSnas2\DGSback\DGS_Backup\" -Recurse -force -Passthru  -ea Stop |Select Name,LastWriteTime,Mode 
Start-Sleep -s 2
Rename-Item -path "\\DGSnas2\DGSback\DGS_Backup\SQL" -newName SQLBackup-TEST
}
catch{
   "There was an error $($_.exception.Message)" | out-file ".\error.txt"
}

Open in new window

0
G RamAuthor Commented:
Hmmm. I get  "There was an error Logon failure: unknown user name or bad password."
0
G RamAuthor Commented:
To me, use has all perms to the folders,source and script containing.. as far as admin has told me ...
0
Jose Gabriel Ortega CEE Solution Guide - CEO Faru Bonon ITCommented:
Well that's your error.

Since the computer is not part of the domain you're not logging the automatic credentials, and you would need to specify them into the task so you can actually make it work. because you are using credentials from a "workgroup admin" in a "domain environment" obviously it will fail.

PS: Actually your user needs to "login" to the computer to run task against any other computer.

Sol1: Set Credentials for domain saved into the tasks and try again.
Sol2: Run it in a computer within the domain.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
G RamAuthor Commented:
Thanks for the assistance ! Glad that you were able to pinpoint our Sysadmin to fix the issue. Root cause was for some reason Credential Manager was not holding the Windows Credentials for the user.Manually added, PS can now run from Task Scheduler
0
Jose Gabriel Ortega CEE Solution Guide - CEO Faru Bonon ITCommented:
Glad to help!.
If you hardcoded the password on the script is a bad practice.
but you can just do something like this:

ConvertTo-SecureString "<PasswordInPlainText>" -AsPlainText -Force | ConvertFrom-SecureString| out-File ".\pwd.txt"

Open in new window


This will encrypt the password and save it into the file ".\Pwd.txt"

then you can just use the password by reading that file and converting it back to securestring.

$pwd=Get-Content ".\pwd.txt" | ConverTo-SecureString
$Creds =New-Object System.Net.NetworkCrededntial("Username",$pwd).

Open in new window

0
G RamAuthor Commented:
No.No . I am referring to Credential Manager in Win 7 Ent.  Control Panel--> Manage your accounts .Remember this machine is not joined to Domain
0
Jose Gabriel Ortega CEE Solution Guide - CEO Faru Bonon ITCommented:
Ok, that works too.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.