How to remotely access a resource and open a window program

matitdob 21 Reputation points


I have a script that executes a task from the attached schematic. Everything works fine on the local machine.
After placing the script in Invoke-Command -ComputerName $ ComputerName -Credential $ Credential -ScriptBlock {MyScript}
I have two problems:

  1. Access to resource is denied: "\FS\C$\IT\Folder" (The user specified in the credentials has rights to the resource)
  2. And the window application does not start. $path = Get-CimInstance -Class Win32_Product | Where-Object Name -eq $AppName | Select Caption,InstallLocation
    if($path.InstallLocation -ne $null){
    if((Test-Path $path.InstallLocation) -and (Test-Path "\FS\C$\IT\Folder")){
    for($Counter = 0; $Counter -lt 3; $Counter++){
    Get-Process -Name $ProcessName -ErrorAction SilentlyContinue | Stop-Process -Force
    Remove-Item $path.InstallLocation -Recurse -Force
    sleep 10
    if(!(Test-Path $path.InstallLocation)){
    Copy-Item -Path "\FS\C$\IT\Folder" -Destination $path.InstallLocation.Replace("\Folder",'') -Recurse
    Sleep 10
    Start-Process $PathProgramLNK


Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,273 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Rich Matheisen 43,926 Reputation points

    Have you checked the permissions on the C$ share? How about the C: disk? Are you using a domain account or a local user account in the credential? Have you tried using the -RunAsAdministrator switch on the Invoke-Command?

    0 comments No comments

  2. MotoX80 30,821 Reputation points

    The following variables are not defined/initialized: $AppName, $ProcessName, $PathProgramLNK. Any statement that references them will not work properly. To reference variables that are set in the main part of the script by statements within the -ScriptBlock you will have to use "$using:". Search the net for "powershell invoke-command $using variables"

    1. Access to resource is denied: "\FS\C$\IT\Folder" (The user specified in the credentials has rights to the resource)

    For starters, I would not recommend using the C$ (administrative) share. Use a "normal" share and see if you still have the problem. If you do it's most likely the double hop problem. Search the internet for "powershell double hop" and implement the solution that works best for your environment.

    1. And the window application does not start.

    Assuming that you correctly set the $PathProgramLNK variable somewhere, the program will in fact start. It "works on the local machine" probably because you are running the script in an interactive session. Invoke-Command is non-interactive. There is no user's desktop. You are not running in a user's session. If you were expecting a GUI window to popup somewhere, that's not going to happen. Using the task scheduler you can create and start a task that is set to run as INTERACTIVE. If someone is logged on to the desktop, then they should see the GUI window.

    I do not understand what the FOR loop that references $Counter is supposed to do. I also don't understand why you are overwriting the folder where the app has been installed to. That is the function of the Windows Installer.

    0 comments No comments