2

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

7
  • This is (naturally) not normal behavior. It must have to do something with what happens in the script. We cannot tell without seeing it. Maybe your anti virus software deletes it? Commented Jan 23, 2019 at 17:22
  • @marsze - How could it be the script? The scripts runs fine when the lines are entered manually in Powershell. The PS1 script file execution works just fine when ran via powershell. The PS1 script file execution when ran via command line is the only issue... the file gets deleted. Thanks for your idea about anti virus; Is there some way I can look into that possibility? Our org uses Kaspersky for anti virus. Commented Jan 23, 2019 at 19:54
  • 1
    It's very simple to verify: Have you tried changing the script's contents to something simple like an echo command and then try it again? I suppose it should not be deleted then. You can use that technique to narrow down the problem. Commented Jan 23, 2019 at 21:12
  • 1
    Have you tried other script names / paths etc.? Try to narrow down the problem. I cannot really help since I cannot reproduce this. Commented Jan 23, 2019 at 22:02
  • 1
    I can reproduce it now. It seems to be related to the dot in the file name (from ".xlsx"). Funny, if you remove the dot, it works. Even funnier: If you replace ".xlsx" with ".ps1" inside the file name, it works too. This seems almost like a bug in PowerShell or CMD. I will investigate it further. Commented Jan 24, 2019 at 8:19

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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.

1 Comment

Somehow -NoExit flag raises this problem. It works if I remove it.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.