0

I have a Powershell script to copy .mkv and .mp4 files from Downloads to my Plex server. The script works fine on its own.

I schedule it to run every 30 minutes using Schtasks and it runs fine. Except a PS window appears on each run.

This thread https://stackoverflow.com/a/1802836/4934442 indicates that going to Task Scheduler and setting the option to "Run whether user is logged on or not" will prevent the window from showing.

When I set this option, the Copy-Item in my script fails to execute. It won't copy to my Plex network share and it won't copy to a local folder. (The Remove-Item does execute). I have worked around this issue by using "pscp" to my Plex server, but I would like to find a way for Copy-Item to function for future scripts running in the background.

Note: one comment in the reference thread mentioned a similar problem

The script:

$mypath='C:\Users\rammjet\Downloads\'
$mydest='Z:\a_watch_folder'

$myext='*.mkv','*.mp4'
foreach($ext in $myext)
{
    $mysearch=$mypath + $ext
    $mylist=dir $mysearch -Name
    foreach($myfile in $mylist)
    {
        $myfile=$mypath + $myfile
        $mytest=$myfile + '.part' # in process of download
        if (!(Test-Path $mytest))
        {
           Copy-Item $myfile -Destination $mydest
           Remove-Item $myfile
        }
    }
}
rammjet
  • 15
  • 3
  • So just for clarification, your script will not run even when you are logged on with this option? What is the run command for the script? – Nico Nekoru Nov 19 '20 at 17:47
  • The script runs from a Powershell window. It runs when set to run with Schtasks but a window pops up. Going to Task Scheduler and setting "Run whether user is logged on or not", the script still runs but the line with "Copy-Item" does not execute. Nothing gets copied whether to local directory or network share. But any found files do get deleted. – rammjet Nov 19 '20 at 17:56
  • So the issue you have is how to prevent the PowerShell window from appearing when your script executes. Is that the purpose of the question? – Bill_Stewart Nov 19 '20 at 20:33
  • Is the "do not save password" box checked? Any difference if you use the UNC path instead of the mapped Z-drive? What command line are you using to run the PowerShell script in Task Scheduler? – PMental Nov 19 '20 at 20:35
  • @Bill_Stewart The method I have outlined above successfully runs the script in a hidden window. It was an answer in another question. The problem is that the "Copy-Item" powershell command does not execute. All other commands execute. But yes, if I can find another easy way, that would be good too. – rammjet Nov 19 '20 at 20:59
  • @PMental The save password is checked and I gave the password. As I said, when I set the flag in Scheduled Tasks and tried the copy to a local folder in my user directory and the copy failed. I used the following command to schedule the script and then made the manual change to "run whether user is logged on or not". schtasks.exe /CREATE /SC DAILY /MO 1 /TN 'MoveMKV' /TR 'powershell.exe C:\Users\rammjet\myApps\movemkv.ps1' /ST 07:00 /RI 30 /DU 24:00 – rammjet Nov 19 '20 at 21:02
  • The checkbox is for NOT saving the password. Do not check it, or it won't be able to access your network folder no matter what method you use. If you add `Start-Transcript -Path C:\temp\log.txt` (or any valid path), what does it say in the logs for the failed attempts? For both local and remote network folders. – PMental Nov 19 '20 at 21:06
  • Try reverting that setting and try the WSH script in my answer. (The reason I suggest it is that it avoids drive mapping/authentication issues.) – Bill_Stewart Nov 19 '20 at 21:29

1 Answers1

0

If the question is really "how do I execute a PowerShell script interactively but prevent its console window from appearing?", then you can use a workaround that involves a WSH script. Here's a short WSH script that can do this:

// ExecPS1Hidden.js
// Executes a PowerShell script in a hidden window
// Run this using wscript.exe so there's not a console window

var PowerShell = "%SystemRoot%\\System32\\WindowsPowerShell\\v1.0\\powershell.exe";

var Args = WScript.Arguments;

if ( (Args.Unnamed.Count == 0) || (Args.Named.Item(0) == "/?") ) {
  WScript.Echo('Usage: wscript ExecPS1Hidden.js "ps1scriptfilename"');
  WScript.Quit(0);
}

var FSO = new ActiveXObject("Scripting.FileSystemObject");
var ScriptFilename = Args.Unnamed.Item(0);
if ( ! FSO.FileExists(ScriptFilename) ) {
  WScript.Echo("File not found - " + ScriptFilename);
  WScript.Quit(2);  // ERROR_FILE_NOT_FOUND
}

var WshShell = new ActiveXObject("WScript.Shell");
var ExitCode = WshShell.Run(PowerShell + ' -File "' + ScriptFilename + '"',0,true);
WScript.Quit(ExitCode);

If you use the above script, you would use something like the following in your scheduled task:

Program/script: C:\Windows\System32\wscript.exe

Add arguments (optional): "C:\Scripts\ExecPS1Hidden.js" "C:\Scripts\MyScript.ps1"

Naturally you would replace C:\Scripts with the correct directories, etc.

This workaround executes the above WSH script using wscript.exe, which won't display a console window. The WSH script then invokes powershell.exe and runs your script in a hidden window.

Bill_Stewart
  • 22,916
  • 4
  • 51
  • 62
  • The method in my original question resulted in drive mapping issues. The WSH/JS solution works. – rammjet Nov 19 '20 at 23:41