I was creating a powershell script that finds the location of an office program executable in the registry of an rd server host and uses that to publish the application with the path (that exists on every rd server host):
[cmdletbinding()] Param( [Parameter(Mandatory=$true)]$alias,[Parameter(Mandatory=$true)]$collection,[Parameter(Mandatory=$false)]$broker=[System.Net.Dns]::GetHostByName(($env:computerName)).HostName,[Parameter(Mandatory=$false)][bool]$webaccess = $true,[Parameter(Mandatory=$false)][bool]$associate = $false,[Parameter(Mandatory=$false)]$fp,[Parameter(Mandatory=$false)]$friendlyname,[Parameter(Mandatory=$false)]$group) $friendlyname="'"+"$alias Rapp"+"'" $group="GG_RAP_$alias" $groupspec= $env:USERDNSDOMAIN + "\$group" $groups=@($groupspec) $x=(Get-RDserver -role RDS-RD-SERVER -ConnectionBroker $broker) #all registry ops need to be done on one of the rd servers $rdh=($x[(get-random -Maximum $x.length)]).Server $alli=invoke-command -ComputerName $rdh -ScriptBlock {gp -Path HKLM:\Software\RegisteredApplications} $ToI=($alli.PSObject.Properties |Where-Object {$_.Name -Like "*$alias.*" -or $_.Name -Like "* $alias *" -or $_.Name -Like "$alias"}) $capabilities=$ToI.Value $filep=invoke-command -ComputerName $rdh -ScriptBlock {if (!($fp)) { $hkc='HKLM:\Software\Classes\' + $args[0].Name + '\CLSID' $clsid=(gp $hkc).'(default)' $app="HKLM:\SOFTWARE\WOW6432Node\Classes\CLSID\$clsid\LocalServer32" if ($clsid) { $fp=(gp $app).'(default)' #gives in a error if alias is not found #separate on parameters $fp=($fp -split " /")[0] } else { write-output "please specify filepath" exit } write-output $fp }} -Args $ToI Write-Host "The following settings will be used to publish $alias : filepath $filep, usergroups $groups" $filep=[string]$filep New-RDRemoteApp -CollectionName $collection -Alias $alias -FilePath $filep -DisplayName $friendlyname -UserGroups $group -ShowInWebAccess:1 -ConnectionBroker $broker -Verbose
When executing this it invariably returns (for example):
"New-RDRemoteApp : The specified AppPath, "C:\Program Files\Microsoft Office\Office16\POWERPNT.EXE", is not valid. Specify a valid file path."
But when I execute the exact same command (placing write-host before the command in the script and copying the written command to the command shell), it works (for example):
New-RDRemoteApp -CollectionName FARM1 -Alias Powerpoint -FilePath "C:\Program Files\Microsoft Office\Office16\POWERPNT.EXE" -DisplayName 'Powerpoint Rapp' -UserGroups GG_RAP_Powerpoint -ShowInWebAccess:1 -ConnectionBroker BROKERFQDN -Verbose VERBOSE: Fetching FTAs and Icon contents from endpoint: SOMEHOSTFQDN CollectionName Alias DisplayName FilePath ShowIn CommandLin RequiredC UserGroups WebAcc eSetting ommandLin ess e -------------- ----- ----------- -------- ------ ---------- --------- ---------- FARM1 Powerpoint Powerpoint Rapp C:\Program Files\Microsoft Of... True DoNotAllow {OCMW\GG_RAP_Powerpo...
This is on a full server 2012 R2 deployment of RDS.
How come this does not work in a script?
Kind regards,
Leander Quintelier