从CMD

时间:2019-01-23 17:15:22

标签: powershell cmd

当我从 Command Prompt Window 执行Powershell脚本文件时,该文件就消失了。 CMD窗口不显示任何错误消息,它仅在执行命令后转到下一行。我从备份还原文件。

当我从 Power Shell Window 执行powershell脚本文件时,该过程成功完成。

最初,我发现尝试使用 Execute Process Task in SSIS 时文件已消失,如SQL Server Central: execute+process+taskMSDN Blog Article: run-powershell-scripts-in-ssis上所显示。

是什么导致文件消失?

.PS1文件使用数据连接刷新excel文件并保存。


Power Shell,已测试命令(仅第一个失败,其他三个起作用)

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:\>

命令提示符,已测试命令(文件消失)

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

PowerShell_Exec_FileDisappears.png

enter image description here

2 个答案:

答案 0 :(得分:2)

这不奇怪。我能够重现它。但是我无法解释为什么会这样。我的猜测是,这在调用PowerShell运行脚本时在某种程度上是路径解析中的优化错误。从 inside PowerShell调用它时,甚至在遇到这样的问题时,例如:

. "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"

我可以为您提供一些指示,但哪些替代方法应该起作用:

最简单:删除或替换点!

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

或者,这些变体也应该起作用:

# 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="

我已就此向Microsoft写了一些反馈,但我建议您进一步调查,并可能向PowerShell团队写一个错误报告。

答案 1 :(得分:1)

对不起,如果我很傻,但是我遇到了类似的问题。

就我而言,卡巴斯基反病毒软件正在删除脚本以进行隔离。预先在另一台计算机上安装McAfee的情况相同。

在两种情况下,我都可以通过将PowerShell脚本添加为“受信任的应用程序”来解决该问题。

触发防病毒的行是:

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

起初我没有注意到Kaspersky's notifications.

然后,我尝试将脚本注册为排除项,但没有按预期运行。因此,卡巴斯基开始asking for desinfect.