'PowerShell Add PS-Drive to robocopy to a device outside of the domain

I'm currently creating a PowerShell script that basically maps the network drive from a NAS which is outside of the domain, but I get the following error message:

New-PSDrive : The network path was not found
...
+ New-PSDrive -Name X -PSProvider "FileSystem" -Credential $credential  ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (X:PSDriveInfo) [New-PSDrive], Win32Exception
    + FullyQualifiedErrorId : CouldNotMapNetworkDrive,Microsoft.PowerShell.Commands.NewPSDriveCommand  

This is the following code:

$nasipaddress = "192.168.0.110"
$nasusername = ".\nas-local"
$naspassword = cat "F:\Skript\password.txt" | ConvertTo-SecureString 
$credential = New-Object System.Management.Automation.PSCredential ($nasusername, $naspassword)

robocopy F:\Admin$ \\$nasipaddress\robocopy /MIR /PURGE /e /log+:$filepath

Robocopy works just fine, but it needs authentication credentials and that's the reason why it doesn't run when the PC is inactive for a while, that's why I'm implementing it to map it first. It has to be mapped temporarily, not persistent.

I think it might be because it's not a domain device, but I might be wrong here. Did google a lot, like really a lot, but couldn't find anything that worked so here's my last resort. Not that experienced with PowerShell, but on my way to mastering it.

Best Regards and thank you very much

stillrigeway



Solution 1:[1]

You should map to a share, are you exposing a share from the NAS?

Can you provide the full "New-PSDrive" with your arguments?

Also, if this in a script, set the Scope parameter value to "Global" to ensure the drive persists outside the current scope.

source: https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.management/new-psdrive?view=powershell-7.2

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 jack_skellington