No Luck Trying to Run Command Line Program

I have a - what I thought would be simple - script… but cannot figure out what I am doing wrong. It is a command line run of TeamViewer to change some account parameters…

If I run the same command in a CMD window as Administrator it works as expected.

Here is my PowerShell script:

$working_directory = "C:\Program Files (x86)\TeamViewer"
$application = "TeamViewer.exe" 
$ea_string = "--grant_easy_access"
$SiteInitials = "AAA"
$teamviwer_api_token = "12345-12342-12312323"
# it appears that the order of the parameters makes a huge difference. Be careful.
#TeamViewer.exe assign --api-token aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa --group AAA --alias "AAA-TEST" --grant-easy-access --reassign

Clear-Host
$command_string = "assign --api-token $teamviwer_api_token --group $SiteInitials --alias ""$ComputerName"" $ea_string --reassign"
Write-Output "Working directory is $working_directory"
Write-Output "Application name is $application"
Write-Output "Command Arguments = $command_string"

#attempt number one
Start-Process -FilePath $application -WorkingDirectory $working_directory -ArgumentList $command_string 
# if I add the following to the Start-Process line above I get an error...
#-RedirectStandardOutput "E:\sout.txt"

#attempt number two
$CMD = "$working_directory\$application"
$arg1 = 'assign'
$arg2 = "--api-token $teamviwer_api_token"
$arg3 = "--group $SiteInitials"
$arg4 = "--alias ""$ComputerName"""
$arg5 = "$ea_string --reassign"
& $CMD $arg1 $arg2 $arg3 $arg4 $arg5

It does not help that there is no output from TeamViewer when you run this but it seems like it is not running.

Help - please. Any thoughts?

Thanks in advance!

Paul.

 

Ok - so I figured it out - I think…

TeamViewer produces no output - which is bad…

It turns out the argument

$ea_string = "--grant_easy_access"

should have been

$ea_string = "--grant-easy-access"

with dashes…

Lesson learned - always make sure your programs produce output… shame on TeamViewer!

Still would love any feedback on the code…

The arguments parameter is a string array, so you can write your code like this:

$ComputerName        = 'Computer123'
$ea_string           = "--grant-easy-access"
$SiteInitials        = "AAA"
$teamviwer_api_token = "12345-12342-12312323"

$tvArgs = @()
$tvArgs += 'assign'
$tvArgs += '--api-token {0}' -f $teamviwer_api_token
$tvArgs += '--group {0}'     -f $SiteInitials
$tvArgs += '--alias "{0}"'   -f $ComputerName
$tvArgs += '{0} --reassign'  -f $ea_string

$params = @{
    FilePath         = 'TeamViewer.exe'
    WorkingDirectory = 'C:\Program Files (x86)\TeamViewer' 
    ArgumentList     = $tvArgs
    Wait             = $true
    ErrorAction      = 'Stop'
}

try {
    $result = Start-Process @params
}
catch {
    Throw 'Issue installing {0}. {1}' -f $params.FilePath, $_
}

# If the catch doesn't work or you are not getting a code or message in $result, you can do a check
# by setting the -Wait above and then doing a path, registry or some other check to confirm installation
if ($someCheck = $someValue) {
    'Installation was successful'
}
else {
    'Installation failed'
}

You can check the parameters:

PS C:\WINDOWS\system32> $tvargs
assign
--api-token 12345-12342-12312323
--group AAA
--alias "Computer123"
--grant-easy-access --reassign

PS C:\WINDOWS\system32> $params

Name                           Value                                                                                                                                                                                                                             
----                           -----                                                                                                                                                                                                                             
Wait                           True                                                                                                                                                                                                                              
FilePath                       TeamViewer.exe                                                                                                                                                                                                                    
ArgumentList                   {assign, --api-token 12345-12342-12312323, --group AAA, --alias "Computer123"...}                                                                                                                                                 
ErrorAction                    Stop                                                                                                                                                                                                                              
WorkingDirectory               C:\Program Files (x86)\TeamViewer