简体   繁体   中英

Install program remotely using Invoke-Command

The variable at the top of the script defines several commands/variables for New-PSDrive , as well as connection and installation. After this, a function is created to open a text file and extract information out of it. I know this part works because I use it in 2 other scripts. Lastly, The script executes the commands in the first variable.

The script will show as running successfully, but checking the remote computer reveals that nothing happened.

Prior to doing any of this activity, the remote computer has a script run against it that:

  • enables PSRemoting (setting firewall rules and starting WinRM), and
  • bypasses execution policies.

After those steps, the script below is run to install a piece of software.

$eAudIT2014V2Install = {
  $eAudIT2014V2password = ConvertTo-SecureString "PasswordHere" -AsPlainText -Force
  $eAudIT2014V2cred = New-Object System.Management.Automation.PSCredential('domain\user', $eAudIT2014V2password)
  $eAudIT2014V2drive = New-PSDrive -Name eAudIT2014V2 -PSProvider FileSystem -Root "\\Server\Share" -Credential $eAudIT2014V2cred
  $eAudIT2014V2job = Start-Job {"eAudIT2014V2:\Setup.cmd"}
  Wait-Job $eAudIT2014V2job
  Receive-Job $eAudIT2014V2job
}

Function Get-OpenFile($initialDirectory) {
  [System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms") |
    Out-Null

  $OpenFileDialog = New-Object System.Windows.Forms.OpenFileDialog
  $OpenFileDialog.InitialDirectory = $initialDirectory
  $OpenFileDialog.ShowDialog()
  $OpenFileDialog.Filename
  $OpenFileDialog.ShowHelp = $true
}

$InputFile = Get-OpenFile
if ($InputFile -eq "Cancel") {
  Write-Host "Canceled By User"
  exit
} else {
  $Computers = @(Get-Content -Path $InputFile)
}

foreach ($computer in $computers) {
  Write-Host "Installing eAudIT 2014V2 on Selected Computers"
  Invoke-Command $eAudIT2014V2Install
}

I'm noticing that if I tell this script to run something basic like notepad.exe, a dllhost process starts on the machine, but notepad never does. What am I doing wrong?

The answer is pretty simple here. All of your script is for naught if you don't tell the Invoke-Command cmdlet what computer you want to execute the code on. As it is you are simply iterating a loop and invoking that command X number of times on the local machine. You need to change that second to the last line to specify the machine to execute the code on:

Invoke-Command $eAudIT2014V2Install -ComputerName $computer

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM