Home » script » Page 2

Tag: script

How to License a Netscaler Device

The netscalers are license by the FLEXnet host ID of the machine.

To get this information from the device

1) SSH, putty or use the console of the netscaler device and login as the nsroot account
2) run the shell
– command: shell
3) Run the FLEXnet host ID command
– command: lmutil lmhostid -ether

4) Output will be something like

root@ns# lmutil lmhostid -ether
lmutil – Copyright (c) 1989-2007 Macrovision Europe Ltd. and/or Macrovision Corporation. All Rights Reserved.
The FLEXnet host ID of this machine is “xxxxxxxxxxxx”

5) Use this when allocating your licensing file from mycitrix.com

 

XenServer Mount USB from HOST

Situation: USB Drive attached to the XenServer host, need to mount directly into a VM for removable usb drive access.

Solution: Download the usbmount.bat from http://support.citrix.com/article/CTX118198

Edit the file with your xenserver detailsĀ  and your vm and usb uuid’s.

HOW DO I FIND MY XENSERVER VM UUID?

bring up a console session on the XenServer and run

xe vm-list

HOW DO I FIND MY XENSERVER USB UUID?

the citrix provided command for listing Removable storage didn’t return any results in my setup.

xe sr-list name-label=Removable Storage

So under the XenServer Console > Click the Removable storage node in the console > Right click and copy the UUID Displayed

 

RUNNING THE SCRIPT

Edit the below script with your details

then run

usbmount.bat ATTACH

or

usbmount DETACH

—————————————————————————————–START SCRIPT —————————————————————————————————-

@ECHO OFF
setlocal
REM Attach a USB flash device to a specified VM on XenServer 4.x

REM ————- EDIT THE OPTIONS BELOW TO SUIT YOUR ENVIRONMENT ————-

REM XenServer Credentials
SET XE_USERNAME=root
SET XE_PASSWORD=CHANGEME
SET XE_SERVER=xxx.xxx.xxx.xxx

REM Removable Storage Repository UUID
SET REMOVABLE_SR_UUID=CHANGEME

REM UUID of the VM you wish to attach the USB storage to
SET VM_UUID=CHANGEME

REM Device name/order on the VM (e.g. hdb, hdc, hdd…)
set DEVICE_NAME=hdb

REM —————————————————————————

REM XenCenter Path
SET XE_CENTER_PATH=C:program filescitrixXenCenter

REM XE Binary and Baseline Parameters
SET XE_EXEC=”%XE_CENTER_PATH%xe.exe” -s %XE_SERVER% -u %XE_USERNAME% -pw %XE_PASSWORD%

REM Temporary working file
SET TEMP_FILE=%TEMP%/xs-usbmount.tmp

REM ———— DO NOT EDIT BEYOND THIS LINE —————-

IF “%1″==”ATTACH” GOTO ATTACH_STORAGE
IF “%1″==”DETACH” GOTO DETACH_STORAGE

REM No parameters
echo Usage USBMOUNT.BAT ^<ATTACH ^| DETACH^>
GOTO END

:DETACH_STORAGE
%XE_EXEC% vdi-list sr-uuid=%REMOVABLE_SR_UUID% params=vbd-uuids –minimal > %TEMP_FILE%
SET /P VBD_UUIDS= < %TEMP_FILE%

IF “%VBD_UUIDS%”==”” GOTO NOT_BOUND
%XE_EXEC% vbd-unplug uuid=%VBD_UUIDS%
%XE_EXEC% vbd-destroy uuid=%VBD_UUIDS%
echo.Storage Detached

GOTO END

:ATTACH_STORAGE

REM See if the storage is already bound to a VBD
%XE_EXEC% vdi-list sr-uuid=%REMOVABLE_SR_UUID% params=vbd-uuids –minimal > %TEMP_FILE%
SET /P VBD_UUIDS= < %TEMP_FILE%

IF NOT “%VBD_UUIDS%”==”” GOTO ALREADY_BOUND

%XE_EXEC% vdi-list sr-uuid=%REMOVABLE_SR_UUID% params=uuid –minimal > %TEMP_FILE%
SET /P VDI_UUID= < %TEMP_FILE%

%XE_EXEC% vbd-create vm-uuid=%VM_UUID% device=%DEVICE_NAME% vdi-uuid=%VDI_UUID% –minimal > %TEMP_FILE%
SET /P VBD_UUID= < %TEMP_FILE%

%XE_EXEC% vbd-plug uuid=%VBD_UUID%

echo.VBD UUID attached as: %VBD_UUID%
GOTO END

:ALREADY_BOUND
echo.Removable storage already attached to a VM – aborting.
GOTO END

:NOT_BOUND
echo.Storage device not bound to any VMs – aborting.
GOTO END

:END
endlocal

Taking ownership of multiple folders and files

Situation
Numerous roaming profiles without the administrator added for NTFS permissions. Access was denied for copying, moving etc.

Native tools for taking ownership of multiple folders and files for end user profiles.

Command

Taking Ownership
TAKEOWN.exe /F <folderpath> /R /A /D Y

/F = path
/R=recurvise
/A=owner to administrators rather than logged on user
/D = default answer to setting list folder permission Options: Y or N

Set NTFS Permissions on multiple files
Set permissions instead
icacls.exe <folderpath> /grant administrators:F /T

To run this command on a range of folders get the list into a txt file with each entry on a single line and run

for /f %1 in (c:folders.txt) do takeown.exe /F %1 /R /A /D Y

SCRIPT: loop through users CN, tsprofile paths and userParameters

‘vbscript
Option Explicit
Dim objCommand, objConnection, strBase, strFilter, strAttributes, objUser
Dim strQuery, objRecordset, strdistinguishedName, strTSPath, strCN, objFSO,
Set objCommand = CreateObject(“ADODB.Command”)
Set objConnection = CreateObject(“ADODB.Connection”)
objConnection.Provider = “ADsDSOObject”
objConnection.Open “Active Directory Provider”
objCommand.ActiveConnection = objConnection
Const ForWriting = 2
‘…set the base DN
strBase = “<ldap://DC=domain,DC=/local>”
strFilter = “(&(objectCategory=person)(objectClass=user))”
strAttributes = “sAMAccountName,cn,distinguishedName”
strQuery = strBase & “;” & strFilter & “;” & strAttributes & “;subtree”
objCommand.CommandText = strQuery
objCommand.Properties(“Page Size”) = 100
objCommand.Properties(“Timeout”) = 30
objCommand.Properties(“Cache Results”) = False
Set objRecordSet = objCommand.Execute
Set objFSO = CreateObject(“Scripting.FileSystemObject”)
Do Until objRecordSet.EOF
  strdistinguishedName = objRecordSet.Fields(“distinguishedName”).Value
  Set objUser = GetObject(“LDAP://” & strdistinguishedName)
  On error resume next
 wscript.echo “User:” & objUser.cn & vbtab &  “UserParameters:” & objUser.userParameters & vbtab & ” TSProfilePath:” & objUser.TerminalServicesProfilePath
 ‘ & objUser.sAMAccountName _
  ‘& “,” & objUser.TerminalServicesProfilePath & “,” & objUser.profilePath _
 ‘ & “,” & objUser.homeDirectory & “,” &objUser.ScriptPath
 ‘ objRecordSet.MoveNext
  objRecordSet.MoveNext
Loop
objConnection.Close

SCRIPT: query server uptime output to text file

‘vbscript
‘list computers to check in com,puters.txt in same directory script is run from
‘will write server uptime to uptime.txt in same directory
rfile = “computers.txt”
wfile = “Uptime.txt”

on error resume next
Const ForReading = 1
Const ForWriting = 2
Const ForAppending = 8

Set objFSO = CreateObject(“Scripting.FileSystemObject”)
Set r = objFSO.OpenTextFile(rfile, ForReading, True)
Set w = objFSO.OpenTextFile(wfile, ForWriting, True)
arycomp = split(r.readall, vbcrlf)
r.close

for each pc in arycomp
Set objWMIService = GetObject(“winmgmts:\” & pc & “rootcimv2”)
dtmBootup = “”
dtmLastBootupTime = “”
dtmSystemUptime = “”
Set colOperatingSystems = objWMIService.ExecQuery(“Select * from Win32_OperatingSystem”)
For Each objOS in colOperatingSystems
dtmBootup = objOS.LastBootUpTime
dtmLastBootupTime = WMIDateStringToDate(dtmBootup)
dtmSystemUptime = DateDiff(“d”, dtmLastBootUpTime, Now)
‘how many days since the last reboot.
‘w.writeline “SYSTEM UPTIME”
w.writeline pc & vbtab & “Up Time (Days): ” & dtmSystemUptime & vbtab & vbtab & “last reboot time: ” & dtmLastBootupTime
‘WScript.Echo pc & vbtab & “UpTime (Days): ” & dtmSystemUptime & vbtab & vbtab & “last reboot time: ” & dtmLastBootupTime
dtmBootup = “”
dtmLastBootupTime = “”
dtmSystemUptime = “”
Next
objWMIService = Nothing
colOperatingSystems = Nothing
next
w.writeline “EOF”
Function WMIDateStringToDate(dtmBootup)
WMIDateStringToDate = CDate(Mid(dtmBootup, 5, 2) & “/” & _
Mid(dtmBootup, 7, 2) & “/” & Left(dtmBootup, 4) _
& ” ” & Mid (dtmBootup, 9, 2) & “:” & _
Mid(dtmBootup, 11, 2) & “:” & Mid(dtmBootup, _
13, 2))
‘uncomment the belowline in order to see the actual time and date the computer was last restarted.
‘wscript.echo WMIDateStringToDate
End Function

SCRIPT: check if folder is empty

‘vbscript
Option Explicit
Dim oFS : Set oFS = CreateObject(“Scripting.FileSystemObject”)
Function FolderEmpty(strFolderPathName)
 Dim oFiles, oFile, oFolder, oSubFolders, oSubFolder
 Dim blnFileFound : blnFileFound = False
 Set oFolder = oFS.GetFolder(strFolderPathName)
 Set oFiles = oFolder.Files
 If oFiles.Count > 0 Then
  FolderEmpty = False
  Exit Function
 End If
 Set oSubFolders = oFolder.SubFolders
 For Each oSubFolder In oSubFolders
  If Not FolderEmpty(oSubFolder.Path) Then
   FolderEmpty = False
   Exit Function
  End If
 Next
 FolderEmpty = True
End Function
Dim strFolderPathName1 : strFolderPathName1 = “file://server/c$/folder
If FolderEmpty(strFolderPathName1) Then
 MsgBox “the folder ” & strFolderPathName1 & ” is empty”
Else
 MsgBox “the folder ” & strFolderPathName1 & ” is not empty”
End If

SCRIPT: check Xen servers logons enabled

‘vbscript
const HKEY_LOCAL_MACHINE = &H80000002
Dim oReg, strKeyPath, strValueName, strDisabled, objArgs, strComputer, arrComputers
Set objArgs = WScript.Arguments
arrComputers = Array(“server1″,”server2”)

sOutput = “Logons Enabled(0) or Disabled(1)” & VBCRLF & VBCRLF
For EACH strComputer in arrComputers
 Set oReg=GetObject(“winmgmts:{impersonationLevel=impersonate}!\” & strComputer & “rootdefault:StdRegProv”)
 strKeyPath = “SoftwareMicrosoftWindows NTCurrentVersionWinlogon”
 strValueName = “WinStationsDisabled”
 oReg.GetStringValue HKEY_LOCAL_MACHINE,strKeyPath,strValueName,strDisabled
 sOutput = sOutput & strcomputer & ”    ” & strDisabled & vbcrlf

On Error Resume Next               
Next
        wscript.echo sOutput