1

我想通过 powershell 运行一些命令来远程执行 Sysprep。

所以首先我使用这个创建了一个会话:

$UserName = "IPAddress\username"
$Password = ConvertTo-SecureString "password" -AsPlainText -Force
$psCred = New-Object System.Management.Automation.PSCredential($UserName, $Password)
$s = New-PSSession -ComputerName IPAddress -Credential $psCred

然后分配 Sysprep 中使用的变量:

$sysprep = 'C:\Windows\System32\Sysprep\Sysprep.exe'
$arg = '/generalize /oobe /shutdown /quiet'
$sysprep += " $arg"

然后最后运行这个:

Invoke-Command -Session $s -ScriptBlock {$sysprep}

当我运行最后一个命令时,实际上什么都没有发生。但是在脚本块中$sysprep,如果我给出任何其他命令,例如启动/停止服务,它会给出一些响应。但是 SysPrep 命令似乎不起作用。任何人都可以建议我做错了什么。

4

1 回答 1

1

您正在尝试调用scriptblock 对象运行 while $sysprepis a string。您可能希望为此使用Start-Processcmdlet。像这样:

$sysprep = 'C:\Windows\System32\Sysprep\Sysprep.exe'
$arg = '/generalize /oobe /shutdown /quiet'
Invoke-Command -Session $s -ScriptBlock {param($sysprep,$arg) Start-Process -FilePath $sysprep -ArgumentList $arg} -ArgumentList $sysprep,$arg
于 2018-09-03T06:50:27.880 回答