正在尝试使用powershell执行scanstate.exe

正在尝试使用powershell执行scanstate.exe,powershell,Powershell,我正在尝试使用PowerShell制作GUI,以便更容易使用不同的xml文件操作scanstate.exe 然而,当我尝试执行它时,实际上什么都没有发生。我给出了正确的位置,但它似乎找不到 Start进程:由于以下错误,无法运行此命令:系统找不到指定的文件。 在C:\DigiTool\DigiTool.ps1:1266 char:129 + ... 启动进程-文件路径$c-参数列表$arglist-等待-。。。 + ~~~~~~~~~~~~

我正在尝试使用PowerShell制作GUI,以便更容易使用不同的xml文件操作scanstate.exe

然而,当我尝试执行它时,实际上什么都没有发生。我给出了正确的位置,但它似乎找不到

Start进程:由于以下错误,无法运行此命令:系统找不到指定的文件。
在C:\DigiTool\DigiTool.ps1:1266 char:129
+ ...             启动进程-文件路径$c-参数列表$arglist-等待-。。。
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+CategoryInfo:InvalidOperation:(:)[Start Process],InvalidOperationException
+FullyQualifiedErrorId:InvalidOperationException,Microsoft.PowerShell.Commands.StartProcess命令
我给出的参数可能有问题。但我不确定是什么

$c = Split-Path -Path $PSCommandPath
$c = (Join-Path $c "USMT\amd64\scanstate.exe")
Write-Host "$c"
Write-Host "$arglist"
Start-Process -FilePath $c -ArgumentList $arglist -Wait -PassThru                                                                                                                               

我找到了一种解决问题的方法,直接调用cmd并传递命令

启动进程cmd.exe-工作目录$dir-参数列表{/k scanstate.exe G:\a_ex03505\10.28.19-14.55.37/ue:*/ui:$user/o/c /i:Custom\migapo2019.xml/i:Custom\MigUser\u包括\u Downloads.xml /i:Migdocs.xml/i:Custom\ExcludeSystemFolders.xml /i:Custom\excludeddrives\u D\u to\u Z.xml /i:Custom\ExcludeOneDriveUserFolders.xml}


下面是一种高效的PowerShell惯用方法,可以在同一控制台窗口中同步调用命令,并将输出连接到PowerShell的流

# Determine the executable's file path.
$c = Join-Path $PSScriptRoot 'USMT\amd64\scanstate.exe'

# Note how &, the call operator is needed for invocation, because the
# executable path is stored in a *variable* (the same would apply if
# a *quoted* string were used).
& $c G:\a_ex03505\10.28.19-14.55.37 /ue:*\* /ui:$user /o /c /i:Custom\MigAppO2019.xml /i:Custom\MigUser_Including_Downloads.xml /i:Migdocs.xml /i:Custom\ExcludeSystemFolders.xml /i:Custom\ExcludeDrives_D_to_Z.xml /i:Custom\ExcludeOneDriveUserFolders.xml
如果需要在给定的工作目录中运行命令

# Determine the executable's file path.
$c = Join-Path $PSScriptRoot 'USMT\amd64\scanstate.exe'

# Note how &, the call operator is needed for invocation, because the
# executable path is stored in a *variable* (the same would apply if
# a *quoted* string were used).
& $c G:\a_ex03505\10.28.19-14.55.37 /ue:*\* /ui:$user /o /c /i:Custom\MigAppO2019.xml /i:Custom\MigUser_Including_Downloads.xml /i:Migdocs.xml /i:Custom\ExcludeSystemFolders.xml /i:Custom\ExcludeDrives_D_to_Z.xml /i:Custom\ExcludeOneDriveUserFolders.xml
推送位置$dir
#请注意,假定scanstate.exe位于$dir中,
#必须作为。\scanstate.exe调用-路径组件
#引用当前目录。-为了从那里执行。
# 
#根据设计,出于安全原因,PowerShell不会执行
#当前目录中的可执行文件。仅按文件名-仅适用于
#用于系统路径中的可执行文件(目录列在$env:path中)。
\scanstate.exe G:\a_ex03505\10.28.19-14.55.37/ue:\*/ui:$user/o/c/i:Custom\migapo2019.xml/i:Custom\MigUser\u包括_Downloads.xml/i:Migdocs.xml/i:Custom\ExcludeSystemFolders.xml/i:Custom\excludeddrives\u D\u to_Z.xml/i:Custom\ExcludeOneDriveUserFolders.xml
流行位置
也就是说,上面的代码在当前控制台窗口中调用
scanstate.exe
,并等待它完成,将其输出传递给-尽管带有捕获/中继/重定向输出以供进一步处理的选项(例如,
$output=scanstate.exe…
/
scanstate.exe | ForEach对象{…}
scanstate.exe 2>$null

  • 您只需要在新的独立控制台窗口中运行命令。

    • 请注意,这在具有PowerShell核心的类Unix平台上不起作用,其中始终隐含着
      -nonewindow
  • 控制台应用程序永远不需要执行
    cmd.exe
    ——PowerShell本身就是一个shell,就像
    cmd.exe
    ,所以让它处理调用,如上图所示

有关详细信息,请参阅


您声明在您的用例中控制台窗口是隐藏的,因此如果您需要命令以可见的方式运行,您确实需要
Start Process
,以便在新窗口中运行命令:

请注意,使用单个文本字符串(
“…”
)传递所有参数,这些参数隐式绑定到
-ArgumentList
参数


添加
-Wait
以等待新窗口关闭。

除了
cmd.exe
不是必需的,也可能不是
启动进程
(除非您需要在新窗口中运行命令),我建议不要使用脚本块
{…}
将参数传递给
[string]
-typed
-ArgumentList
参数:虽然从引用的角度来看很方便,但它会产生一种错误的印象,即您可以在脚本块内使用变量或表达式,这在这种情况下不起作用。