Automation and scripting for windows has been around ever since windows became home windows. First there have been batch data, then VBscript (VBS) and now PowerShell. certain, there were some others alongside the way but, for the most part, decent ol' batch, VBS and PowerShell had been the mainstays. through the years, scripts have turn into not best less difficult to write but greater powerful as well. You not need to be a developer to be aware windows scripting with PowerShell. here's first-rate news for IT professionals however not every person is up to snuff on the latest windows PowerShell cmdlets. For people simply dipping their toe in the PowerShell pool, it's critical to have a shallow end to slowly wade into as an alternative of pushing into the deep end head first.
The PowerShell "shallow end" is demonstrating the historical formulation of automating home windows (batch and VBS) vs. the brand new formulation (PowerShell). It's helpful to peer side-by means of-facet comparisons in preference to announcing "Discard that batch and VBS skills you've discovered over a pair many years. It's nugatory now." reasonably, display an apples-to-apples assessment of various problem-fixing options and how to solve them in each and every language. When shown aspect-by way of-side like this the most effective method will certainly show via.
Let's get started through specializing in file management. File administration is a common floor that each one methods have journey with. We'll go over a couple of hobbies activities when managing info the usage of each and every components to let you decide which is the simplest. We'll start from fundamental initiatives comparable to managing single files all the way to making a CSV file of a folder's contents. Pay close attention as the tougher the ideas get the more you'll see batch data and VBscript code spoil down whereas PowerShell will keep a constant ease of use.
First up is managing a single file. in this instance, each and every alternative seems equivalent in syntax other than the all the time imprecise VBscript.
Batch:
reproduction C:\MyFile.txt C:\SomeOtherFolder
movement C:\MyFile.txt C:\SomeOtherFolder
del C:\MyFile.txt
VBscript:
Set objFs = CreateObject("Scripting.FileSystemObject")
objFs.CopyFile "C:\MyFile.txt", "C:\SomeOtherFolder\"
objFs.MoveFile "C:\MyFile.txt", "C:\SomeOtherFolder\"
objFs.DeleteFile "C:\MyFile.txt"
Powershell:
copy-item -direction 'C:\MyFile.txt' -destination 'C:\SomeOtherFolder'
stream-merchandise -course 'C:\MyFile.txt' -destination 'C:\SomeOtherFolder'
get rid of-item -route 'C:\MyFile.txt'
subsequent up is getting the contents in a folder. once more, batch and PowerShell are both one-liners however what's up with VBscript? Now you recognize why so many VBscripters had fun when PowerShell got here alongside.
Batch:
dir C:\
VBscript:
Set objFs = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFs.GetFolder("C:\")
Set colFiles = objFolder.data
For each objFile in colFiles
Wscript.Echo objFile.name
next
Powershell:
Get-ChildItem -course C:\
You likely already know how to listing information in a folder, but what if we only want some info that in shape a specific sample?
Batch:
dir C:\*somestringinsideafile*
VBscript:
Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")
Set colFiles = objWMIService.ExecQuery("opt for * from CIM_DataFile the place identify LIKE 'C:\%somestringinsideafile%\'")
For each and every objFile in colFiles
Wscript.Echo objFile.FileName
subsequent
Powershell:
Get-ChildItem -course C:\*somestringinsideafile*
once more, batch and PowerShell are very identical but VBscript goes off the rails with complexity as a result of we're forced to make use of WMI.
ultimately, let's definitely put each and every system to the look at various and add some complexity. in this example, I need to locate the file names in a folder and then generate a record for my manager with these file names and the file size interior a straightforward to study CSV file.
Batch:
(for %F in (C:\*) do @echo "%~dpF","%~nxF",%~zF)>>files.csv
VBscript:
Set objFs = CreateObject("Scripting.FileSystemObject")
Set objFile = objFs.CreateTextFile("C:\info.csv")
Wscript.sleep(2000) 'Pause while file is being created
Set objFile = objFs.GetFile("C:\files.csv")
Const ForWriting = 2
Set outputFile = objFs.OpenTextFile("C:\info.csv", ForWriting)
Set objFolder = objFs.GetFolder("C:\")
Set colFiles = objFolder.files
For every objFile in colFiles
OutputFile.Write "C:\," & objFile.name & "," & objFile.measurement
Wscript.Echo "C:\," & objFile.identify & "," & objFile.size
next
OutputFile.shut
PowerShell:
Get-ChildItem -course C:\ -File | select-Object listing,identify,length | Export-Csv C:\csv.csv -Append -NoTypeInformation
in case you're nevertheless scripting with batch files or VBscript this side-by way of-facet evaluation should still have resonated with you. it can display that:
VBscript's return on time determining a way to automate whatever is bad compared to batch or PowerShell.
that you may take what you've learned scripting with batch info and easily move these advantage over to PowerShell.
As script complexity raises, batch, and especially VBscript's code, complexity increases exponentially vs with PowerShell most effective slightly.
make a decision for your self. Which language is easier to read? which would be less difficult to make use of to your day-to-day projects? and finally, which language is getting more help at the moment? when you answer these questions, the language of choice should be clear.
No comments:
Post a Comment