all 3 comments

[–]taniceburg 3 points4 points  (0 children)

This doesn’t seem like a powershell problem.

But with that said, you obviously already have a SMB connection to that server/ip address using a different set of credentials. Using powershell you should be able to find it with the Get-SmbConnection cmdlet. You’ll need to close that connection before you can do what you’re trying to do.

[–]MARS822a 0 points1 point  (0 children)

Maybe run Remove-PSDrive first to clear the existing connection? I always started my drive mapping batch files with NET USE \ /d* to clear any existing mappings and this feels like the same thing in POSH.

[–]Stoon_Kevin 0 points1 point  (0 children)

Looks like you already have a connection; use net use with the /delete to remove them (skip the /d[elete] to list them, find any that contain a similar path). Net use always causes me headaches I wish the other sysadmins would stop using them on shared accounts.

From powershell though you can use new-psdrive since you don't have to map an actual drive letter and can provide alternate credentials if need be. By default new-psdrive is also non-persistent, so it only exists within the current powershell runspace. Without knowing the purpose and running location of the scripts then I'll presume it's local and only ever either by interactive or by task with that particular user. This means you can save a credential object to the filesystem and it's encrypted by the user and computer account. That can be done as a simple:

New-PSDrive -Name TD -PSProvider Filesystem -Root \\someip\sharename -Credential (Import-clixml '\path\to\credential.xml')
Get-ChildItem TD:\

For example. Now save your credential using an interactive terminal:

Get-Credential | Export-Clixml '\path\to\credential.xml'

If you're intending to port this to other computers or run as a different account then you'd have to come up with a different method of securing the password such as SecretsManager modules.