1

i have trouble with "New-PSDrive -Root"

When i try to map a New-PSDrive -Root $patch with a $path using an array, the cmd does not map the drive but give me an error : "The network Path was not found".

The path is working if i use an explorer in my windows.

How can i fix that ?

Thanks a lot

exemple :

foreach ($s in $serverlist) 
{
    $path = "\\$s\e$\Updates\file\
    New-PSDrive -Name "S" -Root $path -Persist -PSProvider "FileSystem" -Credential $cred
}

Problem

This is the entire script :

Get-Date -Format "dddd MM/dd/yyyy HH:mm K">> "C:\file\results.txt"
$cred = Get-Credential -Credential domain\name

$serverlist = @(get-content -Path "C:\file\serverlist.txt") 
foreach ($s in $serverlist) 
{
    $path = "\\$s\e$\Updates\file\"
    New-PSDrive -Name "S" -Root $path -Persist -PSProvider "FileSystem" -Credential $cred
    $path2 = "\\$s\e$\Updates\file\errors.txt"
    $file = Get-Content $path2
    $containsWord = $file | %{$_ -match "0"}
    if ($containsWord -contains $true) {
        Out-File -FilePath  "C:\file\results.txt" -Append -InputObject "$s : ok"
    } else {
        Out-File -FilePath  "C:\file\results.txt" -Append -InputObject "$s : nok"
    }
    Remove-PSDrive -Name "S"
}

EDIT 1 : If i try to access to the file directly by an windows explorer with the same credential and I, after that, run the script, it works

su7
  • 66
  • 1
  • 7
  • What does `$path` by itself return? – Abraham Zinala Nov 26 '21 at 14:55
  • [1] Show us some of your serverlist.txt file, because now we don't know what `"\\$s\e$\Updates\file\"` reads. [2] Why are you trying to make a mapping when in the rest of your code you do not use it? [3] Is user `domain\name` allowed acces to drive `e$` ? – Theo Nov 26 '21 at 15:29
  • @AbrahamZinala $path return the correct path : \\server1\e$\update\file\ – su7 Nov 26 '21 at 15:53
  • @Theo The user $cred has the right to acces to drive e$. Mapping the drive provide me the acces to the log in Get-Content "\\$s\e$\Updates\file\errors.txt". – su7 Nov 26 '21 at 15:56
  • @su7 `"\\$s\e$\Updates\file\errors.txt"` is the UNC name. Again, you are NOT using the mapping `S:\errors.txt`, so I would leave that out of the code completely and use the UNC names always. – Theo Nov 26 '21 at 15:57
  • @Theo If i dont use '"New-PsDrive"' and I only use '"Get-content $path"' I got an error : "Cannot find path '\\server1\e$\Update\file\errors.txt' because it does not exist" – su7 Nov 26 '21 at 16:02
  • I added an edit to the post. I found that earlier trying desperately to fix the script :) – su7 Nov 26 '21 at 16:06
  • Then it seems obvious **you** as user do not have permissions to go there. Use `Invoke-Command` where you can use the credentials in `$cred` and have the scriptblock simply output either `"$($env:COMPUTERNAME) : ok"` or `"$($env:COMPUTERNAME) : nok"`. Capture that result and append it to your local `"C:\file\results.txt"` file. – Theo Nov 26 '21 at 16:14

1 Answers1

1

As commented, the user in $cred may have permissions to access the file in the path on the server, but you as it seems do not.

Try using Invoke-Command where you can execute a scriptblock using different credentials than your own:

$cred = Get-Credential -Credential domain\name

$serverlist = Get-Content -Path "C:\file\serverlist.txt"
# loop through the list of servers and have these perform the action in the scriptblock
$result = foreach ($s in $serverlist) {
    Invoke-Command -ComputerName $s -Credential $cred -ScriptBlock {
        # you're running this on the server itself, so now use the LOCAL path
        $msg = if ((Get-Content 'E:\Updates\file\errors.txt' -Raw) -match '0') { 'ok' } else { 'nok' }
        # output 'ok' or 'nok'
        '{0} : {1}' -f $env:COMPUTERNAME, $msg
    }
}

# write to the results.txt file
# change 'Add-Content' in the next line to 'Set-Content' if you want to create a new, blank file
Get-Date -Format "dddd MM/dd/yyyy HH:mm K" | Add-Content -Path 'C:\file\results.txt'
$result | Add-Content -Path 'C:\file\results.txt'

In fact, you don't even need a foreach loop because parameter -ComputerName can receive an array of server names:

$result = Invoke-Command -ComputerName $serverlist -Credential $cred -ScriptBlock {
    # you're running this on the server itself, so now use the LOCAL path
    $msg = if ((Get-Content 'E:\Updates\file\errors.txt' -Raw) -match '0') { 'ok' } else { 'nok' }
    # output 'ok' or 'nok'
    '{0} : {1}' -f $env:COMPUTERNAME, $msg
}
Theo
  • 57,719
  • 8
  • 24
  • 41
  • Thanks a lot for you help mate ! I will try it tommorow (i cant rn) and send you a feedback. your help is much appreciated ! – su7 Nov 29 '21 at 10:14
  • Hello @Theo. I tried your script with the good path & credential but i still got a problem : `[****] Connecting to remote server **** failed with the following error message : The user name or password is incorrect. For more information, see the about_Remote_Troubleshooting Help topic. + CategoryInfo : OpenError: (*****:String) [], PSRemotingTransportException + FullyQualifiedErrorId : LogonFailure,PSSessionStateBroken` . I'm sure it is the correct credential and the user is admin of the computer. Thanks a lot – su7 Nov 30 '21 at 10:52
  • @su7 Please try `$cred = Get-Credential -Message 'Please enter your admin credentials'`. The credentials you give now are definitively refused.. – Theo Nov 30 '21 at 11:08