[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Powershell Script scheduled task not working when choosing "run whether user is logged on or not"

Posted on 2011-10-21
3
Medium Priority
?
838 Views
Last Modified: 2012-05-12
Hello everyone,

I'm kinda stuck with trying to run a powershell script through a batch as a scheduled task.

I create the task which runs the following batch file:

C:\Windows\SysWOW64\WindowsPowerShell\v1.0\powershell -NoLogo -NonInteractive -NoProfile -ExecutionPolicy Unrestricted -File C:\Scripts\DelDesktopIni.ps1

Open in new window


The powershell file looks like this:
# Get all folders under D:\data and write them into $DataFolder
$DataFolder = Get-ChildItem -Path D:\data | where {$_.psIsContainer -eq $true}

# Set counter $i to 0
$i = 0

#Do-While loop
do {
# Get data from $DataFolder on position $i and write it into $SubDataFolder
$SubDataFolder = $DataFolder[$i]
# Get the desktop.ini files and write the path into $IniFilePath
$IniFilePath = Get-ChildItem -Path D:\data\$SubDataFolder\*.* -Force  -Include desktop.ini

# If the desktop.ini file exists remove it
if ($IniFilePath -ne $null){
# Remove the desktop.ini
Remove-Item $IniFilePath -Force
# Write deleted file into the log file
$IniFilePath | Out-File C:\Scripts\log.txt -Append
}

# Add +1 to counter $i
$i++
}
# Do-While loop until folders are cleaned,
While($i -le $DataFolder.Count)

Open in new window


It all works except "Remove-Item". The Log get's generated (which says that there is a desktop.ini file), the task says 0x0 but the desktop.ini file is still there.

Is there a workaround for this problem?
0
Comment
Question by:HPatzen
  • 2
3 Comments
 
LVL 5

Accepted Solution

by:
neothwin earned 2000 total points
ID: 37009975
remove -NoProfile and try again

rgds,
0
 
LVL 1

Assisted Solution

by:HPatzen
HPatzen earned 0 total points
ID: 37016919
Thank you for you answer.

I got it working now. Removing -NoProfile didn't do the trick but helped me find the solutions.

All I had to do is changing the Scheduled Task so that it now starts the powershell.exe with just the script as parameter. Nothing else.

Cheers.
0
 
LVL 1

Author Closing Comment

by:HPatzen
ID: 37049201
Neothwin helped me finding right solution.
0

Featured Post

NEW Veeam Backup for Microsoft Office 365 1.5

With Office 365, it’s your data and your responsibility to protect it. NEW Veeam Backup for Microsoft Office 365 eliminates the risk of losing access to your Office 365 data.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.
After seeing many questions for JRNL_WRAP_ERROR for replication failure, I thought it would be useful to write this article.
This tutorial will walk an individual through the steps necessary to install and configure the Windows Server Backup Utility. Directly connect an external storage device such as a USB drive, or CD\DVD burner: If the device is a USB drive, ensure i…
This tutorial will walk an individual through setting the global and backup job media overwrite and protection periods in Backup Exec 2012. Log onto the Backup Exec Central Administration Server. Examine the services. If all or most of them are stop…

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question