Auth to a Samba server in a script

Hello,

I’m trying to use the following script to modify hundreds of LNK dispatched in several folders on a Samba server.

The problem is I can’t access it without authentification and PowerShell doesn’t seem to inherit the authentification saved on the computer.

Here is the script :

<span style="color: #000000">
<span style="color: #0000bb"> 
</span><span style="color: #ff8000"># Call wscript com object
</span><span style="color: #0000bb">$shell </span><span style="color: #007700">= new-</span><span style="color: #0000bb">object </span><span style="color: #007700">-</span><span style="color: #0000bb">com wscript</span><span style="color: #007700">.</span><span style="color: #0000bb">shell

</span><span style="color: #ff8000"># Recurse through directories for .lnk files
</span><span style="color: #0000bb">dir </span><span style="color: #dd0000">"\\SAMBA\COMMUN\" </span><span style="color: #007700">-</span><span style="color: #0000bb">filter </span><span style="color: #007700">*.</span><span style="color: #0000bb">lnk </span><span style="color: #007700">-</span><span style="color: #0000bb">recurse </span><span style="color: #007700">| foreach {
</span><span style="color: #0000bb">$lnk </span><span style="color: #007700">= </span><span style="color: #0000bb">$shell</span><span style="color: #007700">.</span><span style="color: #0000bb">createShortcut</span><span style="color: #007700">(</span><span style="color: #0000bb">$_</span><span style="color: #007700">.</span><span style="color: #0000bb">fullname</span><span style="color: #007700">)
</span><span style="color: #0000bb">$oldPath</span><span style="color: #007700">= </span><span style="color: #0000bb">$lnk</span><span style="color: #007700">.</span><span style="color: #0000bb">targetPath

</span><span style="color: #ff8000"># If match text, perform operation
</span><span style="color: #007700">if(</span><span style="color: #0000bb">$oldpath </span><span style="color: #007700">-</span><span style="color: #0000bb">match </span><span style="color: #dd0000">"\\SAMBA\SambaShare"</span><span style="color: #007700">)
{
</span><span style="color: #0000bb">write</span><span style="color: #007700">-</span><span style="color: #0000bb">host </span><span style="color: #dd0000">"Match: " </span><span style="color: #007700">+ </span><span style="color: #0000bb">$_</span><span style="color: #007700">.</span><span style="color: #0000bb">fullname
remove</span><span style="color: #007700">-</span><span style="color: #0000bb">item $_</span><span style="color: #007700">.</span><span style="color: #0000bb">fullname
$lnknew </span><span style="color: #007700">= </span><span style="color: #0000bb">$shell</span><span style="color: #007700">.</span><span style="color: #0000bb">createShortcut</span><span style="color: #007700">(</span><span style="color: #0000bb">$_</span><span style="color: #007700">.</span><span style="color: #0000bb">fullname</span><span style="color: #007700">)
</span><span style="color: #0000bb">$lnknew</span><span style="color: #007700">.</span><span style="color: #0000bb">targetPath </span><span style="color: #007700">= </span><span style="color: #dd0000">"`"</span><span style="color: #0000bb">\\SAMBA</span><span style="color: #007700">`</span><span style="color: #0000bb">""
$lnknew.IconLocation = "%SystemRoot%\system32\SHELL32.dll,4"
$lnknew.Save()
</span><span style="color: #007700">}</span>
<span style="color: #007700">}</span><span style="color: #0000bb">
Write-Host "End..."
</span></span>

So I would need to auth on the server before launching the command “dir”.

Can anyone help with that ? Thanks you :slight_smile:

 

You can try creating a PSDrive that points to the share using the Credential parameter.

New-PSDrive -Name Share -PSProvider FileSystem -Root \\SAMBA\COMMUN -Credential (Get-Credential) # You can enter your credentials here e.g. domain\user
Get-ChildItem Share:\ -filter *.lnk -recurse| ForEach-Object {
    $lnk = $shell.createShortcut($_.fullname)
    $oldPath = $lnk.targetPath
}

Just taking a quick stab at it, not sure if this will actually work in your instance.

pwshliquori

Thanks you, I will try that later and let you know.

 

I have also been considering a simple, but not sure if it will be enough:

net use \\server\share /user:<domain\username> <password>

All right, thanks to your New-PSDrive command I connected PowerShell to the share and can now access it inside PowerShell.

Now the problem is … my script doesn’t work as expected. Inside the LNKs, I have paths, which contains SambaShare this word. which needs to be removed.

My script find it, but instead of just replacing it by the new value, it replace the entire path with the new value…

How could I just removed this word from the paths ?

.lnk files are just text files. So, you can update the info in them the same way.

Example - this is for an icon, but the same approach can but used for each item

### Replace a shortcut link icon path

$file = 'E:\Temp\about_Redirection  Microsoft Docs.url'
$regex = 'IconFile.*'


Get-Content $file
# Results

[{000214A0-0000-0000-C000-000000000046}]
Prop3=19,11
[InternetShortcut]
IDList=
URL=https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_redirection?view=powershell-6
IconFile=https://docs.microsoft.com/favicon.ico
IconIndex=1


(Get-Content $file) -replace $regex, 'IconFile=E:\temp\world-wide-web-sml.ico'  | 
Set-Content $file

Get-Content $file

# Results

[{000214A0-0000-0000-C000-000000000046}]
Prop3=19,11
[InternetShortcut]
IDList=
URL=https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_redirection?view=powershell-6
IconFile=E:\temp\world-wide-web-sml.ico
IconIndex=1