Quantcast
Channel: The Official Scripting Guys Forum! forum
Viewing all articles
Browse latest Browse all 15028

How to write to a file with vbscript via powershell

$
0
0

I have a script that will enumerate through an OU and extract the users email addresses, then check to see if the output file and the powershell file (which contains the powershell command) exists. It then executes powershell and the powershell script, but here's the problem. Everything will work fine until tries to write to the output file "outsize.txt". It creates the file but does not write anything. Also, the script keeps kicking back a run time error "Input past end of file". Here's part of the script.

Option Explicit

Dim strsmtpserver, strfromemail1, sgroups, strDelay, strsleep, strOU, WshShell, objDictionary, objOU

Dim strUser, objFSO, strusername, Stuff, objFile, strX, strCharacters, objUser

' ################

' # SETTING VARS #   ' /// Enter your email details here the rest is automated  ///

' ################

strsmtpserver = "mydomain.com"

strfromemail1 = "user@domain.com"

strDelay = 20

strsleep = 100

strOU = "OU=Users,dc=domain,dc=com"

' #############################

' # EMAIL SHELL DO NOT REMOVE #

' #############################

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

' ############################

' # GET USER ACCOUNT DETAILS #

' ############################

Set objDictionary = CreateObject("Scripting.Dictionary")

Set objOU = GetObject("LDAP://OU=Users,dc=domain,dc=com")

objOU.Filter = Array("User")

For Each objUser in objOU

strUser = objUser.Mail

If Not objDictionary.Exists(strUser) Then

Wscript.Echo strUser

End If

Next

' ############################

' ### DELETE THE OLD OUTPUT  #

Set objFSO = CreateObject("Scripting.FileSystemObject") 

If objFSO.FileExists("./outsize.txt") Then

   objFSO.DeleteFile "./outsize.txt" 

'''   msgbox "File deleted"

Else 

'''   msgbox "NO File to delete"

End If 

If objFSO.FileExists("./sizestat.ps1") Then

   objFSO.DeleteFile "./sizestat.ps1" 

'''   msgbox "File deleted"

Else 

'''   msgbox "NO File to delete"

End If 

SET objFSO = NOTHING

' #####################################

' # CREATE EXCHANGE SIZE SHELL OUTPUT #

strusername = strUser

Stuff = "(Get-MailboxStatistics " & strusername & ").TotalItemSize.Value.ToMB() | out-file -filePath ./outsize.txt -encoding ASCII"

Set objFSO = CreateObject("Scripting.FileSystemObject")

Set objFile = objFSO.createTextFile("./sizestat.ps1", 8, True)

objFile.WriteLine(Stuff)

objFile.Close

SET objFile = NOTHING

SET objFSO = NOTHING

' ###################

' ### GET THE SIZE  #

WshShell.Run "PowerShell.exe -PSConsoleFile ""C:\Program Files\Microsoft\Exchange Server\Bin\ExShell.psc1"" -Command ""./sizestat.ps1""" ,1, True

' ######################

' ### READ THE OUTPUT  #

Set objFSO = CreateObject("Scripting.FileSystemObject")

strX = 0

Do While objFSO.FileExists("./outsize.txt") = false

strX = strX + 1

    WScript.Sleep strsleep

if strX = strDelay then 

Set objFile = objFSO.createTextFile("./outsize.txt", 8, False)

exit do

End if

Loop

strCharacters = 0

Set objFile = objFSO.OpenTextFile("./outsize.txt", 1)

strCharacters = trim(objFile.ReadAll)

If trim(strCharacters) = "" or isnull(strCharacters) = true then

strCharacters = 0

End if

objFile.close

SET objFile = NOTHING

SET objFSO = NOTHING

THIS IS NOT THE ENTIRE SCRIPT, it keeps giving error before it gets past this part of the script. What am I doing wrong? Please go easy on me, I have no experience in coding or writing scripts.

Viewing all articles
Browse latest Browse all 15028

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>