通过Azure Powershell中的命令远程运行SysPrep

时间:2018-09-03 06:43:50

标签: azure powershell command azure-virtual-machine sysprep

我想运行一些命令以通过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命令似乎不起作用。有人可以建议我做错了什么。

1 个答案:

答案 0 :(得分:0)

您正在尝试在$sysprep字符串的情况下运行脚本块对象。您可能要为此使用Start-Process cmdlet。像这样:

$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
相关问题