5

I've gone through the documentation here. Oddly enough, I can't find anything mentioning how to simply connect to (not mount) an SMB share with PowerShell. I'm looking for the equivalent of this in Python:

with smbclient.open_file(args.share, username=args.smbuser, password=args.smbpass, mode='w', encoding='utf-8-sig', newline='') as file:
  file.write("stuff")
Grant Curell
  • 1,321
  • 2
  • 16
  • 32
  • 1
    Does this answer your question? ["net use" in PowerShell without specifying drive](https://stackoverflow.com/questions/40009297/net-use-in-powershell-without-specifying-drive) – marsze Nov 05 '20 at 14:37
  • @marsze I don't think so? The documentation is pretty scarce - maybe if I saw it in an example writing out a file? I think it might be part of the answer because the `New-SmbMapping` is the one I queued on, but it's not immediately clear how to use it to write a file from within a script. – Grant Curell Nov 05 '20 at 14:43

2 Answers2

4

You could use the Windows' net utility

net use \\server /user:domain\username password

Or use PowerShell cmdlet New-SmbMapping from the SmbShare module:

New-SmbMapping -RemotePath '\\server' -Username "domain\username" -Password "password"

After that, you can simply use the UNC path for writing to the file, for example with Out-File

"Your output" | Out-File "\\server\path\myfile.txt"
marsze
  • 15,079
  • 5
  • 45
  • 61
  • I see that New-SmbMapping returns a MSFT_SmbMapping object, but it's not clear to me how to write to a file. MS has only documented two methods for it - Create and Remove and neither of them seem to pertain to writing out an object. https://learn.microsoft.com/en-us/previous-versions/windows/desktop/smb/msft-smbmapping. Apologies, I didn't have the write to file bit in the title - I just had it in the Python example. I've edited the title to clarify. That ones on me - I appreciate the help. – Grant Curell Nov 05 '20 at 14:55
  • 2
    Just write to it after via it's unc path – Doug Maurer Nov 05 '20 at 14:56
  • 1
    @GrantCurell You can always write/read the file using the UNC path (\\server\...). The `net use` or `New-SmbMapping` is needed **only** if you have to authenticate first. – marsze Nov 05 '20 at 14:58
  • DougMaurer / @marsze firstly, thanks very much for the help. I'm still getting used to Powershell - is there a way to have known that from the documentation? In Python's modules they've got usage examples. I certainly understand how pipes work, but being new to Powershell would never have known the correlation between a returned MSFT_SmbMapping object and being able to pipe output into a UNC path. Honestly, I'm a bit unclear even now how that works - New-SmbMapping sets up the share and then when you pipe to a UNC with Out-File and it works? – Grant Curell Nov 05 '20 at 15:00
  • 1
    @GrantCurell Powershell has a very extensive [help system](https://learn.microsoft.com/en-us/powershell/scripting/learn/ps101/02-help-system?view=powershell-5.1). For example, you could write `Get-Help New-SmbMapping -Full` – marsze Nov 05 '20 at 15:04
  • @GrantCurell The whole fileshare/unc paths topic on the other hand is a windows feature and doesn't have much to do with powershell. You would have to do a bit of research about it if you're not familiar with it. – marsze Nov 05 '20 at 15:05
  • @marsze - I have a decent understanding of UNC - I guess what I'm confused about is how New-SmbMapping knows about them. In other programming languages you would run a command like New-SmbMapping and the return value would contain some binding to that UNC path which you could then action on. Maybe like bind=New-SmbMapping(args). Then you'd do something like bind.write(stuff). In PowerShell though you just ignore the return of New-SmbMapping and then pipe to the UNC path. I assume the Windows OS handles that under the hood, but is that just invisible to us as programmers? – Grant Curell Nov 05 '20 at 15:14
  • 1
    @GrantCurell Windows and the IO apis handle that. There are several options, that define if credentials should be cached or if the mapping should persist after a logoff. As I said, if there is no authentication required, all unc paths will work without any mapping at all. – marsze Nov 05 '20 at 15:23
1

Sorry for the late answer,

The native Powershell way is to use the PSDrive.

You can do it without sending credential using you current account.

To provide credentials you can use:

$remoteCredential = Get-Credential

or

$remoteCredential = [System.Management.Automation.PSCredential]::new('MyDomain\MyUser', (ConvertTo-SecureString 'MyPassword' -AsPlainText -Force))

Now to map the drive:

$psDrive = New-PSDrive -Name MyRemoteDrive -PSProvider FileSystem -Root '\\server\NetShare' -Credential $remoteCredential

You can then access it using the URI notation or MyRemoteDrive:

To disconnect the drive at the end you do:

Remove-PSDrive -Name MyRemoteDrive
delianmc
  • 61
  • 1
  • 6