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
Related
I am having an issue with running a batch file that is located on a remote server.
I have a batch file located on a remote server that i want to run that kicks off an automated Selenium test. For simplicity, let's say the name of my batch file is mybatch.bat
I have the following code in a Powershell script located on the server:
$BatchFile = "mybatch.bat"
Start-Process -FilePath $BatchFile -Wait -Verb RunAs
If I run this PowerShell script locally on the server in ISE then it runs fine and it kicks off the selenium test which takes a couple minutes to run.
Now I want to try to execute this test from another machine by using PowerShell remoting. Let's assume that remoting is already configured on the servers.
I have a PowerShell script located on another server which has the following code segment. Assume that all of the session variables have the correct information set:
$CMD = "D:\mybatch.bat"
$TargetSession = New-PSSession -ComputerName $FullComputerName -Credential $myCreds -ConfigurationName RemoteExecution
$command = "powershell.exe -File $CMD -Wait"
Invoke-Command -Session $TargetSession -ScriptBlock { $command }
When this script runs, it does connect to the remote machine and create a remote session. It does look like it kicks off the batch file because PowerShell does not give me an error. But it does not wait for the full 3 or 4 minutes for the Selenium test to finish. It seems like it just times out. Also if I am logged onto the other machine, I don't see any Selenium web test running. No Selenium log files or results files are created on remote server as should be expected.
I was wondering what I could be doing wrong with my code.
Also, it seems that the server always returns the echo outputs of the batch file to my local machine. I see these random blinking white screen on ISE which looks like output from the batch file
$command = "powershell.exe -File $CMD -Wait"
Invoke-Command -Session $TargetSession -ScriptBlock { $command }
There are 2 issues with the above code:
$command inside the scriptblock and $command outside the scriptblock are different variables due to different scopes. The variable inside the scriptblock is thus undefined and the scriptblock will simply echo an emtpy value.
Even if $command weren't undefined, the scriptblock would still just echo its value, since it's defined as a string. PowerShell does not execute strings unless you're using something like Invoke-Expression (which you shouldn't).
This should do what you want:
$CMD = "D:\mybatch.bat"
$TargetSession = New-PSSession -ComputerName $FullComputerName -Credential $myCreds -ConfigurationName RemoteExecution
Invoke-Command -Session $TargetSession -ScriptBlock { & $using:CMD }
If you would like execute a bat file from another machine by using PowerShell Remote Session, simply enter dot and then follow by a whitespace, then enter the exact path of bat file located on that remote machine.
Example
Invoke-Command -ComputerName RemoteComputerName -Credential $credential -ScriptBlock {. "D:\Temp\Install_Something.bat"}
I have a script that installs an .exe with some arguments remotely to a list of servers that works fine. When I try to do almost the exact same thing but run the uninstall.exe that gets installed to C:\Program Files (x86)\ it won't work.
When I run the scripts on the server locally, it kicks off the uninstall. When I try to run the exact same script or command using the powershell invoke-command it won't work.
$serverlist = Get-Content -Path C:\NagiosInstall\test.txt
ForEach ($server in $serverlist) {
New-Item -Path "\\$server\C$\" -Name "NagiosInstall" -Force -ItemType "directory"
Copy C:\NagiosInstall\ncpa-2.1.6.exe \\$server\C$\NagiosInstall\ncpa-2.1.6.exe
Copy C:\NagiosInstall\install.bat \\$server\C$\NagiosInstall\install.bat
invoke-command -ComputerName $server -ScriptBlock {C:\NagiosInstall\install.bat}
Start-Sleep -s 15
invoke-Command -ComputerName $server -ScriptBlock {Remove-Item -LiteralPath "C:\NagiosInstall" -Force -Recurse}
}
The install .bat is just a simple command to silently install that ncpa-2.1.6.exe.
Above is my install script, that part all works fine.
invoke-command -ComputerName $server -ScriptBlock {Start-Process -FilePath "C:\Program Files (x86)\Nagios\NCPA\uninstall.exe" -ArgumentList "/S"}
Running the above command, nothing happens. No errors, nothing.
& "C:\Program Files (x86)\Nagios\NCPA\uninstall.exe" -ArgumentList "/S"
But running the above command in powershell that's running as admin locally on the server and it works just fine.
I've also tried the same approach to create and copy and run a batch file, very similar to the above "install" code. Same thing... nothing happens but if you run the batch locally on the server, it works just fine. I can post this code if anyone is interested.
I'm guessing it has to do with the invoke-command or the fact that it's in C:\Program Files (x86) which might make the syntax different, but I've tried many things and I'm out of ideas besides making an account and posting here.
The issue is that Invoke-Command runs non-interactively, and therefore cannot run as Administrator and respond to a UAC prompt.
The only workaround is to connect to the computer via a PSSession with credentials, and execute it that way:
$Cred = Get-Credential
$Session = New-PSSession -ComputerName $server -Credential $Cred
Invoke-Command -Session $Session -ScriptBlock {Start-Process -FilePath "C:\Program Files (x86)\Nagios\NCPA\uninstall.exe" -ArgumentList "/S"}
$Session | Exit-PSSession
Edit:
The reason that the installer works is that the UAC prompt for Windows Installs is different than anything else in Windows see: How to Silence the UAC Prompt for Per-Machine MSI Packages for Non-Admins or Using Windows Installer with UAC.
Essentially, Windows Installer (already running as admin and UAC approved), is what runs the install on your behalf, and it is Windows Installer and installer settings that determines if you need to see a UAC prompt or not. Hence, this is why the install works. Windows Installer determined that you did not need to see the UAC prompt, and the install proceeds.
Uninstalling is different. Since you are running uninstall.exe, the executable needs admin access and Windows will do UAC before the uninstall.exe even runs.
I uploaded some files to a remote host with PowerShell, by FTP. On this host runs Windows 7 Embedded.
It turns out there is EWF (Enhanced Write Filter). So after a restart the uploaded files were gone. For saving the changes it needs commit them in cmd (at the remote host) by: ewfmgr d:-commit How can I include this command in my PowerShell code?
The code:
Enable-PSRemoting -Force
Set-Item wsman:\localhost\client\trustedhosts -Value * -Force
Restart-Service WinRm
Test-WSMan $line
Invoke-Command -ComputerName $line -scriptblock {cmd.exe /c "ewfmgr d: -commit"} -credential $FTPCredential
When I run Enable-PSRemoting -Force manually on the remote computer, it works, but it is uncomfortable and take lots of time. Is there another way to do this once for many hosts simultaneously?
Example-Code:
$session = New-PSSession -ComputerName yourRemoteComputer
Invoke-Command -Session $session -Scriptblock {ewfmgr d: -commit}
Remove-PSSession -Session $session
You have to enable Powershell Remoting on your host to invoke a command like this (https://technet.microsoft.com/en-us/library/ff700227.aspx)
If you need to transmit Credentials to your remote host, you can add the -Credential-Parameter to New-PSSession. This article describes how to add valid Credentials to your script (https://technet.microsoft.com/en-us/library/ff700227.aspx)
Greetings, Ronny
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.
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.