PowerShell:使用WCX檔案將使用者自動新增RemoteAPP程式捷徑

這程式我分成兩段

子程式類似Function,可以重複使用

[cc lang=”powershell”]
Param(
[parameter(Mandatory=$true,Position=0)]
[string]
$WCXPath
)

function CheckForConnection
{
Param (
[parameter(Mandatory=$true,Position=0)]
[string]
$URL
)

[string] $connectionKey = “”
[bool] $found = $false

foreach ($connectionKey in get-item ‘HKCU:\Software\Microsoft\Workspaces\Feeds\*’ 2> $null)
{

if ((Get-ItemProperty -path Registry::$connectionKey).URL -eq $URL)

{
$found = $true
break
}
}

return $found
}

# Process the bootstrap file
[string] $wcxExpanded = [System.Environment]::ExpandEnvironmentVariables($WCXPath)
[object[]] $wcxPathResults = @(Get-Item $wcxExpanded 2> $null)

if ($wcxPathResults.Count -eq 0)
{
Write-Host @”

The .wcx file could not be found.

“@

exit(1)
}

if ($wcxPathResults.Count -gt 1)
{
Write-Host @”

Please specify a single .wcx file.

“@

exit(1)
}

[string] $wcxFile = $wcxPathResults[0].FullName
[xml] $wcxXml = [string]::Join(“”, (Get-Content -LiteralPath $wcxFile))
[string] $connectionUrl = $wcxXml.workspace.defaultFeed.url

if (-not $connectionUrl)
{
Write-Host @”

The .wcx file is not valid.

“@

exit(1)
}

if ((CheckForConnection $connectionUrl))
{
Write-Host @”

The connection in RemoteApp and Desktop Connections already exists.

“@

exit(1)
}

Start-Process -FilePath rundll32.exe -ArgumentList ‘tsworkspace,WorkspaceSilentSetup’,$wcxFile -NoNewWindow -Wait

# check for the Connection in the registry
if ((CheckForConnection $connectionUrl))
{
Write-Host @”

Connection setup succeeded.

“@
}
else
{
Write-Host @”

Connection setup failed.

Consult the event log for failure information:
(Applications and Services\Microsoft\Windows\RemoteApp and Desktop Connections).

“@

exit(1)
}
[/cc]

然後外面的程式要餵給他 WCX檔案

[cc lang=”powershell”]

Start-Process powershell “XXX\Sub_RemoteAPP.ps1 D:\XXX\ABC.wcx”

[/cc]

WCX檔案其實是文字檔
重點其實是那個defaultFeed的位置,一般如果RemoteAPP架設好沒特別修改設定,通常defaultFeed都會位於網站目錄下面的/RDWeb/Feed/webfeed.aspx
<?xml version=”1.0″ encoding=”utf-8″ standalone=”yes”?>

<workspace name=”Enterprise Remote Access” xmlns=”http://schemas.microsoft.com/ts/2008/09/tswcx” xmlns:xs=”http://www.w3.org/2001/XMLSchema”>

<defaultFeed url=”https://abc.com/RDWeb/Feed/webfeed.aspx” />

</workspace>

 

這樣就可以透過powershell去幫使用者新增RemoteAPP的捷徑
當然你也可以更貼心一點直接幫他再複製到桌面
[cc lang=”powershell”]
Copy-Item “$env:APPDATA\Microsoft\Windows\Start Menu\Programs\Work Resources (RADC)\*.lnk” -Destination “$env:USERPROFILE\Desktop”
[/cc]