function UnPublish-CitrixServer{ Param([string]$server) Begin{ function gcs{ Param($srv) $type = [System.Type]::GetTypeFromProgID("MetaframeCOM.MetaframeServer",$Server) $mfServer = [system.Activator]::CreateInstance($type) $mfServer.Initialize(6,$Server) $mfServer } function cUnPublish { Param([string]$Srv) $Srv = $Srv.toUpper() $mfSrv = gcs $srv If($mfSrv.Applications.Count -gt 0) { Write-Host "Removing All Published Applications from $Srv" -ForegroundColor Red Write-Host "===================================================" -ForegroundColor Green ForEach($a in $mfSrv.Applications) { $myApp = $a.AppName Write-Host "Removing App [$myApp] from Server [$Srv]" -ForegroundColor White $a.RemoveServer($Srv) $a.SaveData() } } else { Write-Host "No Published Applications for $Srv" -ForegroundColor Red } } Write-Host $process = @() } Process{ if($_){ if($_.ServerName) { $process += $_.ServerName } else { $process += $_ } } } End{ if($Server){$Process += $Server} foreach($s in $process){ cUnPublish $s Write-Host } } }
File: RemoveAllAppsFromAServer.wsf Description: Exanmple of Remove App from a Server . Requirements: WSH 5.5 or higher. Remove All Applications from a Citrix server . CScript //nologo RemoveAllAppsFromAServer.wsf SERVER1 Dim theFarm, ServerName ' If no parameters specified, quit and ' show the usage of the script ' if WScript.Arguments.Count <> 1 Then WScript.Echo "USAGE: RemoveAllAppsFromAServer.wsf ServerName" WScript.Echo "" WScript.Echo "Example: RemoveAllAppsFromAServer.wsf SERVER1" WScript.Quit 0 End If ' ' Create MetaFrameFarm object ' Set theFarm = CreateObject("MetaFrameCOM.MetaFrameFarm") if Err.Number <> 0 Then WScript.Echo "Can't create MetaFrameFarm object" WScript.Echo "(" & Err.Number & ") " & Err.Description WScript.Echo "" WScript.Quit Err.Number End if ' ' Initialize the farm object. ' theFarm.Initialize(MetaFrameWinFarmObject) if Err.Number <> 0 Then WScript.Echo "Can't Initialize MetaFrameFarm object" WScript.Echo "(" & Err.Number & ") " & Err.Description WScript.Echo "" WScript.Quit Err.Number End if ' ' Are you Citrix Administrator? ' if theFarm.WinFarmObject.IsCitrixAdministrator = 0 then WScript.Echo "You must be a Citrix admin to run this script" WScript.Echo "" WScript.Quit 0 End If For Each aServer in theFarm.Servers If (aServer.ServerName = UCase(ServerName)) Then For Each anApp In aServer.Applications anApp.LoadData(1) anApp.RemoveServer(ServerName) anApp.SaveData Next End If Next if Err.Number <> 0 Then WScript.Echo "Can't Remove app from server" WScript.Echo "(" & Err.Number & ") " & Err.Description WScript.Echo "" WScript.Quit Err.Number End if WScript.Echo "Removed Apps successfully"
I'm rather new to PoSh, and having a little trouble with a slightly reworked version of Brandon's script. I'm hoping someone can help.
Here's what I've got:
Param([string]$Server) Write-Host $Server = $Server.toUpper() $MFSrv = New-Object -ComObject MetaFrameCOM.MetaFrameServer $MFSrv.Initialize(6,"$Server") If($MFSrv.Applications.Count -gt 0) { Write-Host "Removing All Published Applications from $Server" Write-Host "===================================================" ForEach($App in $MFSrv.Applications) { $PubApp = $App.BrowserName ; modified the MFCOM object for PS4.5 Write-Host "Removing PUblished Application $PubApp from Server $Server" $App.RemoveServer($Server) $App.SaveData() } } else { Write-Host "No Published Applications for $Server" }
The script does manage to enumerate the applications, but throws an error when it gets to the RemoveServer action:
At P:\Server Administration\Scripting\PowerShell\RemoveAllApps.ps1:12 char:27+ $App.RemoveServer( <<<< $Server)Exception calling "SaveData" with "0" argument(s): "Exception from HRESULT: 0x820D0002"At P:\Server Administration\Scripting\PowerShell\RemoveAllApps.ps1:13 char:23+ $App.SaveData( <<<< )
I've tried it a bunch of different ways with no luck. Any ideas?
$App.RemoveServer($Server.ToUpper())
Hi Mike,
Thanks for your reply, but still no. In fact, I'm more confused because the statement looks perfect to me.
I added the LoadData method before RemoveServer, and the output is now:
Missing ')' in method call.
At :line:12 char:18
+ $App.LoadData(f <<<< alse)
The original poster was looking for a script; however, perhaps these free apps are useful to someone:
http://www.citrixtools.nethttp://sbc.vanbragt.net/mambo/index.php?option=com_content&task=view&id=523&Itemid=49http://sbc.vanbragt.net/mambo/index.php?option=com_content&task=view&id=749&Itemid=49http://sbc.vanbragt.net/mambo/index.php?option=com_content&task=view&id=221&Itemid=49
Alan Osborne
President (MCSE, CCNA, VCP, CCA)
VCIT Consulting - Citrix/Terminal Services Remote Desktop Solutions for SMB
VCIT website My Blog
Hey Mike,
Believe it or not, I'm still struggling with this. Do you (or perhaps anyone out there) have any other recommendations?
try $app.loaddata(0) instead of false.
also what version of Citrix is this?
Sorry, meant to post that off the top, but I apparrently forgot.
XA4.5 R03, running on W2K3 SP2 (mainly, a few SP1 servers still).
did you try the $app.loaddata(0) ?
also put a sleep right after removeserver and after savedata, like this
$App.RemoveServer($Server)
Start-Sleep –milliseconds 2000
$App.SaveData()
Sorry for multiple postings, here is the script i want you to try. Loaddata call should be set to true instead of false, as you are changing the application properties. Looks like for 4.5 LoadData call MUST be made, where as CPS 4.0 versions this wasn't necessary.
This line also looks like need to be modified , enclosed in Quotes.
Write-Host "Removing PUblished Application $PubApp from Server $Server"
Finalized script to try as below, input a test server name to test on one of your test servers, instead of passing it as argument.
$Server="ServerName"
$Server = $Server.toUpper()
$MFSrv = New-Object -ComObject MetaFrameCOM.MetaFrameServer $MFSrv.Initialize(6,"$Server") If($MFSrv.Applications.Count -gt 0) { Write-Host "Removing All Published Applications from $Server" Write-Host "===================================================" ForEach($App in $MFSrv.Applications) {
$App.LoadData(1) $PubApp = $App.BrowserName
Write-Host "Removing Published Application $PubApp from Server $Server"
Start-Sleep -milliseconds 2000
Start-Sleep -milliseconds 2000 } } else { Write-Host "No Published Applications for $Server" }