0

I am trying to run a TaskScheduler task on a Virtual Machine. The issue is, when I set the Security Options to "Run whether user is logged on or not," with the highest priviledges (Selected user account is an Domain Admin), the task fails. My PowerShell script writes a temporary XLSX file into a folder in the C: and promptly removes it after it is uploaded to a FTP site. I am getting permission issues where the remote computer cannot save files into the C:.

I spent hours Googling this issue and so far I've found no solid answers.

My TaskScheduler task is located within an admin account in the Virtual Machine. It does not fail when the security option is set to "Run only when user is logged on."

Does anyone have any ideas?


I attempted to remote in by enabling PSRemoting and using a separate machine with Admin credentials. Even then I am unable to create any excel files using the workbook.saveas() function.


I verbosed the messages I am receiving from my script.

Debug Log: 02/06/2015 14:31:19
C:\Users\SQLADMIN\Desktop
Exception calling "InvokeScript" with "1" argument(s): "Unable to save to 
C:\Users\SQLADMIN\Desktop\FTP_MYS_Upload\ExhibitorCompaniesDetails.xlsx. 
Please ensure you have write access."
At C:\Users\SQLADMIN\Desktop\FTP_MYS_Upload\Export-XLSX with Excel.ps1:168 
char:13
+             $ExecutionContext.InvokeCommand.InvokeScript($Script)
+             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : RuntimeException

Exception calling "InvokeScript" with "1" argument(s): "Unable to save to 
C:\Users\SQLADMIN\Desktop\FTP_MYS_Upload\ExhibitorCompaniesExpoHistory.xlsx. 
Please ensure you have write access."
At C:\Users\SQLADMIN\Desktop\FTP_MYS_Upload\Export-XLSX with Excel.ps1:168 
char:13
+             $ExecutionContext.InvokeCommand.InvokeScript($Script)
+             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : RuntimeException

The "Unable to save to DIR. Please ensure you have write access" is a try catch for the .saveas() method in my create XLSX script.

I can out-file remotely, essentially creating a txt document but I cannot use the .saveas() method. Any ideas? Does it have to do with Excel/Powershell?


Looks like its a dotNet issue. dotNet does not play nicely when it calls Excel remotely. I'll investigate further.

AJY
  • 188
  • 2
  • 12
  • You can't save to anywhere on c: or just the root? Is user also local admin? – campbell.rw Feb 02 '15 at 15:43
  • I can't save to anywhere on C: The user is also a Domain Admin. – AJY Feb 02 '15 at 18:02
  • So far all my research has pointed towards the "Double Hop" issue. However, this deals with multiple machines, meanwhile I'm trying to set this automated task as a service to the same machine (in this case a VM). – AJY Feb 02 '15 at 19:01
  • When you created and saved the task, were you prompted to enter the credential for that user? The admin creds I mean. – campbell.rw Feb 03 '15 at 13:28
  • Yes, of course. I don't think this is an issue with taskscheduler but with powershell/windows permissions itself. – AJY Feb 03 '15 at 18:51
  • Funny thing is, I can out-file but I cannot .saveas(). Not sure why, since I am new to powershell. – AJY Feb 03 '15 at 19:20
  • For the person who thumbs down this topic, care to provide an explanation? – AJY Feb 06 '15 at 19:38
  • Perhaps the SaveAs is dependent on a users temp directory. Which would not be present in your case. – Matt Feb 08 '15 at 01:15
  • I think .SaveAs is part of .Net. I don't think that's the issue. Maybe it has something to do with dotnet and permissions? – AJY Feb 09 '15 at 01:39
  • Sounds like a question for http://serverfault.com/. – Martin Brown Feb 12 '15 at 14:11

4 Answers4

1

After two months of trying to figure out what the problem was.... I found an article on TechDecode.com...

http://www.techdecode.com/wp/?p=58

For the sake of continuity I will copy the text into here.

All I had to do was grant myself permissions to: C:\Windows\SysWOW64\config\systemprofile\Desktop

I hope this helps the next person.

Gotcha! – Office 2007 Automation with Powershell in Windows 2008

It seems MS has changed the game again on Office automations with Office 2007 and Windows 2008 and/or Windows 7 and Powershell scripts. I do quite a bit of Powershell scripting and found that after upgrading my script host to 2008 R2, my scripts scheduled in Task Manager no longer worked. I pulled out my hair for a few days trying all kinds of different things – changing paths, permissions, syntax – you name it, I tried it. I kept getting this error message –

Exception calling “SaveAs” with “1” argument(s): “SaveAs method of Workbook class failed”

After Googling for the hundreth time or so, I finally stumbled across this post on the Social MSDN Forums. I could not believe it that when I simply added a folder to the server, it all worked! This is the folder you need to add (Thank you H Ogawa for your posting!) – ・Windows 2008 Server x64 Please make this folder. C:\Windows\SysWOW64\config\systemprofile\Desktop

・Windows 2008 Server x86 Please make this folder. C:\Windows\System32\config\systemprofile\Desktop

Frustrating, but fixed!

AJY
  • 188
  • 2
  • 12
0

I would try an workaround and make system thing that is saves file on remote PC by not saving file directly to c:\ but to \\PCname\c$ if it is cross domain environment would use computer`s \\FQDN\c$

Ja Som
  • 23
  • 6
0

Have you tried to add the Selected User Account to the local Users group ? (Go to computer Management, Local Users and Groups, Groups, Users and add the account in this group). Depending on your security settings, you might also need to add him to the Local Administrators group (but I doubt it is necessary to simply create a file).

Superzadeh
  • 1,106
  • 7
  • 23
0

Ensure the service "Secondary Logon" is running.

Ensure your "log on as batch" local security policy is correct: https://social.technet.microsoft.com/Forums/windowsserver/en-US/760ee186-8074-40a5-95b0-35d17c2bdfab/log-on-as-batch-job-right

If all that is working, test the script as the user by doing the following:

runas /user:yourdomain\yourusername cmd.exe
Eric Longstreet
  • 783
  • 1
  • 9
  • 23