Connect to Administrative Shares using credentials from 1Password

Connect to normal and administrative shares like \\hostname\c$ with a credential from 1Password.

It will:

  • open each share in a new tab
  • reuse the credential across all connections
  • assume a hostname is an administrative share and convert it from hostname to \\hostname\c$

The intended use case is calling it from Powershell, with a function that's loaded by your profile. This function could be called by a different function that looks up a users machine from their name.

Example profile function

function Enter-NetworkShare
{
    [alias("ens")]
    [CmdletBinding()]
    param
    (
        [Parameter(Mandatory = $true, Position = 0, ValueFromPipeline = $true)]
        [ValidateNotNullorEmpty()]
        # designed for passing the entire share like `\\hostname\c$` but the Directory Opus script
        # is fine with receiving just a hostname cause it'll wrap the rest around if it's missing
        [string[]]$Shares
    )
    
    process {
        $SharesList = @()
        $Shares | Foreach-Object {
            $_ -match "^(?:\\\\|)([-a-zA-Z0-9]+)(?:\\|$)" | Out-Null
            $name = $Matches[1]
            $Matches.Clear()
            
            if( Test-Connection -Quiet -Count 1 -ComputerName $name )
            {
                $SharesList += $_
            }
            else
            {
                Write-Host "Couldn't reach $name" -ForegroundColor Red
            }
        }
        
        $SharesString = $SharesList -join ','
        & $directoryOpusRTPath /cmd:active ConnectToNetworkShares /SHARES $SharesString /CREDENTIALNAME "Optional Credential Name"
    }
}

Enter-NetworkShare -Shares "\\hostname\c$","hostname2"

Requirements

  • 1Password CLI must be installed and configured
  • Replace "Default 1Password Credential Name Here" with the default credential name to use

This is my first time sharing anything so I hope I got the format right.
I thought about splitting this out into separate commands for re-usability, but that would make sharing harder.

ConnectToNetworkShares_1.0.0.ouc (12.5 KB)