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
Then assign the variables used in Sysprep:
$sysprep = 'C:\Windows\System32\Sysprep\Sysprep.exe'
$arg = '/generalize /oobe /shutdown /quiet'
$sysprep += " $arg"
And then finally run this:
Invoke-Command -Session $s -ScriptBlock {$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.
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