SherlockSpreadsheets
SherlockSpreadsheets

Reputation: 2360

PowerShell .ps1 script file is deleted when run from CMD

When I execute a powershell script file from Command Prompt Window, the file just disappears. The CMD window does not show any error messages, it just goes to the next line immediately after executing the command. I restore the file from a backup.

When I execute the powershell script file from Power Shell Window, the process completes successfully.

I initially noticed the file had disappeared when I tried to using the Execute Process Task in SSIS, as seen in the posted on SQL Server Central: execute+process+task and MSDN Blog Article: run-powershell-scripts-in-ssis.

What is causing the file to disappear?

The .PS1 file refreshes an excel file with data connections and saves the file.


Power Shell, commands tested (only the first one fails, the other 3 work)

PS H:\> "PowerShell.exe" "-F C:\SVN\BusinessAnalysts\ExcelTools\DatabaseSSAS_UsageStats.xlsx_ExcelRefresh.ps1"
Unexpected token '-F C:\SVN\BusinessAnalysts\ExcelTools\DatabaseSSAS_UsageStats.xlsx_ExcelRefresh.ps1' in expression
 statement.
At line:1 char:103
+ "PowerShell.exe" "-F C:\SVN\BusinessAnalysts\ExcelTools\DatabaseSSAS_UsageStats.xlsx_ExcelRefresh.ps1" <<<<
    + CategoryInfo          : ParserError: (-F C:\SVN\Busin...xcelRefresh.ps1:String) [], ParentContainsErrorRecordEx
   eption
    + FullyQualifiedErrorId : UnexpectedToken

PS H:\> powershell -noexit C:\SVN\BusinessAnalysts\ExcelTools\DatabaseSSAS_UsageStats.xlsx_ExcelRefresh.ps1
PS H:\> PowerShell.exe -F C:\SVN\BusinessAnalysts\ExcelTools\DatabaseSSAS_UsageStats.xlsx_ExcelRefresh.ps1
PS H:\> PowerShell.exe -File C:\SVN\BusinessAnalysts\ExcelTools\DatabaseSSAS_UsageStats.xlsx_ExcelRefresh.ps1
PS H:\>

Command Prompt, commands tested (file disapears)

PowerShell.exe -File C:\SVN\BusinessAnalysts\ExcelTools\DatabaseSSAS_UsageStats.xlsx_ExcelRefresh.ps1

PowerShell_Exec_FileDisappears.png

enter image description here

Upvotes: 2

Views: 1693

Answers (2)

Ricardo Souza
Ricardo Souza

Reputation: 11

Sorry if I'm been silly, but I had a similar issue.

In my case the Kaspersky antivirus was removing the script to quarentine. Same thing had happen with pre-installed McAfee in another computer.

In both situations I was able to solve it by adding the PowerShell Scripts as a 'trusted application'.

The line that was triggering the antivirus was:

    Start-Process -FilePath PowerShell.exe -ArgumentList ('-File', $PSCommandPath, '-NoExit', '-NoProfile') -Verb Runas

At first I didn't notice the Kaspersky's notifications.

Then I tried register the script as a exclusion but didn't work as I was expecting. So Kaspersky start asking for desinfect.

Upvotes: 1

marsze
marsze

Reputation: 17055

This is more than weird. I was able to reproduce it. But I cannot explain why this happens. My guess is, this is somehow an optimization bug in the parsing of paths when calling PowerShell to run a script. You even have the same issue when calling it from inside PowerShell, when calling it like this for example:

. "powershell.exe" -File C:\SVN\BusinessAnalysts\ExcelTools\DatabaseSSAS_UsageStats.xlsx_ExcelRefresh.ps1
# or
Start-Process "powershell" -Arg "-File", "C:\SVN\BusinessAnalysts\ExcelTools\DatabaseSSAS_UsageStats.xlsx_ExcelRefresh.ps1"

I can give you some pointers what alternatives should work though:

Easiest: Remove or replace the dot!

powershell -File "C:\SVN\BusinessAnalysts\ExcelTools\DatabaseSSAS_UsageStats_xlsx_ExcelRefresh.ps1"

Alternatively, these variations should also work:

# Pass a command which calls the script using the call operator (&)
# Note: The single quotes are necessary! Else you will have the same behavior
powershell -Command "& 'C:\SVN\BusinessAnalysts\ExcelTools\DatabaseSSAS_UsageStats.xlsx_ExcelRefresh.ps1'"

# A workaround to avoid the issue that's probably causing this.
# The semicolon basically puts an empty command at the beginning.
# This supports my theory, that this is somehow an optimization bug.
powershell -Command ";& C:\SVN\BusinessAnalysts\ExcelTools\DatabaseSSAS_UsageStats.xlsx_ExcelRefresh.ps1"

# Base64-encoded version of
# "& C:\SVN\BusinessAnalysts\ExcelTools\DatabaseSSAS_UsageStats.xlsx_ExcelRefresh.ps1"
powershell -EncodedCommand "JgAgAEMAOgBcAFMAVgBOAFwAQgB1AHMAaQBuAGUAcwBzAEEAbgBhAGwAeQBzAHQAcwBcAEUAeABjAGUAbABUAG8AbwBsAHMAXABEAGEAdABhAGIAYQBzAGUAUwBTAEEAUwBfAFUAcwBhAGcAZQBTAHQAYQB0AHMALgB4AGwAcwB4AF8ARQB4AGMAZQBsAFIAZQBmAHIAZQBzAGgALgBwAHMAMQA="

I wrote some feedback to Microsoft about this, but I recommend you investigate this further and maybe write a bug report to the PowerShell team.

Upvotes: 2

Related Questions