Search code examples
powershellstartupfile-conversion

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-005p\204ISShare"         'maps network drive x to \\108wg-fs-05\204is
End If

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

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

'the next section maps network printers

ptrName = "\\ptfg-mp-001v\204isispcl000"                          '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

Solution

  • 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.Core\FileSystem -Name K -Root "\\108arw-fs-02\apps" -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 \\printServer\printerName
    

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