Archive: ‘Scripts’ Category

VB script to get VirtualIP

3 comments January 26th, 2011

I built this script to get the virtual ip of the usersession as i needed to send that to the application when the user started. I built two versions of it one using mfcom, the user needs to have certain admin rights in the farm to get this going which I dont like, and the other uses WMI and the registry to figure it out. Its not perfect and should be modified to fit your situation. It will only get the last sessions virtualip, as thats all i needed. So if the user has 2 sessions going you will only get the last one of them.

It is very not documented as i was in abit of rush writing it. Sorry for that! =)

** Updated with bug fix discussed in the comments thnx Augustin **

Here is the script (sesInfo.vbs):

‘ Written by Roger Eklund (roger.eklund at envokeit.com)
‘ Use at your own risk!
‘ To get the Client Virtual IP run “sesInfo.vbs ip”
‘ To get the Client hostname run “sesInfo.vbs clientname”

Const HKCR = &H80000000 ‘HKEY_CLASSES_ROOT
Const HKCU = &H80000001 ‘HKEY_CURRENT_USER
Const HKLM = &H80000002 ‘HKEY_LOCAL_MACHINE
Const HKU  = &H80000003 ‘HKEY_USERS
Const HKCC = &H80000005 ‘HKEY_CURRENT_CONFIG
Const REG_SZ        =1
Const REG_EXPAND_SZ =2
Const REG_BINARY    =3
Const REG_DWORD     =4
Const REG_MULTI_SZ  =7
Const MetaFrameWinFarmObject = 1
Const MetaFrameWinSrvObject = 6
Const Active=1
Const Connected=2
SessionID=GetSessionID

args = WScript.Arguments.Count

If args < 1 then
WScript.Echo “usage: sesInfo.vbs [ip|clientname]”
wscript.echo “Will get either VirtualIP of the client”
wscript.echo “or the Clientname”
WScript.Quit
end If

choice = ucase(wscript.arguments.item(0))

Select Case choice
Case “CLIENTNAME”
hkey=HKU
Set oReg = GetObject( “winmgmts:!root/default:StdRegProv” )
curUser = GetCurrentUser(“.”)
uSid = GetSIDFromUser(curUser)
wscript.echo vbCrLf & GetVal(hkey,uSid+”\Volatile Environment” , “CLIENTNAME”)
Case “IP”
Set oReg = GetObject( “winmgmts:!root/default:StdRegProv” )
hkey=HKLM
path = “SOFTWARE\Citrix\VIP\” & SessionID
Vip = GetVal(hkey,path , “VIPAddress”)
wscript.echo hex2ip(Vip)
Case Else
wscript.echo “omg”
End Select
Function hex2ip(hex)
Dim oct1
Dim oct2
Dim oct3
Dim oct4
Dim IPadress

If len(hex) = 7 Then
hex = “0″ & hex
end if

if len(hex) > “8″ or len(hex) < “8″ Then
wscript.echo “Not a Hex number: ” & hex
wscript.quit(0)
end if

oct1 = (Mid(hex, 1, 2))
oct2 = (Mid(hex, 3, 2))
oct3 = (Mid(hex, 5, 2))
oct4 = (Mid(hex, 7, 2))
hexNumber1 = “&H” & oct1
hexNumber2 = “&H” & oct2
hexNumber3 = “&H” & oct3
hexNumber4 = “&H” & oct4
int1 = CLng(hexNumber1)
int2 = CLng(hexNumber2)
int3 = CLng(hexNumber3)
int4 = CLng(hexNumber4)
hex2ip = int1 & “.” & int2 & “.” & int3 & “.” & int4
End Function
Function convertIP(BinIP)

Dim x
Dim Num
Dim IPNumeric
Dim IPAddy

IPNumeric = BinIp

For x = 1 To 4

Num = int(IPNumeric / 255 ^ (4 – x))
IPNumeric = IPNumeric – (Num * 255 ^ 4 – x)

If Num > 255 Then
IPConvert = “0.0.0.0″
wscript.echo(“fail” & Num)
wscript.quit
End If

If x = 1 Then
IPAddy = Num
Else
IPAddy = IPAddy & “.” & Num
End If

Next
convertIP=IPAddy

End Function
Function GetVal( strHive, strTree, strKey )

‘ Read all keys and their values for the entire section into an array
If oReg.EnumValues( strHive, strTree, sKeys, iKeyType ) Then
GetVal = “-None-”
Else
For Count = 0 to UBound( sKeys )
‘ Select the requested key
If strKey = sKeys( Count )  Then
‘ Format the output

Select Case iKeyType( Count)
Case REG_SZ
oReg.GetStringValue strHive, strTree, sKeys( Count ), sValue
GetVal = sValue
Case REG_EXPAND_SZ
oReg.GetExpandedStringValue strHive, strTree, sKeys( Count ), sValue
GetVal = sValue
Case REG_BINARY
oReg.GetBinaryValue strHive, strTree, sKeys( Count ), aValue
GetVal =  Join( aValue,”” )
Case REG_DWORD
oReg.GetDWORDValue strHive, strTree, sKeys( Count ), lValue
GetVal = hex(lValue)
Case REG_MULTI_SZ
oReg.GetMultiStringValue strHive, strTree, sKeys( Count ), sValue
GetVal =  Join( sValue,”” )
End Select
End If
Next
GetVal = GetVal
End If
End Function

Function GetCurrentUser(sstrComputer)
On Error Resume Next
Dim sstrKeyPath,sstrCU,sstrCUD,sstrCurrentUser,sstrCurrentUserDomain
sHKEY_LOCAL_MACHINE = &H80000002
Set sobjRegistry = GetObject(“winmgmts:\\” & sstrComputer & “\root\default:StdRegProv”)
sstrKeyPath = “SOFTWARE\Microsoft\Windows NT\CurrentVersion\WinLogon”
sstrCU = “DefaultUserName”
sstrCUD = “DefaultDomainName”
sobjRegistry.GetStringValue sHKEY_LOCAL_MACHINE, sstrKeyPath, sstrCU, sstrCurrentUser
sobjRegistry.GetStringValue sHKEY_LOCAL_MACHINE, sstrKeyPath, sstrCUD, sstrCurrentUserDomain
GetCurrentUser = sstrCurrentUserDomain & “\” & sstrCurrentUser
End Function
Function GetSIDFromUser(UserName)

Dim DomainName, Result, WMIUser
If InStr(UserName, “\”) > 0 Then
DomainName = Mid(UserName, 2, InStr(2,UserName, “\”) -2)
UserName = Mid(UserName, InStr(2, UserName, “\”) + 1)
Else
DomainName = CreateObject(“WScript.Network”).UserDomain
End If
On Error Resume Next
Set WMIUser = GetObject(“winmgmts:{impersonationlevel=impersonate}!” _
& “/root/cimv2:Win32_UserAccount.Domain=’” & DomainName & “‘” _
& “,Name=’” & UserName & “‘”)
If Err = 0 Then Result = WMIUser.SID Else Result = “”
On Error GoTo 0
GetSIDFromUser = Result
End Function
Function GetSessionID()
Dim TheFarm, TheSession, TheSession2
Dim sessionID, strComputerName
SessionID = -1

Set objWSHNet = CreateObject(“WScript.Network”)
strServerName = objWSHNet.ComputerName
Set TheFarm = CreateObject(“MetaFrameCOM.MetaFrameFarm”)
theFarm.Initialize(MetaFrameWinFarmObject)
Set TheSession = theFarm.GetSession(MetaFrameWinSrvObject, strServerName, SessionID)
strSessionID = TheSession.SessionID
GetSessionID = strSessionID
End Function

Create a Set of Default Applications

No comments October 2nd, 2010

Where I work we create a default set of applications to all of our customers. So I wrote this script to do it for me. It can easily be customized to anyones needs if someone wants something similar. This is for Xenapp 6 and needs to be modified to run on Xenapp 5.

Download


cls
Write-Host
Write-Host
Write-Host "#########################################################################################"
Write-Host "## Before you run this scripte there are a few thing you need to have create before. ##"
Write-host "## 1. The officeremotedesktopusers group of the company. ##"
Write-Host "## 2. The officeusers group. ##"
write-host "## 3. a workergroup named after the company. ##"
Write-Host "## 4. And a folder named after the company in the citrix console. ##"
Write-Host "## ##"
Write-Host "## Written By Roger Eklund (gobba@sajberhagen.com) ##"
Write-Host "#########################################################################################"
Write-Host
Write-Host
Write-Host "Allright Lets Start!"
Write-Host
Write-Host "Company name:(Demo) " -NoNewline
$Company = read-host
Write-Host "Upn Suffix:(demo.org) " -NoNewline
$Upn = Read-Host
Write-Host "Server Where applications is installed:(servername) " -NoNewline
$Server = Read-Host

# Desktop
New-XAApplication -ApplicationType "ServerDesktop" -DisplayName "Desktop" -Description "$Company Desktop" -FolderPath "Applications/$Company" -BrowserName "$Company Desktop"
Add-XAApplicationWorkerGroup -BrowserName "$Company Desktop" -WorkerGroupNames $Company
Add-XAApplicationAccount -BrowserName "$Company Desktop" -Accounts "OfficeRemoteDesktopUsers@$Upn"

# Word
$icon = Get-CtxIcon "\\$Server\C$\Program Files (x86)\Microsoft Office\Office12\WINWORD.EXE" -Index 0
New-XAApplication -ApplicationType "ServerInstalled" -DisplayName "Word" -Description "$Company Word" -FolderPath "Applications/$Company" -BrowserName "$Company Word" -CommandLineExecutable '"C:\Program Files (x86)\Microsoft Office\Office12\WINWORD.EXE"' -WorkingDirectory "C:\Program Files (x86)\Microsoft Office\Office12" -EncodedIconData $icon
Add-XAApplicationAccount -BrowserName "$Company Word" -Accounts "OfficeUsers@$Upn"
Add-XAApplicationWorkerGroup -BrowserName "$Company Word" -WorkerGroupNames $Company

# Excel
$icon = Get-CtxIcon "\\$Server\C$\Program Files (x86)\Microsoft Office\Office12\excel.EXE" -Index 0
New-XAApplication -ApplicationType "ServerInstalled" -DisplayName "Excel" -Description "$Company Excel" -FolderPath "Applications/$Company" -BrowserName "$Company Excel" -CommandLineExecutable '"C:\Program Files (x86)\Microsoft Office\Office12\EXCEL.EXE"' -WorkingDirectory "C:\Program Files (x86)\Microsoft Office\Office12" -EncodedIconData $icon
Add-XAApplicationAccount -BrowserName "$Company Excel" -Accounts "OfficeUsers@$Upn"
Add-XAApplicationWorkerGroup -BrowserName "$Company Excel" -WorkerGroupNames $Company

# Outlook
$icon = Get-CtxIcon "\\$Server\C$\Program Files (x86)\Microsoft Office\Office12\OUTLOOK.EXE" -Index 0
New-XAApplication -ApplicationType "ServerInstalled" -DisplayName "Outlook" -Description "$Company Outlook" -FolderPath "Applications/$Company" -BrowserName "$Company Outlook" -CommandLineExecutable '"C:\Program Files (x86)\Microsoft Office\Office12\OUTLOOK.EXE"' -WorkingDirectory "C:\Program Files (x86)\Microsoft Office\Office12" -EncodedIconData $icon
Add-XAApplicationAccount -BrowserName "$Company Outlook" -Accounts "OfficeUsers@$Upn"
Add-XAApplicationWorkerGroup -BrowserName "$Company Outlook" -WorkerGroupNames $Company

# Powerpoint
$icon = Get-CtxIcon "\\$Server\C$\Program Files (x86)\Microsoft Office\Office12\POWERPNT.EXE" -Index 0
New-XAApplication -ApplicationType "ServerInstalled" -DisplayName "Powerpoint" -Description "$Company Powerpoint" -FolderPath "Applications/$Company" -BrowserName "$Company Powerpoint" -CommandLineExecutable '"C:\Program Files (x86)\Microsoft Office\Office12\POWERPNT.EXE"' -WorkingDirectory "C:\Program Files (x86)\Microsoft Office\Office12" -EncodedIconData $icon
Add-XAApplicationAccount -BrowserName "$Company Powerpoint" -Accounts "OfficeUsers@$Upn"
Add-XAApplicationWorkerGroup -BrowserName "$Company Powerpoint" -WorkerGroupNames $Company

# Explorer
$icon = Get-CtxIcon "C:\Windows\explorer.exe" -Index 0
New-XAApplication -ApplicationType "ServerInstalled" -DisplayName "Explorer" -Description "$Company Explorer" -FolderPath "Applications/$Company" -BrowserName "$Company Explorer" -CommandLineExecutable '"C:\Program Files\Internet Explorer\iexplore.exe" -e h:\' -WorkingDirectory "%windir%" -EncodedIconData $icon
Add-XAApplicationAccount -BrowserName "$Company Explorer" -Accounts "OfficeRemoteDesktopUsers@$Upn"
Add-XAApplicationWorkerGroup -BrowserName "$Company Explorer" -WorkerGroupNames $Company

# Internet Explorer
$icon = Get-CtxIcon "C:\Program Files\Internet Explorer\iexplore.exe" -Index 0
New-XAApplication -ApplicationType "ServerInstalled" -DisplayName "Internet Explorer" -Description "$Company Internet Explorer" -FolderPath "Applications/$Company" -BrowserName "$Company Internet Explorer" -CommandLineExecutable "C:\Program Files\Internet Explorer\iexplore.exe" -WorkingDirectory "C:\Program Files (x86)\Internet Explorer\" -EncodedIconData $icon
Add-XAApplicationAccount -BrowserName "$Company Internet Explorer" -Accounts "OfficeRemoteDesktopUsers@$Upn"
Add-XAApplicationWorkerGroup -BrowserName "$Company Internet Explorer" -WorkerGroupNames $Company