# Remotely executing a program via a script

**URL:** <https://forums.powershell.org/t/remotely-executing-a-program-via-a-script/7312>\
**Category:** PowerShell Help\
**Created:** [October 4, 2016, 7:54am UTC](https://forums.powershell.org/t/remotely-executing-a-program-via-a-script/7312 "2016-10-04T07:54:18Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![andy-tuke](https://avatars.discourse-cdn.com/v4/letter/a/73ab20/32.png) [@andy-tuke](https://forums.powershell.org/u/andy-tuke)\
**Post date:** [October 4, 2016, 7:54am UTC](https://forums.powershell.org/t/remotely-executing-a-program-via-a-script/7312/1 "2016-10-04T07:54:18Z")

</div>

Hi all,

I have written a script which generates an argument set for an IBM application called Cognos which produces data cubes. My script produces a set of strings in the format -  
-nologo -n -c -g -f’\dfsnamespace-Path\Preferences.xml’ -m’\dfsnamespace-Path\PPC005.mdl’

The unc paths are abstracted by DFS in Active Directory.

I am testing this on my workstation, the final plan will be for it to run on a clustered scheduling 2012 Server, and the final action is to create a remote session to the Cognos application server which will then run the command line generated. So my script creates a variable $CommandLineArgs containing a string like the one above

If I run the following in the shell it works (the Cogtr.exe executable has been added to the Path Environment variable on the app server)

```
$CommandLineArgs = "-nologo -n -c -g -f'\\dfsnamespace-Path\Preferences.xml' -m'\\dfsnamespace-Path\PPC005.mdl'"
Enter-PSSession -ComputerName PPCLive01.mydomain.co.uk -Credential $Cred -Authentication Credssp
    Invoke-Expression ("Cogtr.exe "+ $CommandLineArgs)
Exit-PSSession
```

But if I put exactly the same lines of code in a script then I get the error, I have tried putting the full path

Cogtr.exe : The term ‘Cogtr.exe’ is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.  
At line:1 char:1

Any help would be enormously appreciated. I am assuming that the script is not attempting to run the Cogtr.exe on the server, but on my Workstation.  
Thanks

Andy

---

<div class="post-metadata">

**Author:** ![sergiu-andrei-creife](https://avatars.discourse-cdn.com/v4/letter/s/77aa72/32.png) [@sergiu-andrei-creife](https://forums.powershell.org/u/sergiu-andrei-creife)\
**Post date:** [October 4, 2016, 8:57am UTC](https://forums.powershell.org/t/remotely-executing-a-program-via-a-script/7312/2 "2016-10-04T08:57:59Z")

</div>

Hi Andy,  
Try specifying the full path name to Cogtr.exe and if this fails try to use "Cogtr.exe " -join $CommandLineArgs  
Regards,  
Sergiu

---

<div class="post-metadata">

**Author:** ![andy-tuke](https://avatars.discourse-cdn.com/v4/letter/a/73ab20/32.png) [@andy-tuke](https://forums.powershell.org/u/andy-tuke)\
**Post date:** [October 4, 2016, 9:05am UTC](https://forums.powershell.org/t/remotely-executing-a-program-via-a-script/7312/3 "2016-10-04T09:05:03Z")

</div>

Hi Sergiu,

Already tried that. I don’t think its trying to run the expression on the remote server, but on my workstation which doesn’t have the Cogtr.exe installed. I’m also trying to get it to work with Invoke-Command, something like

```
$MyArgs1 = "-nologo -n -c -g -f'\\dfsnamespace-Path\Preferences.xml' -m'\\dfsnamespace-Path\PPC005.mdl'"
$Session = New-PSSession -ComputerName PPCLive01.domain.co.uk -Credential $Cred -Authentication Credssp
    Invoke-Command -Session $Session -ScriptBlock {param ([string]$arguments); Cogtr.exe $arguments} -ArgumentList $MyArgs1 -asjob
Remove-PSSession $Session
```

---

<div class="post-metadata">

**Author:** ![max-kozlov](https://avatars.discourse-cdn.com/v4/letter/m/50afbb/32.png) [@max-kozlov](https://forums.powershell.org/u/max-kozlov)\
**Post date:** [October 5, 2016, 1:26am UTC](https://forums.powershell.org/t/remotely-executing-a-program-via-a-script/7312/4 "2016-10-05T01:26:58Z")

</div>

if you want to ensure your code run on remote side you may try to output `$env:ComputerName`

```
$Session = New-PSSession -ComputerName PPCLive01.domain.co.uk -Credential $Cred -Authentication Credssp
    Invoke-Command -Session $Session -ScriptBlock { $env:ComputerName } -asjob
Remove-PSSession $Session
```

You also can do directory listing

---

<div class="post-metadata">

**Author:** ![sergiu-andrei-creife](https://avatars.discourse-cdn.com/v4/letter/s/77aa72/32.png) [@sergiu-andrei-creife](https://forums.powershell.org/u/sergiu-andrei-creife)\
**Post date:** [October 5, 2016, 1:35am UTC](https://forums.powershell.org/t/remotely-executing-a-program-via-a-script/7312/5 "2016-10-05T01:35:19Z")

</div>

Hi Andy,  
Try the following:

```
$MyArgs1 = "-nologo -n -c -g -f'\\dfsnamespace-Path\Preferences.xml' -m'\\dfsnamespace-Path\PPC005.mdl'"
$Session = New-PSSession -ComputerName PPCLive01.domain.co.uk -Credential $Cred -Authentication Credssp
Enter-PSSession -Session $Session
Start-Process "PathToFile\Cotgr.exe " -join $MyArgs1
Exit-PSSession
```

Regards,  
Sergiu

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:40pm UTC](https://forums.powershell.org/t/remotely-executing-a-program-via-a-script/7312/6 "2024-05-16T20:40:13Z")

</div>

Closed on May 16, 2024, 8:40pm UTC


