Task Scheduler/Powershell .bat file

I am trying to schedule a few file/directory backups using the Windows 10 Task Scheduler.  I create a .bat files, and create a scheduled task based on the this .bat file.  When I run the task the file(s) do not get created and moved to the correct file directory.  Is there a was to step thru the PowerShell bat file.  I Have never used this method before.

Here is my current .bat file:
@Echo Off
Zip: zip "\\abc\data\Metrics_Data_Repository\abc_Queries" "\\abc\data\gdra\Backup_Code_Repository"

$source = "\\abc\data\Metrics_Data_Repository"
$destination = "\\abc\data\GDRA\Backup_Code_Repository"

Get-ChildItem $source -Recurse -Include *.zip | % {
    $name = "abc_Queries_" + ($_.CreationTime | Get-Date -Format yyyymmdd) + "_" + ($_.CreationTime | Get-Date  -Format hhmmss) + ".zip"
    Rename-Item $_ -NewName $name
    Move-Item "$($_.Directory)\$name" -Destination $destination
}

Open in new window

Karen SchaeferBI ANALYSTAsked:
Who is Participating?
 
Jose Gabriel Ortega CEE Solution Guide - CEO Faru Bonon ITCommented:
You are trying to do in 2 steps what you can do in 1. So you don't need to extract the "creation Time of the zip file" to get the Actual Time, you can use directly the GetDate with the format that you would like. (in date time terms).
 
Just Change the "destination line for":
$destination = "\\abc\data\GDRA\Backup_Code_Repository\abc_Queries_$(Get-Date -Format yyyymmdd-hhmmss).zip"

Open in new window

And remove the "Get-Childitem part" if the goal of that block is just to rename zip files.
The result would be:
\\abc\data\GDRA\Backup_Code_Repository\abc_Queries_20182322-022329.zip
0
 
Hello ThereSystem AdministratorCommented:
Do not use .bat when using Powershell. You have to use .ps1.
0
 
Jose Gabriel Ortega CEE Solution Guide - CEO Faru Bonon ITCommented:
Yeah the point is that "bat" is different than "powershell" you're using both in a single script like if it were the same.
0
On-Demand: Securing Your Wi-Fi for Summer Travel

Traveling this summer?Check out our on-demand webinar to learn about the importance of Wi-Fi security and 3 easy measures you can start taking immediately to protect your private data while using public Wi-Fi. Follow us today to learn more!

 
Hello ThereSystem AdministratorCommented:
Just create a new task that will run Powershell.exe under admin account and then in Arguments field write the path to your script.
(arguments:  -ExecutionPolicy RemoteSigned -File C:\script.ps1)
0
 
Hello ThereSystem AdministratorCommented:
And your script has to contain only powershell commands.
0
 
Karen SchaeferBI ANALYSTAuthor Commented:
Can I run multiple task within the same .ps1 file?  

I have a few directories that need to be zipped and moved to new location.

Do I need to copy the zipped file to new location, or will the zip file suffice?

Thanks,
Karen
0
 
Hello ThereSystem AdministratorCommented:
You can use one .ps1 for multiple operation.
0
 
Karen SchaeferBI ANALYSTAuthor Commented:
"And your script has to contain only powershell commands. "

What do I need to correct in my current  script?

Karen
0
 
Karen SchaeferBI ANALYSTAuthor Commented:
My attempt to run this within the PowerShell window I go the following error message:

File \\abc\data\Metrics_Data_Repository\FolderSaver.ps1 cannot be loaded because running scripts is disabled on this system. For more information,
see about_Execution_Policies at http://go.microsoft.com/fwlink/?LinkID=135170.
    + CategoryInfo          : SecurityError: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : UnauthorizedAccess
0
 
Hello ThereSystem AdministratorCommented:
Run PS as admin:
-ExecutionPolicy RemoteSigned -File C:\script.ps1
0
 
Jose Gabriel Ortega CEE Solution Guide - CEO Faru Bonon ITCommented:
My solution would be:


#region Compression
$BackupName="Backup.zip"
$ZipSource="\\abc\data\Metrics_Data_Repository\abc_Queries"
$ZipDestination="\\abc\data\gdra\Backup_Code_Repository\$BackupName"

if($ZipSource -ne $ZipDestination){
    If(Test-path $ZipDestination) {Remove-item $ZipDestination}
    Add-Type -assembly System.IO.Compression.filesystem
    [IO.Compression.zipfile]::CreateFromDirectory($ZipSource,$ZipDestination) 
}
else{
    Write-Error "ZipSource var is equals to ZipDestination '$ZipSource'='$ZipDestination'"
    exit
}
#endregion
#region MovePart
$source = "\\abc\data\Metrics_Data_Repository"
$destination = "\\abc\data\GDRA\Backup_Code_Repository"

Get-ChildItem $source -Recurse -Include *.zip | % {
    $name = "abc_Queries_$($_.CreationTime | Get-Date -Format yyyymmdd)_$($_.CreationTime | Get-Date  -Format hhmmss).zip"
    Rename-Item $_ -NewName $name

    Move-Item "$($_.Directory)\\$name" -Destination "$destination"
}
#endregion

Open in new window


This code was done combining this:
https://blogs.technet.microsoft.com/heyscriptingguy/2015/03/09/use-powershell-to-create-zip-archive-of-folder/
with your code.

To make run PowerShell script from Task Scheduler you have my article here:
https://social.technet.microsoft.com/wiki/contents/articles/46768.how-to-run-a-powershell-script-with-parameters.aspx
I assumed that your "movement region is good".
1
 
Hello ThereSystem AdministratorCommented:
Nice step-by-step is HERE.
... with screenshots.
0
 
Karen SchaeferBI ANALYSTAuthor Commented:
Jose,

Thanks for the sample code, however I am still trying to bypass the local user issue, unauthorized:

How do I include this into the code:

param([switch]$Elevated)
function Check-Admin {
$currentUser = New-Object Security.Principal.WindowsPrincipal $([Security.Principal.WindowsIdentity]::GetCurrent())
$currentUser.IsInRole([Security.Principal.WindowsBuiltinRole]::Administrator)
}
if ((Check-Admin) -eq $false)  {
if ($elevated)
{
# could not elevate, quit
}
 
else {
 
Start-Process powershell.exe -Verb RunAs -ArgumentList ('-noprofile -noexit -file "{0}" -elevated' -f ($myinvocation.MyCommand.Definition))
}
exit
}

Open in new window

0
 
Jose Gabriel Ortega CEE Solution Guide - CEO Faru Bonon ITCommented:
You don't need to add that code to the script, just save my script as script.ps1, and then in my article is how to use PowerShell with parameters (in the article)
however I am still trying to bypass the local user issue,
But you just don't need to check the "Run with elevated privileges" in the Task Scheduler, you don't need at all that code.

Also can be an option:
Invoke-Command "Powershell.exe" -RunAsAdministrator -ScriptBlock $sb -AsJob
But will need more things to do. So to keep it simple just use the task scheduler to manage the "Privileges" and administrator part.
0
 
Karen SchaeferBI ANALYSTAuthor Commented:
Jose'

Thanks for the suggestion I go the first part of the code to work and create the zipped file in the new location.  Now I need to modify the name of the new zip file to include current date & time.  What am I missing?
$source = "\\abc\data\Metrics_Data_Repository\abc_Queries"
$destination = "\\abc\data\GDRA\Backup_Code_Repository\"
 
Get-ChildItem $source -Recurse -Include *.zip | % {
    $name = "abc_Queries_$_.CreationTime | Get-Date -Format yyyymmdd)_$($_.CreationTime | Get-Date  -Format hhmmss).zip"
    Rename-Item $_ -NewName $name
    }


If(Test-path $destination) {Remove-item $destination}
Add-Type -assembly "system.io.compression.filesystem"
[io.compression.zipfile]::CreateFromDirectory($Source, $destination) 

Open in new window

0
 
Karen SchaeferBI ANALYSTAuthor Commented:
Jose,

Thanks the revised code works great, How do I save the file for PowerShell.ISE?

Karen
0
 
Hello ThereSystem AdministratorCommented:
Please mark all helpful posts and close the question.
0
 
Jose Gabriel Ortega CEE Solution Guide - CEO Faru Bonon ITCommented:
AS you save any other file on your computer,
File/Save/name.ps1
Click save.

Or
File/Save As/name.ps1
click save.
0
 
Karen SchaeferBI ANALYSTAuthor Commented:
How do I modify the schedule to handle the security issue for local user?
0
 
Hello ThereSystem AdministratorCommented:
In Task Scheduler... there is an option to run the task as different user - type domain admin credentials.
When running the task, use the following user account
0
 
Karen SchaeferBI ANALYSTAuthor Commented:
What can cause it work 1 time and then run into the security access issue?

PS Microsoft.PowerShell.Core\FileSystem::\\abc\data\Metrics_Data_Repository> \\abc\data\Metrics_Data_Repository\FolderSaver1.ps1
File \\nw\data\Metrics_Data_Repository\FolderSaver1.ps1 cannot be loaded because running scripts is disabled on this system. For more information, see 
about_Execution_Policies at http://go.microsoft.com/fwlink/?LinkID=135170.
    + CategoryInfo          : SecurityError: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : UnauthorizedAccess

Open in new window

0
 
Karen SchaeferBI ANALYSTAuthor Commented:
Unfortunately, I do not have admin rights to my machine.
0
 
Jose Gabriel Ortega CEE Solution Guide - CEO Faru Bonon ITCommented:
The original question was already answered, please use more questions to solve other issues, this is not a forum.
It's a Question => Answer's page.
0
 
Karen SchaeferBI ANALYSTAuthor Commented:
Thanks for your time and efforts.
0
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.

All Courses

From novice to tech pro — start learning today.