Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
350 views
in Technique[技术] by (71.8m points)

startup - how would i convert this into powershell?

on error resume next                                             'allows the script to continue if an error occurs.
Set fso = CreateObject("Scripting.FileSystemObject")             'creates scripting object
Set objNetwork = CreateObject("wscript.Network")                 'creates an object for network scripting

'the next section maps network drives if they don't already exist

If Not fso.DriveExists("z") Then                                 'checks for an existing drive x
  objNetwork.MapNetworkDrive "z:", "\ptfg-fs-005p204ISShare"         'maps network drive x to \108wg-fs-05204is
End If

If Not fso.DriveExists("Y") Then                                 'repeat of above for other drive letters
  objNetwork.MapNetworkDrive "Y:", "\108arw-fs-02global"
End If

If Not fso.DriveExists("K") Then                                 'repeat of above for other drive letters
  objNetwork.MapNetworkDrive "K:", "\108arw-fs-02apps"
End If

'the next section maps network printers

ptrName = "\ptfg-mp-001v204isispcl000"                          'creates a variable for the part of a printer name that is a constant
ptrs = array("3","4","5","6","7","8")
for each x in ptrs                                               'start of loop that will create the next printer
    addPtr = ptrName & x                                         'adds current number from loop to printer constant name
    objNetwork.addWindowsPrinterConnection addPtr                'adds printer
next                                                             'end of add printer loop

wScript.quit                                                     'ends script
question from:https://stackoverflow.com/questions/65644377/how-would-i-convert-this-into-powershell

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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...
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...