2

EDIT2: **After much testing and reading I've realised this is a powershell limitation "environmental snag." (read more here). I've resolved the issue by running the .exe as a task via schtasks.exe **

EDIT: After much testing the problem seems to occur from a difference between a remote PowerShell and a local PowerShell... Problem is still not solved, so any help is more than welcomed!

I'm trying to do something rather simple, which is just not working for me.

I have 2 machines, MachineA and MachineB. Both running PowerShell v2 and are trusted sources of each other with enabled remoting.

I'm trying to run a script on MachineB through MachineA via this command:

invoke-command -computername MachineB { C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -noprofile -executionpolicy Bypass Script.ps1 }

The script itself tries to install an .exe file that has a silent installation option (based on a config file in its directory):

$arguments = "-i silent"
$InstallerPath = "Setup.exe" 
Start-Process $InstallerPath5 $arguments5 -verb runas

When I run the script locally on MachineB - all works fine and installation finishes successfully. However, when I run the script remotely (with the 1st command from MachineA) it just finishes instantly and nothing happens - the installers don't open in the task manager on MachineB at all. No error is produced and no logs.

Funnily enough, when I change the actual script to "& C:\Windows\system32\cmd.exe /c Setup.exe -i silent" and run it remotely the setup starts up, works for 5-6 seconds on 50% cpu utilisation and then goes to 0% and hangs forever. Again, if I run it locally it all works perfectly...

If tried:

  • Running the installer remotely directly (e.g. via invoke-command { & Setup.exe -i } )
  • Editing the script to run without Start-Process ( e.g. & Setup.exe -i)
  • Moving the script to MachineA and running it remotely to MachineB (e.g. invoke-command -filename sciprt.ps1 -computername MachineB)

All of these work if I do it locally on MachineB, but none work if done remotely via MachineA (with different problems though)? I'm going crazy.

I also checked if the remoteshell has admin rights via this:

([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")

It returns True. Plus, I can remotely edit the HKLM registry so I assume its working.

Any ideas are more than welcomed at this point!

Petar
  • 51
  • 1
  • 7
  • Does your installer log anything? Can you configure it to log? I don't see a full path specified for the `-FilePath` on start process. Is it just obfuscated for the purpose of the question or in the same dir as the script? Makes me wonder if the path to the exe is the issue – Matt Jan 07 '15 at 20:39
  • HI Matt, its obfuscated for the purpose of the script. Path to exe is correct (to be exact path is D:\Installers\IBM\EMM_Installer.exe). I don't seem to be able to make it log anything - the exe doesn't seem to have a -help function :/ The only parameters it accepts are `-i silent` and `-i console` (for manual console installation)... – Petar Jan 07 '15 at 21:16
  • Not to side track too much but does Windows log anything for this happening? That path is correct relative to the remote computer? – Matt Jan 07 '15 at 21:18
  • Hi Matt, I can't find anything relevant in the logs :-( Anything particular log you have in mind? I tried something else now: `New-PSSession -cn MachineB` and then run the script through the remote session with `& D:\Installers\IBM\EMM_Installer.exe' ... This way the process starts but again after a few seconds hangs at 0% cpu utilisation and some memory usage... and nothing ever happens it just stays there. I also immediately after killing it run the script locally on MachineB and everything installed properly.. I think there might be 2 problems here, but the main one occurs from the fact – Petar Jan 07 '15 at 22:12
  • that remote sessions have something different than local sessions that makes the installer hang?! I think this is where the problem comes from.. – Petar Jan 07 '15 at 22:13
  • 1 more hint: `[MachineB]: PS C:\Users\p_lafchiev\Documents> ([Security.Principal.WindowsPrincipal] [Security.Princi pal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator") True` However, if I run Get-PSSessionConfiguration through that same remote session I get: `Access is denied. + CategoryInfo : + FullyQualifiedErrorId : System.InvalidOperationException,Microsoft.PowerShell.Commands.GetChildItemCommand` Is this an admin right issue?! I'm well confused? – Petar Jan 07 '15 at 22:24

2 Answers2

0

After much testing and reading I've realised this is a powershell limitation "environmental snag." (read more here). I've resolved the issue by running the .exe as a task via schtasks.exe

Petar
  • 51
  • 1
  • 7
0

I've currently been playing with this. I do a lot of remoting in and installing software for a couple hundred machines. We were experiencing an issue where users Java was getting deleted upon every reboot. Anyways, it probably takes 2-5 minutes to get the machine name from them and then install the software. I wrote this little piece (still in the works, but working) so all I need is their username, and roughly 1 and half minutes.

import-module ActiveDirectory
$username = Read-Host -prompt 'Username'
$dn = Get-ADUSER $username -properties * | SELECT -expand DistinguishedName
$results = Get-ADCOMPUTER -LDAPFilter "(ManagedBy=$dn)" -properties * | SELECT CN,ManagedBy
$results | Out-Host
$cn = Read-Host -prompt 'Choose a computerName from above'


#Copy the .exe to remote system
$source = 'C:\Scripts\Programs\JavaSetup8u151.exe'
$destination = '\\' + $cn + '\C$\Users\' + $username + '\Downloads'
Copy-Item -Recurse -Filter *.* -path $source -destination $destination -Force

#move to psTools directory for remote Install, you will need this directory
#tied into your  dir "C:\\psTools" for example


cd \psTools
#Ensures that remoting is activated on end client

.\psexec \\$cn -s powershell Enable-PSRemoting -Force

#Remote to machine

INVOKE-COMMAND -ComputerName $cn -ScriptBlock {

#Passing in local variable, mainly for directory movement purposes
$username = $using:username

#define SearchVariable to see if already install
$searchTerm = 'Java'

#Setup for uninstall
$app = Get-WMIObject -Class Win32_Product -Filter "Name LIKE '%$searchTerm%'"
IF ($app -eq $null) {
    WRITE-HOST 'Currently no programs containing' $searchTerm 'in their name.'
} ELSE {
    $app.uninstall()
    $app = Get-WMIObject -Class Win32_Product -Filter "Name LIKE '%$searchTerm%'"
    IF ($app -eq $null) {
        WRITE-HOST 'Successfully deleted all programs containing' $searchTerm '.'
    } ELSE {
        WRITE-HOST 'Failed to delete all programs containing' $searchTerm '. Please report errors to ADMIN.'
    }
}

cd \Users\$username\Downloads

#This was just me proving to myself that I was in the right dir and the file was actually copying over
$dir = Get-ChildItem -Force
$dir | Out-Host

#Install
cmd
.\JavaSetup8u151.exe INSTALL_SILENT=1 AUTO_UPDATE=0 REBOOT=0 SPONSORS=0 REMOVEOUTOFDATEJRES=1
Start-Sleep -s 60


#Reset variable so we can see if install was successful or not
$app = Get-WMIObject -Class Win32_Product -Filter "Name LIKE '%$searchTerm%'"
IF ($app -ne $null) {
    WRITE-HOST 'You have successfully installed' $searchTerm'.'
} else {
    WRITE-HOST 'You have failed to install' $searchTerm'.'
}
}

Sorry, not a good copy-paste over of the code. My problem was the exe not running; and I think it was trying to run IF ELSE check at the end before the install could finish. The Start-Sleep cmdlet was my answer. I ran it through cmd instead of ps, but I think it would work both ways now. 'EDIT: Works both ways.'

cmd 
.\JavaSetup8u151.exe INSTALL_SILENT=1 AUTO_UPDATE=0 REBOOT=0 SPONSORS=0 REMOVEOUTOFDATEJRES=1
 Start-Sleep -s 60

I would recommend using a:

Enter-PSSession -ComputerName $cn

To time how long it takes for the install. 45 seconds was just shy, so I opted for a full 60.