I was trying to create a mapped network drive in my Windows 10 Hyper-V VM through an "Invoke-Command" from my host.

PS C:\Users\YChan14\Desktop> $session = New-PSSession -VMName "Win10" -Credential $mycreds -Name "NetworkDriveMapping"
PS C:\Users\YChan14\Desktop> invoke-command -Session $session -ScriptBlock { param($myhostcreds) New-PSDrive -Name “Z” -PSProvider FileSystem -Root “\\SLB-GYFXMV2\SharingToVM” -Credential $myhostcreds -Persist -Scope Global} -ArgumentList $myhostcreds

Name           Used (GB)     Free (GB) Provider      Root                                                                CurrentLocation PSComputerName
----           ---------     --------- --------      ----                                                                --------------- --------------
Z                 355.84        118.47               \\SLB-GYFXMV2\SharingToVM                                                           Win10

PS C:\Users\YChan14\Desktop> invoke-command -Session $session -ScriptBlock { Get-PSDrive Z} -ArgumentList $mydrivecreds

Name           Used (GB)     Free (GB) Provider      Root                                                                CurrentLocation PSComputerName
----           ---------     --------- --------      ----                                                                --------------- --------------
Z                 355.70        118.62               \\SLB-GYFXMV2\SharingToVM                                                           Win10

where $mycreds is the credential for the VM and $myhostcreds is the credential for the mapped drive.

As you can see, I'm able to create the mapped network drive in the session and see it:

PS C:\Users\YChan14\Desktop> Enter-PSSession -session $session
[Win10]: PS C:\Users\sdrwcs\Documents> Get-PSDrive Z

Name           Used (GB)     Free (GB) Provider      Root                                                                                                                CurrentLocation
----           ---------     --------- --------      ----                                                                                                                ---------------
Z                 355.85        118.47 FileSystem    \\SLB-GYFXMV2\SharingToVM

However, if I go directly into my VM and do the same thing:

PS C:\Users\sdrwcs\Documents> Get-PSDrive Z
Get-PSDrive : Cannot find drive. A drive with the name 'Z' does not exist.
At line:1 char:1
+ Get-PSDrive Z
+ ~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (Z:String) [Get-PSDrive], DriveNotFoundException
    + FullyQualifiedErrorId : GetLocationNoMatchingDrive,Microsoft.PowerShell.Commands.GetPSDriveCommand

I'm not able to find the mapped drive and either from File Explorer.

After evaluating the Microsoft documentation on New-PSDrive, I tried putting these into a PS1 script add-network-drive.ps1 and dot source the script:

$creds = "vmpwd"
$username = "sdrwcs"
$secpasswd = ConvertTo-SecureString $creds -AsPlainText -Force
$mycreds = New-Object System.Management.Automation.PSCredential ($username, $secpasswd)

$host_creds = "hostpwd"
$host_username = "YChan14"
$host_secpasswd = ConvertTo-SecureString $host_creds -AsPlainText -Force
$host_mycreds = New-Object System.Management.Automation.PSCredential ($host_username, $host_secpasswd)

$session = New-PSSession -VMName "Win10" -Credential $mycreds -Name "NetworkDriveMapping"

invoke-command -Session $session -ScriptBlock { param($host_mycreds) New-PSDrive -Name "Z" -PSProvider FileSystem -Root "\\SLB-GYFXMV2\SharingToVM" -Credential $host_mycreds -Scope Global -Persist} -ArgumentList $host_mycreds
PS C:\Users\YChan14\Desktop> . .\add-network-drive.ps1

Name           Used (GB)     Free (GB) Provider      Root                                                                CurrentLocation PSComputerName
----           ---------     --------- --------      ----                                                                --------------- --------------
Z                 354.95        119.37               \\SLB-GYFXMV2\SharingToVM                                                           Win10

But I'm still not able to access this mapped drive in my VM:

PS C:\Users\sdrwcs\Documents> Get-PSDrive Z
Get-PSDrive : Cannot find drive. A drive with the name 'Z' does not exist.
At line:1 char:1
+ Get-PSDrive Z
+ ~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (Z:String) [Get-PSDrive], DriveNotFoundException
    + FullyQualifiedErrorId : GetLocationNoMatchingDrive,Microsoft.PowerShell.Commands.GetPSDriveCommand

Does anyone have any idea on how to create a persistent mapped drive that I can access through file explorer in the VM from a ps1 script executed from the host?

I would really appreciate the help!

Thanks

0

There are 0 answers