Kaseya Community

Script Variables

  • Hi guys,

    I am not sure if this is possible but if I dont ask I wont know.

    I have a VBscript that has variables in it like

    username, password & domain

    is there anyway from within the kaseya script I can specifiy what the username, password & domain is and it then is pass that information to a the VB script?

    The only possible way I can think of it having a script for each client with the password & username already specefied which kind of defeats the purpose.

    Thanks

    Josh

    Legacy Forum Name: Script Variables,
    Legacy Posted By Username: Joshua Lehman
  • A few thoughts things... Windows stores the current username and domain as %USERNAME% and %USERDOMAIN%, so it's easy to reference these on a shell command line if you want. Passwords... no such luck.

    One approaches that come to mind is to use managed variables (reference the Kaseya help for details). Basically, you can define three managed variables for the user, password and domain name, then save the values of these managed variables on a per client basis (assuming your clients are represented as a unique machinegroup). Then you can use the IMPERSONATE USER function at the top of the script, and pop in the managed variables for the three fields you are trying to use.

    As long as you save the variables for the machinegroup, you can run the script against it.

    I know this doesn't answer your question directly... but hopefully it'll give you an idea to think about. The bottom line, getting the username and domain is easy... the password is the killer.

    Legacy Forum Name: Scripts Forum,
    Legacy Posted By Username: dbennett
  • dbennett
    A few thoughts things... Windows stores the current username and domain as %USERNAME% and %USERDOMAIN%, so it's easy to reference these on a shell command line if you want. Passwords... no such luck.

    One approaches that come to mind is to use managed variables (reference the Kaseya help for details). Basically, you can define three managed variables for the user, password and domain name, then save the values of these managed variables on a per client basis (assuming your clients are represented as a unique machinegroup). Then you can use the IMPERSONATE USER function at the top of the script, and pop in the managed variables for the three fields you are trying to use.

    As long as you save the variables for the machinegroup, you can run the script against it.

    I know this doesn't answer your question directly... but hopefully it'll give you an idea to think about. The bottom line, getting the username and domain is easy... the password is the killer.


    Hmm thanks for your reply I will look more into the manage variables im sure they will help with other things as well..

    Basically what I am trying to do is create a script to remove a machine from a domain and the vbscript sample script I have is

    ' Unjoin a Computer from a Domain



    Const NETSETUP_ACCT_DELETE = 2 'Disables computer account in domain.
    strPassword = "password"
    strUser = "kenmyer"

    Set objNetwork = CreateObject("WScript.Network")
    strComputer = objNetwork.ComputerName

    Set objComputer = GetObject("winmgmts:{impersonationLevel=Impersonate}!\\" & _
    strComputer & "\root\cimv2:Win32_ComputerSystem.Name='" & strComputer & "'")
    strDomain = objComputer.Domain
    intReturn = objComputer.UnjoinDomainOrWorkgroup _
    (strPassword, strDomain & "\" & strUser, NETSETUP_ACCT_DELETE)


    As you can see you need to specify the username & password in this script.
    Would it be possible to modify the script so that it uses the credentials of the user that runs the script, if so then I could use the Impersonate user command to by pass the issue.

    Legacy Forum Name: Scripts Forum,
    Legacy Posted By Username: Joshua Lehman
  • try something like this:
    I use this script to run a command on all the fixed disk drives of an agent. The command to execute is supplied via the command line when calling the script (as in the example)

    The first 15 lines or so is what you are interested in.


    'Usage: Replaces \/drive\/ with the drive letter in your argument and runs it on every fixed disk drive.

    'Example: EnumCommandOnFixedDiskDrives.vbs defrag -o -a -drive=\/drive\/

    'Example: EnumCommandOnFixedDiskDrives.vbs dir -r \/drive\/:\*.*

    On Error Resume Next

    If WScript.Arguments.Count <> 1 Then

    WScript.Echo "Improper usage: EnumCommandOnFixedDiskDrives.vbs [str(Command)]"

    WScript.Quit

    End If

    strSubstitute = "\/drive\/"

    'assign arguments

    strCommand = WScript.Arguments.Item(0)

    'check for replacement

    If (InStr(1,strCommand,strSubstitute)

    WScript.Echo "Substitute string:" & strSubstitute & ", not found in argument"

    WScript.Quit

    End If

    'Drive types

    Const DriveTypeRemovable = 1

    Const DriveTypeFixed = 2

    Const DriveTypeNetwork = 3

    Const DriveTypeCDROM = 4

    Const DriveTypeRAMDisk = 5

    'Filesystem Modes

    Const ForReading = 1

    Const ForWriting = 2

    Const ForAppending = 8

    'initialise objects

    Set FSO = CreateObject("Scripting.FileSystemObject")

    Set Drives = FSO.Drives

    Set WshShell = WScript.CreateObject("WScript.Shell")

    'Set objFSO = CreateObject("Scripting.FileSystemObject")

    'If objFSO.FileExists("enumlog.txt") Then

    ' objFSO.DeleteFile("enumlog.txt")

    'End If

    'objFSO.CreateTextFile "enumlog.txt", True

    Set objFileEnum = objFSO.OpenTextFile("enumlog.txt", ForAppending)

    For each Drive in Drives

    If Drive.DriveType = 2 Then

    If Drive <> "B:" Then

    'Run the command with substitution here

    strFinalCommand = ReplaceSubstituteWithDrive(strSubstitute,strCommand,Drive)

    'objFileEnum.WriteLine "final command: " & strFinalCommand

    'strReturn = WshShell.Run("""strFinalCommand""" ,0, true)

    WshShell.Run("""" & strFinalCommand & """" ,0, true)

    'objFileEnum.WriteLine strReturn

    End If

    End If

    Next

    'objFileEnum.Close





    Function ReplaceSubstituteWithDrive(strSubstitute,strStringToReplace,strDriveLetter)

    If InStr(1,strDriveLetter,":") Then

    strDriveLetter=Left(StrDriveLetter,1)

    End If



    strResult = Replace(strStringToReplace,strSubstitute,strDriveLetter)

    If InStr(1,strResult,strSubstitute) Then

    strResult = ReplaceSubstitueWithDrive(strSubstitute,strResult,strDriveLetter)

    End If

    ReplaceSubstituteWithDrive = strResult

    End Function


    Legacy Forum Name: Scripts Forum,
    Legacy Posted By Username: mparr
  • For multiple variables:

    Dim strUser, strPassword, strDomain

    StrUser = wscript.arguments(0)
    StrPassword = wscript.arguments(1)
    StrDomain = wscript.arguments(2)
    ]


    You just have to remember that you need to call the script in the right order.


    cscript /nologo #temp#\myScript.vbs Fred.lastname "Freds password" FredDomain


    If you aren't the only one to use the script(documentation wise) you can also look here: http://www.microsoft.com/technet/scriptcenter/resources/qanda/may06/hey0505.mspx

    This enforces arguments and ensures they are in the correct order.

    Legacy Forum Name: Scripts Forum,
    Legacy Posted By Username: ryan.odwyer