【问题标题】:Running PowerShell script from a batch file successful on 32 bit but fails on 64 bit从批处理文件运行 PowerShell 脚本在 32 位上成功,但在 64 位上失败
【发布时间】:2013-09-10 02:21:11
【问题描述】:

我有一个 powershell 脚本 (test.ps1),我想从批处理文件中运行它。批处理文件包含以下内容:

PowerShell –Command “& ‘.\test.ps1’” 001
pause

当我在 32 位 Windows 7 机器上运行批处理文件(以管理员身份)时,powershell 脚本运行成功。当我尝试在 64 位 Windows 7 机器上运行它时,我收到以下错误:

C:\Windows\system32>PowerShell –Command “& ‘.\test.ps1’” 001
The term ‘.\test.ps1’ is not recognised as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if the path was included, verify that the path is correct and try again.
At line:1 char:2
+ & <<<<  ‘.\test.ps1’ 001
    + CategoryInfo              : ObjectNotFound: (.\test.ps1:String) [], CommandNotFoundException
    + FullyQualifiedErrorID : CommandNotFoundException

有什么想法吗?

谢谢

【问题讨论】:

    标签: powershell batch-file 64-bit


    【解决方案1】:

    您正在从C:\Windows\system32 运行您的脚本,并且没有这样的脚本(通常在以管理员权限启动 bat 时发生这种情况)。 更改目录或使用完整路径调用它。

    解决此问题的最简单方法 - 在批处理脚本的开头设置 cd /d %~dp0 以更改脚本所在位置的目录

    【讨论】:

    • test.ps1和批处理文件在同一个文件夹下(例如C:\test)。
    • 刚刚检查了两台机器,运行文件时,32 位机器显示 C:\test>... 但 64 位机器启动 C:\Windows\System32>。
    • 那么我如何告诉 64 位机器从我试图运行它的文件夹中运行呢? (我不能放完整路径,因为它可以是任何东西,运行它的人可以将文件复制到他们想要的任何地方)
    • 可能您使用 administrator 权限启动脚本 - 在这些情况下,它从 system32 开始
    • 在批处理脚本的开始处设置cd /d %~dp0以更改脚本所在位置的目录。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-22
    • 2013-02-23
    • 1970-01-01
    • 2011-03-08
    • 2020-03-22
    • 1970-01-01
    相关资源
    最近更新 更多