this is probably a bit tense and I'm not sure if this is possible at all.
But basically, I'm trying to create a small application which contains alot of PowerShell-code which I want to run in an easy matter.
I've managed to create everything myself and it does work. But all of the PowerShell code is manually hardcoded and this gives me a huge disadvantage.
What I was thinking was creating some sort of dynamic structure where I can read a couple of text files (possible a numerous amount of text files) and use these as the source for both the comboboxes and the richtextbox which provovides as the string used to run in PowerShell.
I was thinking something like this:
Combobox - "Choose cmdlet" - Use "menucode.txt" as source
Richtextbox - Use "code.txt" as source
But, the thing is, Powershell snippets need a few arguments in order for them to work.
So I've got a couple of comboboxes and a few textboxes which provides as input for these arguments. And this is done manually as it is right now. So rewriting this small application should also search the textfile for some keywords and have the comboboxes and textboxes to replace those keywords. And I'm not sure how to do this.
So, would this requre a whole lot of textfiles? Or could I use one textfile and separate each PowerShell cmdlet snippets with something? Like some sort of a header?
Right now, I've got this code at the eventhandler (ComboBox_SelectedIndexChanged)
If ComboBoxFunksjon.Text = "Set attribute" Then
TxtBoxUsername.Visible = True
End If
If chkBoxTextfile.Checked = True Then
If txtboxBrowse.Text = "" Then
MsgBox("You haven't choses a textfile as input for usernames")
End If
LabelAttribute.Visible = True
LabelUsername.Visible = False
ComboBoxAttribute.Visible = True
TxtBoxUsername.Visible = False
txtBoxCode.Text = "$users = Get-Content " & txtboxBrowse.Text & vbCrLf & "foreach ($a in $users)" & vbCrLf & "{" & vbCrLf & "Set-QADUser -Identity $a -ObjectAttributes #{" & ComboBoxAttribute.SelectedItem & "='" & TxtBoxValue.Text & "'}" & vbCrLf & "}"
If ComboBoxAttribute.SelectedItem = "Outlook WebAccess" Then
TxtBoxValue.Visible = False
CheckBoxValue.Visible = True
CheckBoxValue.Text = "OWA Enabled?"
txtBoxCode.Text = "$users = Get-Content " & txtboxBrowse.Text & vbCrLf & "foreach ($a in $users)" & vbCrLf & "{" & vbCrLf & "Set-CASMailbox -Identity $a -OWAEnabled" & " " & "$" & CheckBoxValue.Checked & " '}" & vbCrLf & "}"
End If
If ComboBoxAttribute.SelectedItem = "MobileSync" Then
TxtBoxValue.Visible = False
CheckBoxValue.Visible = True
CheckBoxValue.Text = "MobileSync Enabled?"
Dim value
If CheckBoxValue.Checked = True Then
value = "0"
Else
value = "7"
End If
txtBoxCode.Text = "$users = Get-Content " & txtboxBrowse.Text & vbCrLf & "foreach ($a in $users)" & vbCrLf & "{" & vbCrLf & "Set-QADUser -Identity $a -ObjectAttributes #{msExchOmaAdminWirelessEnable='" & value & " '}" & vbCrLf & "}"
End If
Else
LabelAttribute.Visible = True
LabelUsername.Visible = True
ComboBoxAttribute.Visible = True
txtBoxCode.Text = "Set-QADUser -Identity " & TxtBoxUsername.Text & " -ObjectAttributes #{" & ComboBoxAttribute.SelectedItem & "='" & TxtBoxValue.Text & " '}"
If ComboBoxAttribute.SelectedItem = "Outlook WebAccess" Then
TxtBoxValue.Visible = False
CheckBoxValue.Visible = True
CheckBoxValue.Text = "OWA Enabled?"
txtBoxCode.Text = "Set-CASMailbox " & TxtBoxUsername.Text & " -OWAEnabled " & "$" & CheckBoxValue.Checked
End If
If ComboBoxAttribute.SelectedItem = "MobileSync" Then
TxtBoxValue.Visible = False
CheckBoxValue.Visible = True
CheckBoxValue.Text = "MobileSync Enabled?"
Dim value
If CheckBoxValue.Checked = True Then
value = "0"
Else
value = "7"
End If
txtBoxCode.Text = "Set-QADUser " & TxtBoxUsername.Text & " -ObjectAttributes #{msExchOmaAdminWirelessEnable='" & value & "'}"
End If
End If
Now, this snippet above lets me either use a text file as a source for each username used in the powershell snippet. Just so you know :) And I know, this is probably coded as stupidly as it gets. But it does work! :)
It'll probably get fairly horrible to debug when you've been away from it for a year and are trying to figure out how it hangs together, but if you're doing it I'd suggest that this might be a good time to use XML, since you can then create a nice structure to the files easily that will be fairly easy to understand.
<commands>
<command>
<name>Cmd1</name>
<buttonText>NiceName</buttonText>
<parameters>
<parameter>textBox1</parameter>
<parameter>textBox2</parameter>
<parameter>comboBox1</parameter>
</parameters>
<code>your code here</code>
</command>
<command>
...
</command>
</commands>
You'd have to be careful if your script code contains any characters that can interfere with the xml though.
Related
I got some problems with my vba code.
I got a form at access with a filter.
At the moment the one filter is for start/end date and a Person.
And because i am a noob, there is an other Filter for a cost centre and the start/end date.
I want to combine both, but with the case, cost centre can be Null or not.
My "main" filter looks like this:
Private Sub Befehl51_Click()
If Nz(Me.txtvon, "") = "" Then //StartDate
MsgBox "Bitte Datumsbereich wählen!"
Exit Sub
End If
If Nz(Me.txtbis, "") = "" Then //EndDate
MsgBox "Bitte Datumsbereich wählen!"
Exit Sub
End If
Me.Filter = "[TaetigkeitsDatum] between " & Format(Nz(Me!txtvon, Date),"\#yyyy-mm-dd\#") & "
and " & Format(Nz(Me!txtbis, Date), "\#yyyy-mm-dd\#") & " And " & "[PersonalID] = " & Me.Liste0 & ""
Me.FilterOn = True
End Sub
The syntax for the cost criteria is like:
[TaetigkeitsKostenstellenIDRef] = "Kombinationsfeld145"
Thanks for help!
You're nearly there, there are just some minor SQL syntax errors.
I've written it to separate lines to increase readability and make understanding easier
Me.Filter = "[TaetigkeitsDatum] BETWEEN " & Format(Nz(Me!txtvon, Date),"\#yyyy-mm-dd\#") & " AND " & Format(Nz(Me!txtbis, Date), "\#yyyy-mm-dd\#") & _
" AND [PersonalID] = " & Me.Liste0
If IsNumeric(Me!Kombinationsfeld145) Then
Me.Filter = Me.Filter & " AND [TaetigkeitsKostenstellenIDRef] =" & Me!Kombinationsfeld145
End If
Some of the changes I've made:
Spacing was off. Before every And and variable name, there should be a space.
The " & " between And and a variable name was unnecessary, removed that.
I've only included the comparison on Me!Kombinationsfeld145 if it's numeric. That way, if it's Null or a zero-length string, it's not included in the comparison.
I'm relatively new to VBA and trying to use a series of userforms to generate a URL that fills out a separate webform. The webform will be filled in with the information entered into the userform. The 'WEBSITE' is a link to a pre-populated webform template.
I'm having some issues with nesting the If Then statements to trigger the URL generation. Any help would be much appreciated.
If (UserForm1.Tier1 = True Or UserForm1.Tier2 = True) And UserForm1B.CheckBoxN = True Then
If UserFormN.ComboBoxNPT = January And UserFormN.ComboBoxNPG >= 1 Then
ActiveWorkbook.FollowHyperlink ("WEBSITE" & "Vendor Name: " & UserForm1.ComboBoxVN.Value & "%0A" & UserFormN.Label119.Caption & ": " & UserFormN.ComboBoxNPG.Value)
ElseIf UserFormN.ComboBoxNPT = January And UserFormN.ComboBoxNPS >= 1 Then
ActiveWorkbook.FollowHyperlink ("WEBSITE" & "Vendor Name: " & UserForm1.ComboBoxVN.Value & "%0A" & UserFormN.Label120.Caption & ": " & UserFormN.ComboBoxNPS.Value)
End If
End If
I am a newbie and am making a userform that inputs data into a letter. There is a string that I need to bold and underline.
With .Bookmarks("bidDate").Range.InsertBefore
.Text = inputBidM & " " & inputBidD & ", " & inputBidY
.Font.Bold = True
.Font.Underline = True
End With
I found this http://computer-programming-forum.com/1-vba/8e9aacaf425425ad.htm to get what I have above but am getting a Compile Error: Invalid or Unqualified reference.
Any help is much appreciated.
Edit: Here is the code for that section. I have everything else commented out.
Private Sub startButton_Click()
' Inserting Addendum Info from fill in boxes
With ActiveDocument
.Bookmarks("addenDate").Range.InsertBefore inputAddenM & " " & inputAddenD & ", " & inputAddenY
.Bookmarks("addenDateA").Range.InsertBefore inputAddenM & " " & inputAddenD & ", " & inputAddenY
.Bookmarks("contractNo").Range.InsertBefore inputContractNo
.Bookmarks("contractNoA").Range.InsertBefore inputContractNo
.Bookmarks("fapNo").Range.InsertBefore inputFAPNo
.Bookmarks("descrip").Range.InsertBefore inputDescrip
.Bookmarks("addenNo").Range.InsertBefore inputAddenNo
.Bookmarks("addenNoA").Range.InsertBefore inputAddenNo
.Bookmarks("addenNoB").Range.InsertBefore inputAddenNo
.Bookmarks("bidDate").Range.Text inputBidM & " " & inputBidD & ", " & inputBidY
.Font.Bold = True
.Font.Underline = True
End With
End Sub
The With keyword lets you perform multiple actions on an object without needing to type the object out for each line. The code you show could be written without the With and End With by putting ActiveDocument in front of each .Bookmarks line. You can see how this saves time typing and it can be easier to read :-)
Since you want to do more with the last bookmark than just insert text, you can add (nest) another With...End With to let you work with that Range, something like this:
Private Sub startButton_Click()
' Inserting Addendum Info from fill in boxes
With ActiveDocument
.Bookmarks("addenDate").Range.InsertBefore inputAddenM & " " & inputAddenD & ", " & inputAddenY
.Bookmarks("addenDateA").Range.InsertBefore inputAddenM & " " & inputAddenD & ", " & inputAddenY
.Bookmarks("contractNo").Range.InsertBefore inputContractNo
.Bookmarks("contractNoA").Range.InsertBefore inputContractNo
.Bookmarks("fapNo").Range.InsertBefore inputFAPNo
.Bookmarks("descrip").Range.InsertBefore inputDescrip
.Bookmarks("addenNo").Range.InsertBefore inputAddenNo
.Bookmarks("addenNoA").Range.InsertBefore inputAddenNo
.Bookmarks("addenNoB").Range.InsertBefore inputAddenNo
With .Bookmarks("bidDate").Range
.Text = inputBidM & " " & inputBidD & ", " & inputBidY
.Font.Bold = True
.Font.Underline = True
End With
End With
End Sub
The following does work:
Sub test()
With Selection
.GoTo What:=wdGoToBookmark, Name:="bidDate"
.Text = "blah blah blah"
.Font.Bold = True
.Font.Underline = True
End With
End Sub
A colon before .Bookmarks is only possible when another With statement was used. That is why you are getting the error message.
My goal is to return the results of 5 textboxes into a SQL query, by incorporating the Query string with the variables.
How can I get my code to function so that when a checkbox is checked, the value (eg: ID, SC...) is recorded and placed into a Query? And if a checkbox is not checked, then it is not placed into the query.
The 5 checkboxes are as follows:
The code I current have to record whether a textbox is selected, and to place the value (eg: ID, SC, AS...) into a variable is as follows:
If (Me.BoxID = False) And (Me.BoxSC = False) And (Me.BoxASSC = False) And (Me.BoxAS = False) And (Me.BoxEH = False) Then
MsgBox "You must check a Fonction Checkbox", 0
Exit Sub
Else
If (Me.BoxID= True) Then IDValue = Chr(34) & "ID" & Chr(34) Else IDValue = """"""
If (Me.BoxSC= True) Then SCValue = Chr(34) & "SC" & Chr(34) Else SCValue = """"""
If (Me.BoxASSC= True) Then ASSCValue = Chr(34) & "ASSC" & Chr(34) Else ASSCValue = """"""
If (Me.BoxAS= True) Then ASValue = Chr(34) & "AS" & Chr(34) Else ASValue = """"""
If (Me.BoxEH= True) Then EHValue = Chr(34) & "EH" & Chr(34) Else EHValue = """"""
End If
fonctionQryString = "(((tblF.f1)=" & IDValue & ") OR " + "((tblF.f1)=" & SCValue & ") OR " + "((tblF.f1)=" & ASSCValue & ") OR " + "(tblF.f1)=" & ASValue & ") OR " + "(tblF.f1)=" & EHValue & ")))"
The fonctionQryString goes into the WHERE section of the SQL Query.
I know that the method I'm using is not efficient, even though it works.
My problem is that I don't know how to do this another way. I want my code to function so that when a checkbox is not checked, it doesn't go into the Query string.
Any help would be much appreciated.
These two WHERE clauses should produce equivalent results. Consider switching to the second form.
WHERE tblF.f1 = "ID" OR tblF.f1 = "SC" OR tblF.f1 = "AS"
WHERE tblF.f1 IN ("ID","SC","AS")
Here is a rough and untested code sample to produce a similar WHERE clause based on my understanding of what you're trying to achieve.
Dim fonctionQryString As String
Dim lngLoopNum As Long
Dim strControlName As String
Dim strValueList As String
For lngLoopNum = 1 To 5
Select Case lngLoopNum
Case 1
strControlName = "ID"
Case 2
strControlName = "SC"
Case 3
strControlName = "ASSC"
Case 4
strControlName = "AS"
Case 5
strControlName = "EH"
End Select
If Me.Controls("Box" & strControlName) = True Then
strValueList = strValueList & "," & Chr(34) & _
strControlName & Chr(34)
End If
Next
If Len(strValueList) > 0 Then
fonctionQryString = "tblF.f1 IN (" & Mid(strValueList, 2) & ")"
Else
MsgBox "You must check a Fonction Checkbox"
End If
I assumed you didn't actually want to include the condition, WHERE tblF.f1 = "" (an empty string). If I guessed wrong, you'll have more work to do, but hopefully this will still point you to something useful.
I'm trying to move my entire User folder in Vista to a non-system partition. To do so with a minimum hassle I'm following the directions provided at Ben's Blog, specifically the vbs script he provides. However executing the script throws up an error which I can't resolve myself. Here's the vbs code followed by the text file it calls on, and finally my error message. Can someone help me correct the problem? (I really don't know much about VBS, so please write as simple as possible.)
VBS Code:
'# Perform dir /a c:\users > c:\dir.txt
'# place this script file in c:\ too
'# double click to run it
'# run resulting script.bat from recovery mode
repprefix = " Directory of..." ' Modify to your language
sourcedrive = "C:\"
targetdrive = "D:\"
altsourcedrive = "C:\" 'leave same as target drive unless otherwise indicated
alttargetdrive = "E:\" 'leave same as target drive unless otherwise indicated
inname = "dir.txt"
outname = "script.bat"
userroot = "Users"
set fso = CreateObject("Scripting.FileSystemObject")
' construct batch commands for saving rights, then link, the recreating rights
Function GetCommand(curroot, line, typ, keyword)
' first need to get source and target
pos = Instr(line, keyword) + Len(keyword)
tuple = Trim(Mid(line, pos))
arr = Split(tuple, "[")
oldtarget = Replace(arr(1), "]", "")
oldlink = curroot & "\" & Trim(arr(0))
' need to determine if we are pointing back to old disk
newlink = replace(oldlink, sourcedrive, targetdrive)
if(Instr(oldtarget, sourcedrive & userroot)) then
newtarget = Replace(oldtarget, sourcedrive, targetdrive)
else
newtarget = oldtarget ' still pointing to original target
end if
' comment
out = "echo " & newlink & " --- " & newtarget & vbCrLf
' save permissions
out = out & "icacls """ & replace(oldlink, sourcedrive, altsourcedrive) & """ /L /save " & altsourcedrive & "permissions.txt" & vbCrLf
' create link
newlink = replace(newlink, targetdrive, alttargetdrive)
if typ = "junction" then
out = out & "mklink /j """ & newlink & """ """ & newtarget & """" & vbCrLf
else ' typ = "symlink"
out = out & "mklink /d """ & newlink & """ """ & newtarget & """" & vbCrLf
end if
'set hidden attribute
out = out & "attrib +h """ & newlink & """ /L" & vbCrLf
' apply permissions
shortlink = Left(newlink, InstrRev(newlink, "\") - 1) 'icacls works strangely - non-orthogonal for restore
out = out & "icacls """ & shortlink & """ /L /restore " & altsourcedrive & "permissions.txt" & vbCrLf
GetCommand = out & vbCrLf
End Function
Sub WriteToFile(file, text)
ForWriting = 2
Create = true
set outfile = fso.OpenTextFile(file, ForWriting, Create)
Call outfile.Write(text)
Call outfile.Close()
End Sub
outtext = "ROBOCOPY " & altsourcedrive & userroot & " " & alttargetdrive & userroot & " /E /COPYALL /XJ" & vbCrLf & vbCrLf
set intext = fso.OpenTextFile(inname)
while not intext.AtEndOfStream
line = intext.ReadLine()
if Instr(line, repprefix) then
curroot = Replace(line, repprefix, "")
elseif Instr(line, juncname) then
outtext = outtext & GetCommand(curroot, line, "junction", juncname)
elseif Instr(line, linkname) then
outtext = outtext & GetCommand(curroot, line, "symlink", linkname)
end if
Wend
outtext = outtext & "icacls " & altsourcedrive & userroot & " /L /save " & altsourcedrive & "permissions.txt" & vbCrLf
outtext = outtext & "ren " & altsourcedrive & userroot & " _" & userroot & vbCrLf
outtext = outtext & "mklink /j " & altsourcedrive & userroot & " " & targetdrive & userroot & vbCrLf
outtext = outtext & "icacls " & altsourcedrive & " /L /restore " & altsourcedrive & "permissions.txt"
Call intext.Close()
Call WriteToFile(outname, outtext)
MsgBox("Done writing to " & outname)
dir.txt:
Volume in drive C is ACER
Volume Serial Number is 08D7-C0CC
Directory of c:\users
07/16/2009 12:29 PM <DIR
07/16/2009 12:29 PM <DIR> ..
11/02/2006 09:02 AM <SYMLINKD> All Users [C:\ProgramData]
11/02/2006 09:02 AM <DIR> Default
11/02/2006 09:02 AM <JUNCTION> Default User [C:\Users\Default]
08/21/2008 08:37 AM 174 desktop.ini
11/02/2006 08:50 AM <DIR> Public
07/19/2009 08:54 PM <DIR> Steve
1 File(s) 174 bytes
7 Dir(s) 5,679,947,776 bytes free
Error Message:
Windows Script Host
Script: C:\userlocationchange.vbs
Line: 25
Char: 2
Error: Subscript out of range: '[number: 1]'
Code: 800A0009
Source: Microsoft VBScript runtime error
The problem is at these lines:
arr = Split(tuple, "[")
oldtarget = Replace(arr(1), "]", "")
I assume that arr(1) is giving the error because arr has only one entry - and since arrays are zero-based in VBS, that entry should be accessed as arr(0).
Hmmm... if there's only one entry, then presumably no "[" was found. The code probably needs to check for that (by testing whether UBound(arr) > 1).
What that means in a wider context - i.e. why there's no "[" I can't say.
EDIT: OK, I took a look at the blog you referred to, and the exact same problem was reported. The blog author replied:
A couple of pointers: look in the txt
file output by the dir command. On my
system, the targets of the symlinks
are shown in square brackets [].
Apparently in your case there aren't
any - in any case that is a hypothesis
that would explain why the script
can't parse out the link targets.
...which pretty much confirms my theory. I suggest you do as he suggests and take a look at the txt file to see if some other character is used.
Note that this isn't really a problem with the script per se - it's just that the script expects some input that it's not getting.
#Gary, I'm the same one who reported the problem on that blog.
I posted the txt file here under the VBS code. My txt file also has the symlink-junction targets within square brackets. Is there anything else I'm missing?