Why is Get-ChildItem inside a Function Access denied after correct New-PSDrive? (It works outside of the function scope.)

303 views Asked by At

This doesnt work

function ImportProcess{
    param(
        [Parameter()]
        [string]$XMLPath
    )

  
    $sourcePath = $XMLPath
    $DestPath = split-path "$sourcePath" -Parent #this gives format without slash at and and makes powerShell *very happy*
    write-host "connecting to Dir"
    New-PSDrive -Name "Drive" -PSProvider FileSystem -Credential $global:cred -Root "$DestPath" | Out-Null
    write-host "done"

    write-host "getting all xml files"
    $temp1 = Get-ChildItem -Path $sourcePath
}

I want to put this inside a function and access the files in the path and this below works. I got this methode from using credentials to get-childitem on other server

$sourcePath = "path"
$DestPath = split-path "$sourcePath" -Parent #this gives format without slash at and and makes powerShell *very happy*
write-host "connecting to Dir"
New-PSDrive -Name "Drive" -PSProvider FileSystem -Credential $global:cred -Root "$DestPath" | Out-Null
write-host "done"

write-host "getting all xml files"
$temp1 = Get-ChildItem -Path $sourcePath

I tried different scopes in New-PSDrive and set-location before get-childitem, aswell as setting the Path to the Drive name + needed subdir

i removed the part where i get the credentials, because all works but as soon as i put the process in a function i get "Get-ChildItem : Access is denied"

0

There are 0 answers