Exe file remote Execution - powershell

I'm trying to write a script which will patch SQL Instances remotely. Referring this forum, I have framed the following line for executing the .exe remotely on other server:
Invoke-Command -ComputerName $computer -ScriptBlock {
& cmd /c 'D:\SQL_PATCH\SQLServer2012SP2-KB2958429-x64-ENU.exe' /qs /action=patch /allinstances /IAcceptSQLServerLicenseTerms
}
Also tried this as well:
Invoke-Command -ComputerName $computer -ScriptBlock {
& 'D:\SQL_PATCH\SQLServer2012SP2-KB2958429-x64-ENU.exe' -ArgumentList "/qs", "/action=patch", "/allinstances", "/IAcceptSQLServerLicenseTerms"
}
One more peculiar thing with this is, the first command is running fine on the Windows 2012 servers but not on Windows 2008R2 server. I don't know what's the reason behind this.

The following did the trick to what I was looking for:
psexec \\$computer -s -u Adminuser -p AdminPassword E:\SQL_PATCH\SQLServer2012SP2-KB2958429-x64-ENU.exe /quiet /action=patch /allinstances /IAcceptSQLServerLicenseTerms
This piece is working on all the servers irrespective for Windows versions.
Thanks everyone for your valuable suggestions.

Related

remote powershell in azure VM

Since many days I am trying to sysprep an azure virtual machine.
Following is the command that I am using to connect to the VM from local and executing the Upload-GoldImage.ps1 script
Invoke-Command -ConnectionUri $uri -SessionOption (New-PSSessionOption -SkipCACheck -SkipCNCheck) -Credential $credential -ScriptBlock {C:\Windows\system32\WindowsPowerShell\v1.0\powershell.exe -file "c:\program files\WindowsPowershell\Upload-GoldImage.ps1" -validateCurrentOS}
remote machine gets connected successfully. All the commands in the ps1 file are executed successfully but only the sysprep command isn't executing.
The ps1 mentioned above contains one command to sysprep the machine - Start-Process -FilePath C:\Windows\System32\Sysprep\sysprep.exe -ArgumentList '/generalize /oobe /shutdown /quiet'
Write-Host("After sysprep")
When this gets executed on my local powershell prompt - "After sysprep" is getting printed. I am getting no error in the sysprep command. Still the machine is not getting sysprepped.
Please if anyone could help me out
Thanks.
Finally I was able to sysprep the machine from my local powershell.
Previously my script used Start-Process command.
But then, http://social.technet.microsoft.com/wiki/contents/articles/7703.powershell-running-executables.aspx#Diagnostics_Process_Start
- this link helped me out, where I tried various several ways mentioned in the link.

How do I remotely start a VM on VMWare Workstation using Powershell?

I have VMWare Workstation and beginner's knowledge of PowerShell.
I've created a script that successfully starts a VM on my local machine by using the vmrun tool. However, if I run it through a remote session, nothing happens. Any idea why?
Get-PSSession | Remove-PSSession
$VMHostMachine | New-PSSession
$rs = Get-PSSession # testing using localhost
Write-Debug ("Now starting VM on host: " + $VMHostMachine)
$script = {param($VMImagePath, $VMConsolePath);
$QuotedVMPath = "`"{0}`"" -f $VMImagePath
$Result = Start-Process -FilePath $VMConsolePath -ArgumentList "-T", "ws", "start", $QuotedVMPath -Wait -PassThru -NoNewWindow
}
Invoke-Command -Session $rs -ScriptBlock $script -ArgumentList $vmConfig.VMImagePathOnHost, $vmConfig.VMRunUtiltyPath
Invoke-Command works if I remove the session parameter:
Invoke-Command -ScriptBlock $script -ArgumentList $vmConfig.VMImagePathOnHost, $vmConfig.VMRunUtiltyPath
I have a similar script that successfully reverts to a snapshot on my localhost through a PSSession, so why is starting a VM giving me trouble?
Does it work if you "enter-pssession" and then try to invoke?
I'm assuming it's "not working" for you here, because you invoke commands through PSSession where there are no visible desktops / gui-session, and you are expecting the VMWare workstation to appear? The receiving end is a service.
You could check if the process is running VMWare with Get-Process.
To do what you want to achieve here you could take a look at PsExec, which allows to start applications into an active session.
https://technet.microsoft.com/en-US/sysinternals/bb897553.aspx
Note the "-i" parameter on PSExec.

powershell execute .exe remotely

$computername = Read-Host "Enter Machine Name - "
Invoke-command -ComputerName $computername -ScriptBlock { & cmd /c 'c:\download\niniteone\niniteone.exe' /select "malwarebytes"}
Wondering if someone could tell me where I've gone wrong with this, it just dies when I run it. I've put this script together by looking at the others here but I can't seem to get it to work. We use ninite pro to update/install some 3rd party apps and I'm trying to setup some powershell scripts to run it on remote computers. Any help would be appreciated :)
Update - I added the cmd /c to the script block and now it works great!? I read cmd /c isnt needed with powershell v2? I'm confused... It's working but I'd like to get it right.
How about:
Invoke-command -ComputerName $computername -ScriptBlock { Start-Process -FilePath "c:\download\niniteone\niniteone.exe" -ArgumentList "/select `"malwarebytes`""}

Invoke background remote job

I have a problem with start-job and invoke-command. When I run it locally it works as it should, but when I use it remotely it just exists and does nothing. This is the command:
invoke-command {start-job { & notepad}}
Notepad is just an example, as I want another process to run in the background. How to fix it to work remotely? It seems like an issue with powershell session closing after invoke-command. On Linux it was fixed by using nohup command but I can't find anything that would resemble it in powershell.
I'd do this, from the local system:
invoke-command -scriptblock { & notepad } -computername <remote computer> -asjob

Running batch file on Remote Computers using PowerShell 2.0

I am trying to run a exe on remote machines which would basically uninstall a product agent. below is the code:
$test = Get-Content PC.txt
foreach ($a in $test)
{
$curr = Get-Location
Set-Location \\$a\Admin$\System32\CCMSetup
.\ccmsetup.exe /uninstall
Set-Location $curr
}
It doesn't work. I ended up removing the program from the host computer itself :)
Alternate Option: I created a batch file with the command line:
cd C:\Windows\System32\ccmsetup
ccmsetup /uninstall
exit
It seems the above can also be achieved using Invoke-Command.
Invoke-Command -ComputerName $client -FilePath UninstallCCM.cmd
Apparently, it does not accept batch file. I would like to keep it as simple as possible.
Currently I am using PSExec for installing and uninstalling the program. Do I need to enable PS Remoting (WinRM) on every remote machine on whom I need to execute scripts using PowerShell?
Can someone please help? Thanks in advance.
This command should execute successfully:
Invoke-Command -ComputerName $client -ScriptBlock { cd C:\Windows\System32\ccmsetup; ccmsetup /uninstall} -Credential $(Get-Credential) -Authentication CredSSP
but you will need to enable CredSSP authentication on all machines by running these two commands on each machine:
Enable-WsManCredSSP -Role Server -Force
Enable-WSManCredSSP -Role Client -DelegateComputer * -Force
I highly recommend downloading PSTools. There is a command in there called "psexec"
PSexec is so simple, you call it like this:
psexec \\myserver C:\Windows\System32\ccmsetup /uninstall