In a direct sense you can instantiate COM objects in PowerShell like:
$objFSO = New-Object -ComObject "Scripting.FileSystemObject"
$objNetwork = New-Object -ComObject "wscript.Network"
So the same properties and methods are then available...
Note: While common in VBScript, Hungarian notation, ie str...
or obj...
is generally frowned upon.
Above is a direct translation, however I think in the longer term it's better to emulate the same functionality using more native PowerShell functionality.
You can use the Test-Path
cmdlet Documented here to test the presence of a drive. It will return a Boolean, so an alternative might look something like:
If( -not ( Test-Path 'k:' ) ) {
# Do something
}
The PowerShell native way to map a drive is with the New-PSDrive
cmdlet. Documentation here
Example:
New-PSDrive -PSProvider Microsoft.PowerShell.CoreFileSystem -Name K -Root "\108arw-fs-02apps" -Persist
You can declare arrays in a similar fashion:
$Printers = "Printer1", "Printer2" # Etc...
Also note the array subexpression can be used:
$Printers = @( "Printer1", "Printer2" ) # Etc...
For adding printers you can use the Add-Printer
cmdlet, documented here
Example:
Add-Printer -ConnectionName \printServerprinterName
There are several looping constructs to choose from. You can research them all in the PowerShell documentation, but the ForEach
construct looks like:
ForEach( $Printer in $Printers) {
# Map your printer...
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…