Windows - Map Network Drive

Octopus.Script exported 2014-05-19 by bobjwalker belongs to ‘Windows’ category.

Map a network drive.

Parameters

When steps based on the template are included in a project’s deployment process, the parameters below can be set.

Drive letter

DriveLetter

Enter a drive letter. Example: H:

Share/server path

MapPath

Enter the share path. Example: \server\share

Persist the mapping?

MapPersist

Persist the map. Example: True or False

User to map drive as

MapUser

Enter a user to map the drive as. Example: DOMAIN\user

Password for the user

MapPass

Enter the password for the user. This should be bound to a secure variable. Example: #{MappingPassword}

Script body

Steps based on this template will execute the following PowerShell script.

#http://gallery.technet.microsoft.com/scriptcenter/Powershell-Map-utility-to-444c1c95
function Map ($computer){ 
 
function GetDriveType($DriveCode) { 
    switch ($DriveCode) 
        { 0 {"Unknown"}  
        1 {"No root directory"}  
        2 {"Removable Disk"}  
        3 {"Local Disk"}  
        4 {"Network Drive"}  
        5 {"Compact Disk"}  
        6 {"RAM Disk"}  
        } # end switch 
    } # end function GetDriveType 
 
if ($computer -eq $null) {$computer='localhost'} 
Get-WmiObject -Class win32_logicaldisk -ComputerName $computer | select DeviceID, VolumeName, ` 
    @{n='DriveType'; e={GetDriveType($_.driveType)}}, ` 
    @{n='Size';e={"{0:F2} GB" -f ($_.Size / 1gb)}}, `     
    @{n='FreeSpace';e={"{0:F2} GB" -f ($_.FreeSpace / 1gb)}} | Format-Table 
 
} 

$map = new-object -ComObject WScript.Network
if (!(Test-Path $DriveLetter))
{
	$map.MapNetworkDrive($DriveLetter, $MapPath, $MapPersist, $MapUser, $MapPass)
	Write-Host "Drive $DriveLetter mapped to $MapPath as user $MapUser."
}
else
{
    Write-Host "Drive $DriveLetter already in use."
}

Map .

Provided under the Apache License version 2.0.

Report an issue

To use this template in Octopus Deploy, copy the JSON below and paste it into the Library → Step templates → Import dialog.

{
  "Id": "34a67091-bb67-46fa-90cf-ff2d09f24732",
  "Name": "Windows - Map Network Drive",
  "Description": "Map a network drive.",
  "Version": 3,
  "ExportedAt": "2014-05-19T20:21:10.373+00:00",
  "ActionType": "Octopus.Script",
  "Author": "bobjwalker",
  "Parameters": [
    {
      "Name": "DriveLetter",
      "Label": "Drive letter",
      "HelpText": "Enter a drive letter. Example: _H:_",
      "DefaultValue": null,
      "DisplaySettings": {
        "Octopus.ControlType": "SingleLineText"
      }
    },
    {
      "Name": "MapPath",
      "Label": "Share/server path",
      "HelpText": "Enter the share path.  Example: _\\\\server\\share_",
      "DefaultValue": null,
      "DisplaySettings": {
        "Octopus.ControlType": "SingleLineText"
      }
    },
    {
      "Name": "MapPersist",
      "Label": "Persist the mapping?",
      "HelpText": "Persist the map.  Example: _True_ or _False_",
      "DefaultValue": null,
      "DisplaySettings": {
        "Octopus.ControlType": "SingleLineText"
      }
    },
    {
      "Name": "MapUser",
      "Label": "User to map drive as",
      "HelpText": "Enter a user to map the drive as.  Example: _DOMAIN\\user_",
      "DefaultValue": null,
      "DisplaySettings": {
        "Octopus.ControlType": "SingleLineText"
      }
    },
    {
      "Name": "MapPass",
      "Label": "Password for the user",
      "HelpText": "Enter the password for the user. This should be bound to a secure variable.  Example: _#{MappingPassword}_",
      "DefaultValue": null,
      "DisplaySettings": {
        "Octopus.ControlType": "SingleLineText"
      }
    }
  ],
  "Properties": {
    "Octopus.Action.Script.ScriptBody": "#http://gallery.technet.microsoft.com/scriptcenter/Powershell-Map-utility-to-444c1c95\nfunction Map ($computer){ \n \nfunction GetDriveType($DriveCode) { \n    switch ($DriveCode) \n        { 0 {\"Unknown\"}  \n        1 {\"No root directory\"}  \n        2 {\"Removable Disk\"}  \n        3 {\"Local Disk\"}  \n        4 {\"Network Drive\"}  \n        5 {\"Compact Disk\"}  \n        6 {\"RAM Disk\"}  \n        } # end switch \n    } # end function GetDriveType \n \nif ($computer -eq $null) {$computer='localhost'} \nGet-WmiObject -Class win32_logicaldisk -ComputerName $computer | select DeviceID, VolumeName, ` \n    @{n='DriveType'; e={GetDriveType($_.driveType)}}, ` \n    @{n='Size';e={\"{0:F2} GB\" -f ($_.Size / 1gb)}}, `     \n    @{n='FreeSpace';e={\"{0:F2} GB\" -f ($_.FreeSpace / 1gb)}} | Format-Table \n \n} \n\n$map = new-object -ComObject WScript.Network\nif (!(Test-Path $DriveLetter))\n{\n\t$map.MapNetworkDrive($DriveLetter, $MapPath, $MapPersist, $MapUser, $MapPass)\n\tWrite-Host \"Drive $DriveLetter mapped to $MapPath as user $MapUser.\"\n}\nelse\n{\n    Write-Host \"Drive $DriveLetter already in use.\"\n}\n\nMap .",
    "Octopus.Action.Script.Syntax": "PowerShell"
  },
  "Category": "Windows",
  "HistoryUrl": "https://github.com/OctopusDeploy/Library/commits/master/step-templates//opt/buildagent/work/75443764cd38076d/step-templates/windows-map-network-drive.json",
  "Website": "/step-templates/34a67091-bb67-46fa-90cf-ff2d09f24732",
  "Logo": "iVBORw0KGgoAAAANSUhEUgAAAMgAAADICAMAAACahl6sAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAADNQTFRF////Da3qSsLvhtb0wur6O7zuWcfxldv2aMzyK7ftpOD3s+X48Pr+0fD7d9HzHLLr4fX8xD/OcwAAAaNJREFUeNrs3cFygjAUQFECWott1f//2sJoW6kIKEzNs+euXOmcmSSGDa8oJEmSJEmSJGmsj1W1K9cpsGD1Vr2WdToVEPC+2lYvZfpVrEW0qZpF1F+MRdRugzoNlvkiarfBPk0pT8GhWUSX2yASpDlLr2+DEJBmEY1ug6whx7N0n2b30G1QlmmxHsRYp6X76yvF9vg5RYQczq8UVURI35UiFmTgShED0p6lI1eKzCHTrxS5Qk6PZ9PLDtJ9PIsJmXWlyAky6/dAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQMJCyjltF/iO3gpJUpD8s4OAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgID8T8itwwKyhbTdMr4ha8hXUwZqhICcOgyNOIkE+V5wo4MSgr1u/fp7poO+AL8K/gL8yw0UeyRB34m9iQ/pVD8L5JYTO3NI58R+AsiEEzsW5OfE3sUe/zRwYkeGnG2g2CPS7rhjF4GKP0ZwyoldxK37kFqEL/7wU0mSJEmSJOmJ+xRgAHxZTCXGdZkfAAAAAElFTkSuQmCC",
  "$Meta": {
    "Type": "ActionTemplate"
  }
}

History

Page updated on Monday, May 19, 2014