问题描述
我想运行一些命令以通过Powershell远程执行Sysprep.
I want to run some commands to do Sysprep remotely through powershell.
所以我首先使用以下方法创建了一个会话:
So first I created a session using this:
$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中使用的变量:
Then assign the variables used in Sysprep:
$sysprep = 'C:\Windows\System32\Sysprep\Sysprep.exe'
$arg = '/generalize /oobe /shutdown /quiet'
$sysprep += " $arg"
然后最后运行:
Invoke-Command -Session $s -ScriptBlock {$sysprep}
当我运行最后一条命令时,实际上没有任何反应,但是在脚本块中,如果我给出诸如启动/停止服务之类的任何其他命令,而不是 $ sysprep
.但是SysPrep命令似乎不起作用.有人可以建议我做错了吗.
When I run the last command, nothing happens actually.But in the script block, instead of $sysprep
, if I give any other command like start/stop a service, it is giving some response. But SysPrep commands doesn't seem to work.Can anyone suggest what am I doing wrong.
推荐答案
您正试图在 $ sysprep
是字符串的情况下调用 scriptblock对象.您可能要为此使用 Start-Process
cmdlet.像这样:
You are trying to call for scriptblock object to run while $sysprep
is a string. You would want to use Start-Process
cmdlet for this. Like so:
$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
这篇关于通过Azure Powershell中的命令远程运行SysPrep的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!