I´m in the making of a procedure script that is supposed to find out various information about a user account using the "net user" function and update this in a custom field.
What I have been able to accomplish is to get the result i want in a textfile or a variable.
The file/variable contains a lot of text followed by the actual data I´m after that looks like this:
"Password expires 2012-12-31 08:34:28" - stores in variable %lastsetdata%
By using shell command and variables I have been able to come up with the following shell command that basicly just gathers the last 20 charachters from the file/variable and stores this in a separate variable/file. The script looks like this and gives the following result:
set temp=%lastsetdata% && set temp1=%temp:~-20% && echo.%temp1% >> date.txt
Result= 2012-12-31 08:34:28 <---- correct result
Now the problem is when I´m trying to convert this to function in a Kaseya procedure I´m not getting the results I want. By using the following function in a Execute Shell command I´m getting an unexpected result which leads me to a number of questions. This is how the translated function looks like (note that %lastsetdata% has been replaced with #lastset# that contains the exact same string value.
set temp=#lastset# && set temp1=%temp:~-20% && echo.%temp1% >>#vAgentConfiguration.agentTempDir#\date.txt
Result= %temp1% <----incorrect result
I have tried to attack this from different angles aswell by using the Execute File command and call on cmd.exe with the shellcommands as a arguments to pass the executable, and putting the arguments in a bat-file and then execute the bat-file from the execute file and execute shell command. Using the command as logged on user or system makes no difference. And I´ve tried to execute the cmd string in the Live Connect Shell unsucsessfully.
Not getting the results I want....
Has anyone been able to get multiple cmd functions to work in a procedure?
Has anyone bee able to get the cmd store variable function to work in a procedure?
Sometimes there are different ways to get the same information. Try the following command line command to see what information you can get from WMI;
WMIC /OUTPUT:C:\temp\netlogin.txt NETLOGIN
To get the password expiry date value try this command;
WMIC /OUTPUT:C:\temp\netlogin.txt NETLOGIN WHERE Name="(DOMAIN)\(USERNAME)" GET PasswordExpires
Here is a Vbscript sample from my favorite Vbscript editor that you could use if you wanted to;
I find Vbscript is a lot easier to use to get specific values where the dos commands tend to give your more information then what you really want or need for script variable values.
I would write a batch file and skip the && - just do two lines in the batch file.
I ended up using a batch file.
It´s not optimal since the batch is static. This forces you to gather the information and collecting variables outside the batchfile.
For instance in a batch file i need to specify the folder where i want my output file. I cannot call on #vAgentConfiguration.agentTempDir# from the batch file etc.
Would be nice to be able to use variables in the Execute Shell Command function.
This should work:
for /F "tokens=3,4,5" %i IN ('net user %username% ^| find "Password expires"') do echo %i %j %k
It basically says:
1. Do the net user command and find any line containing Password expires
2. For each line you find, break it into tokens and return the 3rd 4th and 5th token (being the date, time and AM/PM).
If you like, you can use the above with the 'execute command shell, get results to variable' procedure step to avoid having to deal with any intermediate files (the agent will handle the output redirection and reading the content into a global variable for you).