简体   繁体   English

将命令行参数插入vbscript

[英]Inserting command line Arguments into vbscript

Hey I have this code were I take two documents and read them. 嘿,我有这段代码,是我拿了两个文档并阅读了它们。 The two are CurrentShares and ApprovedShares . 这两个是CurrentSharesApprovedShares I compare these two and write the differences into a .txt file then I run a command to delete the shares, the command is Net Share ShareName /DELETE . 我将两者进行比较并将差异写入.txt文件,然后运行命令删除共享,该命令为Net Share ShareName /DELETE That was the whiteList section. 那是whiteList部分。

The blacklist section is fairly the same, I take the previous CurrentShares and compare it to the UnApprovedShares . 黑名单部分完全相同,我将以前的CurrentShares并将其与UnApprovedShares进行比较。 I then write the differences to a text file and delete the shares using the same command as before. 然后,将差异写入文本文件,并使用与以前相同的命令删除共享。

What I need is to seperate my script so when someone runs the script from the command line all they have to do is write ShareDelete.vbs /Whitlist or ShareDelete.vbs /Blacklist and depending on which one they type the command calls either the blacklist or whitelist verson. 我需要分开我的脚本,以便当某人从命令行运行该脚本时,他们所要做的就是编写ShareDelete.vbs /WhitlistShareDelete.vbs /Blacklist并根据他们键入的命令来调用黑名单或白名单版本。 my code is 我的代码是

Option Explicit

Function DeleteThisShare(Share)
Dim objShell
   DeleteThisShare = "net share " & Share & " /DELETE"
    Set objShell = CreateObject("Wscript.Shell")
    wscript.echo DeleteThisShare
    objShell.Run DeleteThisShare
End Function
'------------------------------------------------------------------------------

'Whitelist
Wscript.echo "Calling cleanshares.vbs /whitelist.."

'Reads Approvedshare txt and makes the txt file into an array
Public objFSO 
set objFSO = CreateObject("Scripting.FileSystemObject") 
Dim objApprovedFile 
Set objApprovedFile = objFSO.OpenTextFile ("C:\Users\a352592\Desktop\Abdullahi\Remove Shares Utility\approvedshares.txt") 
Public strApprovedFile, strApprovedData, arrApprovedLines, ApprovedLineCount, strApprovedShares
CONST ForReading = 1
strApprovedFile = ("C:\Users\a352592\Desktop\Abdullahi\Remove Shares Utility\approvedshares.txt")
strApprovedData = objFSO.OpenTextFile(Trim(strApprovedFile),ForReading).ReadAll
arrApprovedLines = Split(Trim(strApprovedData),vbCrLf)
wscript.echo strApprovedData
ApprovedLineCount = UBound(arrApprovedLines) + 1
strApprovedShares = strApprovedData
'wscript.echo "Approved share count : " &ApprovedLinecount

'Reads current shares txt and also makes that txt into an array
set objFSO = CreateObject("Scripting.FileSystemObject")
Dim objCurrentFile
Set objCurrentFile = objFSO.OpenTextFile ("C:\Users\a352592\Desktop\Abdullahi\Remove Shares Utility\currentshares.txt") 
Public strCurrentFile, strCurrentData, arrCurrentLines, CurrentLineCount, strCurrentShares, strNonApproved
strCurrentFile = ("C:\Users\a352592\Desktop\Abdullahi\Remove Shares Utility\currentshares.txt")
strCurrentData = objFSO.OpenTextFile(Trim(strCurrentFile),ForReading).ReadAll
arrCurrentLines = Split(Trim(strCurrentData),vbCrLf)
CurrentLineCount = UBound(arrCurrentLines) + 1
'wscript.echo "current share count : " &CurrentLinecount

Do until objCurrentFile.AtEndOfStream
    strCurrentShares = objCurrentFile.ReadLine
    If InStr(strApprovedShares, strCurrentShares) = 0 Then
    StrNonApproved = StrNonApproved & StrCurrentShares & vbCrLf
    End If

Loop
objApprovedFile.Close
objCurrentFile.Close

Wscript.echo "Current Shares Not approved: " & vbCrLf & strNonApproved
Set objWhiteFile = objFSO.CreateTextFile("C:\Users\a352592\Desktop\Abdullahi\Remove Shares Utility\Shares_Not_Approved.txt")
Dim objWhiteFile : objWhiteFile.WriteLine strNonApproved '"The following shares are not listed as an approved share in the approvedshares.txt file.  Removing share from the PC: " & vbCrLf & strNonApproved

Dim notapprovedShares, notapprovedLines, strnotapprovedFile
strnotapprovedFile = ("C:\Users\a352592\Desktop\Abdullahi\Remove Shares Utility\Shares_Not_Approved.txt")
notapprovedLines = objFSO.OpenTextFile(strnotapprovedFile,ForReading).ReadAll
notapprovedLines = Split(notApprovedLines, vbCrLf)

Dim x, k, Line
k = uBound(notApprovedLines)

For x = 0 To k
    Line = Trim(notApprovedLines(x))
    If Len(Line) > 0 then
        DeleteThisShare(Line)
    End If
Next


'------------------------------------------------------------------------------------------------------------
'Blacklist
'Wscript.echo "Calling cleanshares.vbs /Blacklist.."

Function DeleteThisShare(Share)
Dim objShell
   DeleteThisShare = "net share " & Share & " /DELETE"
    Set objShell = CreateObject("Wscript.Shell")
    wscript.echo DeleteThisShare
    objShell.Run DeleteThisShare
End Function


Set objCurrentFile = objFSO.OpenTextFile ("C:\Users\a352592\Desktop\Abdullahi\Remove Shares Utility\currentshares.txt")

'Reads UnApprovedShares and makes txt file into an array
Set objFso = CreateObject("Scripting.FileSystemObject")
Dim strUnApprovedFile, strUnApprovedData, arrUnApprovedLines, UnApprovedLineCount, strUnApprovedShares, strNonUnapprovedShares
strUnApprovedFile = ("C:\Users\a352592\Desktop\Abdullahi\Remove Shares Utility\unapprovedshares.txt")
strUnApprovedData = objFSO.OpenTextFile(Trim(strUnApprovedFile),ForReading).ReadAll
arrUnApprovedLines = Split(Trim(strUnApprovedData),vbCrLf)
UnApprovedLineCount = UBound(arrUnApprovedLines) + 1
strUnApprovedShares = strUnApprovedData
'wscript.echo "Unapproved Share Count: " & UnApprovedLineCount
strCurrentFile = ("C:\Users\a352592\Desktop\Abdullahi\Remove Shares Utility\currentshares.txt")

Do until objCurrentFile.AtEndOfStream
     StrCurrentShares = objCurrentFile.Readline
 If InStr(strUnApprovedShares, strCurrentShares) = 0 Then
StrNonUnapprovedShares = StrNonUnapprovedShares & strCurrentShares & vbCrLf
 End If
Loop

Wscript.echo "Current shares Not UnApproved: " & vbcrLf & StrNonUnapprovedShares
Dim objBlackFile : Set objBlackFile = ObjFSO.CreateTextFile("C:\Users\a352592\Desktop\Abdullahi\Remove Shares Utility\Shares_Not_Unapproved.txt")
objBlackFile.WriteLine StrNonUnapprovedShares

Dim notUnapprovedShares, notUnapprovedLines, strnotUnapprovedFile
strnotUnapprovedFile = ("C:\Users\a352592\Desktop\Abdullahi\Remove Shares Utility\Shares_Not_Unapproved.txt")
notUnapprovedLines = objFSO.OpenTextFile(strnotUnapprovedFile,ForReading).ReadAll
notUnapprovedLines = Split(notUnapprovedLines, vbCrLf)

Dim y, j, Line2
j = uBound(notUnapprovedLines)

For y = 0 to j
    Line2 = Trim(notUnapprovedLines(y))
    If len(Line2) > 0 Then
        DeleteThisShare(Line2)
    End If
Next

A skeleton for dealing with named arguments: 处理命名参数的框架:

Option Explicit

WScript.Quit Main()

Function Main()
  Main = 1

  Dim oWAN : Set oWAN = WScript.Arguments.Named
  Select Case True
    Case 1 <> oWAN.Count
      WScript.Echo "Usage: cscript demoargs.vbs /Whitelist OR /Blacklist"
    Case oWAN.Exists("Whitelist")
      WScript.Echo "doing Whitelist stuff"
      Main = 0
    Case oWAN.Exists("Blacklist")
      WScript.Echo "doing Blacklist stuff"
      Main = 0
  End Select
End Function

output: 输出:

cscript demoarg.vbs
Usage: cscript demoargs.vbs /Whitelist OR /Blacklist

cscript demoarg.vbs /Whitelist
doing Whitelist stuff

cscript demoarg.vbs /Blacklist
doing Blacklist stuff

cscript demoarg.vbs /Blacklist /Whitelist
Usage: cscript demoargs.vbs /Whitelist OR /Blacklist

See 看到

  1. Arguments 参数
  2. Named 命名
  3. Unnamed 无名

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM