SMS 2003 Scripting - Set Security Premissions function in VB
The following VB function block will set security premissions to an object of specified type in SMS. Note: through trial and error, i have 10 types of ObjKeys. SMS SDK 3.1 doesn't list them all.
Have fun and watch for line wrap!
Function SetSecurityPremissions(IntKey As String, ObjKey As Integer, UserName As String) As String
'IntKey = ID of object - e.g. packageID
'objkey = Type of Object
'username = Group to grant access to
'InstancePerms = Permissions to grant
'MSDN:SMS_UserInstancePermissions - Value Description for ObjectKey
'*1 Collection - 6887
'*2 Package - 15
'*3 Advertisement - 7
'4 Status Message -
'5 (Not used)
'6 Site
'*7 Query - 7
'*8 Report - 7
'*9 Software Metering Rule - 7
'*?10 Software Updates ???
Select Case ObjKey
Case "1" 'NEW Collection
InstancePerms = "6887" 'full collection rights
Case "2" 'NEW Package
InstancePerms = "15" 'full package rights
Case "3" 'NEW Advertisement
InstancePerms = "7" 'full Advertisement rights
End Select
'Spawn Instance permissions object for new Collection
Set newright = Services.Get("SMS_UserInstancePermissions").SpawnInstance_
newright.InstanceKey = IntKey
newright.ObjectKey = ObjKey
newright.UserName = UserName
newright.InstancePermissions = InstancePerms
On Error Resume Next
newright.Put_
ErrCounter = 0
Do While (Err.Number <> 0) And (ErrCounter < 3)
'Sleep for 10 seconds because WHATEVER YOU ARE DOING are being communicated to fast (WMI issues)
newHour = Hour(Now())
newMinute = Minute(Now())
newSecond = Second(Now()) + 10
waitTime = TimeSerial(newHour, newMinute, newSecond)
Application.Wait waitTime
'Set the security
Err.Number = 0
newright.Put_
ErrCounter = ErrCounter + 1
If Err.Number <> 0 Then
Application.StatusBar = "Connect failed 10 seconds delay - Loop#: " & ErrCounter & " of 3 - Error: " & Err.Number
If ErrCounter = 3 Then
Application.StatusBar = "Connect failed 3 times during set security premissions - Error: " & Err.Number
Call MsgBox("Connect failed 3 times during set security premissions - Error: " & Err.Number, vbCritical, "Setting Security Failed")
' End
End If
End If
Loop
On Error GoTo 0
Set newright = Nothing
End Function