Karen Schaefer
asked on
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:
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
}
Do not use .bat when using Powershell. You have to use .ps1.
Yeah the point is that "bat" is different than "powershell" you're using both in a single script like if it were the same.
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)
(arguments: -ExecutionPolicy RemoteSigned -File C:\script.ps1)
And your script has to contain only powershell commands.
ASKER
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
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
You can use one .ps1 for multiple operation.
ASKER
"And your script has to contain only powershell commands. "
What do I need to correct in my current script?
Karen
What do I need to correct in my current script?
Karen
ASKER
My attempt to run this within the PowerShell window I go the following error message:
File \\abc\data\Metrics_Data_Repository\F olderSaver .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
Run PS as admin:
-ExecutionPolicy RemoteSigned -File C:\script.ps1
-ExecutionPolicy RemoteSigned -File C:\script.ps1
My solution would be:
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".
#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
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".
Nice step-by-step is HERE.
... with screenshots.
... with screenshots.
ASKER
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:
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
}
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)
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.
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.
ASKER
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?
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)
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Jose,
Thanks the revised code works great, How do I save the file for PowerShell.ISE?
Karen
Thanks the revised code works great, How do I save the file for PowerShell.ISE?
Karen
Please mark all helpful posts and close the question.
AS you save any other file on your computer,
File/Save/name.ps1
Click save.
Or
File/Save As/name.ps1
click save.
File/Save/name.ps1
Click save.
Or
File/Save As/name.ps1
click save.
ASKER
How do I modify the schedule to handle the security issue for local user?
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
When running the task, use the following user account
ASKER
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
ASKER
Unfortunately, I do not have admin rights to my machine.
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.
It's a Question => Answer's page.
ASKER
Thanks for your time and efforts.