#31190 - 2002-10-22 01:50 PM
Help with WriteProfileString
|
funkester
Fresh Scripter
Registered: 2002-10-21
Posts: 12
|
Hi,
Please can someone help me use the WriteProfileString function? I am trying to create a list of desktop machines on my domain and I'm having difficulties getting WriteProfileString to insert more than one entry in the file that should contain the list of machine names.
I have written the following code and it just overwrites the same entry each time the code is run:-
code:
$Target=@Wksta $IniFile="\\Alpha\Allshare\CLIST.INI"
If Exist($IniFile) = 1 $result=WriteProfileString($IniFile,'Workstations',"Name",'$Target') EndIf
BTW hope I've written this msg correctly, I'm new to this forum ;-)
Kind Regards, Greg
|
|
Top
|
|
|
|
#31192 - 2002-10-22 01:55 PM
Re: Help with WriteProfileString
|
Howard Bullock
KiX Supporter
   
Registered: 2000-09-15
Posts: 5809
Loc: Harrisburg, PA USA
|
You have the computer name in the wrong place.
$result=WriteProfileString($IniFile,'Workstations',$Target,'')
or
$result=WriteProfileString($IniFile,$Target,'','')
|
|
Top
|
|
|
|
#31193 - 2002-10-22 02:06 PM
Re: Help with WriteProfileString
|
funkester
Fresh Scripter
Registered: 2002-10-21
Posts: 12
|
Hi Guys,
Thanks for the swift response, your help is greatly appreciated :-)
I tried using the following code as suggested:-
code:
$result=WriteProfileString($IniFile,$Target,'','')
However, this just returns a blank file? All I want is a list of machines. I want the name of the machine recorded to a file on my server when that machine is logged in.
Is WriteProfileString the right way to do this?
Kind Regards, Greg
|
|
Top
|
|
|
|
#31195 - 2002-10-22 02:19 PM
Re: Help with WriteProfileString
|
funkester
Fresh Scripter
Registered: 2002-10-21
Posts: 12
|
The final output I wanted was a list of machine names line by line, i.e. you open the file to see this:
machine1 machine2 machine3 machine4
and so on. I want to ensure that machine names are appended to this list each time the login script runs. I also want to avoid duplicates in the list.
Thanks again. Greg
|
|
Top
|
|
|
|
#31198 - 2002-10-22 02:35 PM
Re: Help with WriteProfileString
|
funkester
Fresh Scripter
Registered: 2002-10-21
Posts: 12
|
I was only using an INI because it had been recommended to me to use one in an earlier post. If I wanted a list of machine names in a file what else should I use?
BTW Lonkero: Your WriteProfileString works, but I was hoping to have just machine names without dates etc, is this possible?
Many thanks for all your help.
|
|
Top
|
|
|
|
#31200 - 2002-10-22 02:45 PM
Re: Help with WriteProfileString
|
funkester
Fresh Scripter
Registered: 2002-10-21
Posts: 12
|
Maybe it would help if I explained why I want this list, then you can tell me if I'm going about it the correct way?
I want a list of machine names so that I can use this list to run Bryce's SU_Install.kix script.
What I'm hoping for is to use the list of machine names to be able to install the SUSS service to allow me to remotely run installations on the workstations that require admin privilges.
I want to use Kix to do this, not the Task Scheduler method.
Thanks again. Greg
|
|
Top
|
|
|
|
#31203 - 2002-10-22 02:59 PM
Re: Help with WriteProfileString
|
funkester
Fresh Scripter
Registered: 2002-10-21
Posts: 12
|
OK, that is a very good point. Let me go back a stage further and ask how you would do the following:
The reason for wanting to be able to remotely install programs that require admin privileges is that I have to rollout an Antivirus application to all our dekstop pc's. I thought, rightly or wrongly, that the best way to do this would be via the login script as I wanted to catch all machines when they logon.
Then I came up against the problem that our W2k desktop users do not have local admin rights, therefore, I cannot run the AV install through the login script without increasing the rights somewhere. This is where I got the idea of using SU, but if the word is that it is a security risk, then I can't use that solution either?
Can anyone help suggest another way I might achieve what I'm trying to do here?
Sorry if I sound stupid, and I'm not trying to annoy anyone. I'm just after some advice from some more experienced admins.
Kind Regards, Greg.
|
|
Top
|
|
|
|
#31204 - 2002-10-22 03:04 PM
Re: Help with WriteProfileString
|
Sealeopard
KiX Master
   
Registered: 2001-04-25
Posts: 11165
Loc: Boston, MA, USA
|
Greg:
The main problem is that you cannot have empty entries in a .INI file. The following is correct:
code:
[name] key=value
whereas
code:
[name]
orcode:
[name] value
would be incorrect.
you must therefore restructure your .INI file structure accordingly. The following would for example be a good way to have a list of computers and at the same time keep track of the SU install
code:
[SU Install] COMP1=0 COMP2=1 COMP3=1 COMP4=0
which would indicate that COMP2 and COMP2 have SU installed. The corresponsding code fragment would look like this:
code:
$suinstalled=val(trim(readprofilestring('su.ini','SU Install',@WKSTA))) if not $suinstalled ; run the SU install routine $rc=writeprofilestring('su.ini,'SU Install',@WKSTA,1) endif
and the login script could for example contain
code:
$suinstalled=val(trim(readprofilestring('su.ini','SU Install',@WKSTA))) if not $suinstalled $rc=writeprofilestring('su.ini,'SU Install',@WKSTA,0) endif
in case you want to keep the collection and the installation separate.
Also, please take the time to read the KiXtart Manual as both READPROFILESTRING and wRITEPROFILESTRING are explained extensively in it.
You might want to take a look at thread about the Task Scheudler or the ScheudleTask() UDF, or read the FAQs, e.g. Installing an Application as an Admin [ 22. October 2002, 15:07: Message edited by: sealeopard ]
_________________________
There are two types of vessels, submarines and targets.
|
|
Top
|
|
|
|
#31206 - 2002-10-22 03:23 PM
Re: Help with WriteProfileString
|
Howard Bullock
KiX Supporter
   
Registered: 2000-09-15
Posts: 5809
Loc: Harrisburg, PA USA
|
If you want to write a file of computer names:
computer1 computer2 computer3 etc...
Use WriteLog2() in the UDF Library as step #1 in your logon script.
WriteLog2("\\server\share\@wksta.","")
Then use this code to gather up all the filenames and write them to a file after some period of time. this will let all the computers create a file.
code:
$Files=WshPipe("%comspec% /c dir /s /b \\server\share",1) for each $file in $Files WriteLog2("c:\computers.txt",$file) next
WSHPIPE is a UDF that you can find in the UDF Library also.
This is almost exactly what you need. code:
WriteLog2("c:\data\scripts\fasttrack\@wksta","") $Files=WshPipe("%comspec% /c dir /s /b c:\data\scripts\fasttrack",1) for each $file in $Files $file = substr($file,instrrev($file,"\")+1) ? $file next
;Function WshPipe() ; ;Author Christopher Shilt (christopher.shilt@relizon.com) ; ;Version 1.1 ; ;Version History ; ; 14 June 2002 Version 1.1 ; Removed ReadAll Function and incorporated it into WshPipe. Added ; $WshErrorMsg to return the actual error message generated by the ; shelled command. ; ; 14 June 2002 Version 1.0 ; ;Action Performs a shell-like command and pipes the output to an array and returns the ; exitcode of the command to the @error macro. ; ;Syntax WshPipe(COMMAND, optional NOECHO) ; ;Parameters ; COMMAND : REQUIRED. The "shell" command you want to perform. ; ; NOECHO : OPTIONAL. Suppress the command's output to the screen, ouput ; is still stored in an array. ; ;Remarks ; ; ;Returns Output of shell command in an array, exitcode of the command in the @error ; macro, and the error message of the command to $WshErrorMsg. By default, the output ; is echoed to the screen but can be suppressed. ; ;Dependencies KiX 4.02 ; WSH 5.6 (Included with Microsoft Internet Explorer 6.0. Also available for download ; from Microsoft's MSDN website.) ; ;Example: ; ; Display all KiX files in C:\ directory: ; $rc=WshPipe("%comspec% /c dir c:\*.kix") ; ? "Exit Code: " @error " " $WshErrorMsg ; ; ; Display all KiX files in C:\ directory, but suppress output to screen: ; $rc=WshPipe("%comspec% /c dir c:\*.kix",1) ; ? "Exit Code: " @error " " $WshErrorMsg ; ; ; Display all KiX files in C:\ directory, suppress output to screen. Then use FOR/NEXT ; ; to exclude data. ; $rc=WshPipe("%comspec% /c dir c:\*.kix",1) ; for each $line in $rc ; if not instr($line, "File Not Found") ; ? $line ; endif ; next ; ? "Exit Code: " @error " " $WshErrorMsg ; ;Source Function WshPipe($ShellCMD, OPTIONAL $NoEcho) Dim $WshShell, $oExec, $AllOutput, $Exit, $WshExitCode $WshErrorMsg="" $WshShell=CreateObject("WScript.Shell") $oExec=$WshShell.Exec($ShellCMD) While $Exit<>1 Dim $Output Select Case Not $oExec.StdOut.AtEndOfStream $Output=$oExec.StdOut.ReadAll Case Not $oExec.StdErr.AtEndOfStream $Output=$oExec.StdErr.ReadAll $WshErrorMsg = $Output Case 1 $Output=-1 EndSelect If $Output=-1 If $oExec.Status=1 $Exit=1 Endif Else If $NoEcho<>1 ? $Output Endif $AllOutput = $AllOutput + $Output Endif Loop $WshExitCode=$oExec.ExitCode $WshPipe=split($AllOutput,chr(10)) Exit($WshExitCode) EndFunction
;-------------------------------------------------------------------------------------------------- ;FUNCTION WriteLog2() ; ;AUTHOR Howard A. Bullock (hbullock@tycoelectronics.com) ; ;ACTION Generic logging facility for scripts. Appends log entry to a file with an ; optional TimeStamp. ; ;SYNTAX WriteLog2($File, $text, [0|1]) ; ;PARAMETERS $File (Required) - String value ; $text (Required) - String value ; $TimeStamp (Optional) Default(0) no TimeStamp (1 or 0) ; ; ;REMARKS This function writes (appends) an optionally time stamped log entry to the file ; defined in function. This function searches for the first unused file handle, ; open the file, and write the entry. The file handle is then closed. When the ; function is unable to perform its it write the error is displayed in a message box. ; ;RETURNS Nothing ; ;DEPENDENCIES None ; ;EXAMPLES WriteLog2("junk.txt","This is a test") ; WriteLog2("junk.txt","This is a test",0) ; WriteLog2("junk.txt","This is a test",1) ; ; Function WriteLog2($File, $Text, optional $TimeStamp) dim $RC, $File, $text, $FH, $TimeStamp $FH=1 $RC=Open ($FH, $File, 5) while $RC = -3 $FH=$FH +1 $RC=Open ($FH, $File, 5) Loop Select Case $RC=0 if ($TimeStamp=1) $TimeStamp = @Date + " " + @Time + " - " else $TimeStamp = "" endif $RC=Writeline ($FH, $TimeStamp + $Text + @CRLF) $RC=Close ($FH) Case $RC=-2 $text = "WriteLog2: Invalid file handle ($FH) specified when trying to Open $File." $RC=MessageBox ($text,"Script Error",48) Case $RC=-1 $text = "WriteLog2: Invalid file name ($File) specified for log file." $RC=MessageBox ($text,"Script Error",48) Case $RC=>0 $text = "System Error($RC) while attempting to open log file ($File)." $RC=MessageBox ($text,"Script Error",48) Endselect EndFunction ;--------------------------------------------------------------------------------------------------
code:
[ 22. October 2002, 15:32: Message edited by: Howard Bullock ]
|
|
Top
|
|
|
|
#31208 - 2002-10-22 03:36 PM
Re: Help with WriteProfileString
|
funkester
Fresh Scripter
Registered: 2002-10-21
Posts: 12
|
WOW is all I can say. Thanks for the help, I'll get cracking with this now. Thanks again guys, sorry if I seem like a newbie-moron !!!
Kind Regards, Greg.
|
|
Top
|
|
|
|
Moderator: Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart
|
1 registered
(Allen)
and 1198 anonymous users online.
|
|
|