Using New-PSDrive inside a New-UDGrid element gives error #### The specified drive root "\\servername01.domain.local\c$" either does not exist, or it is not a folder

I am getting an error using a New-PSDrive inside a universal powershell grid.

The specified drive root “\servername01.domain.local\c$” either does not exist, or it is not a folder.

it works fine if I just run it as a power shell function and does the right thing but inside the grid and trying to render in IIS, I get this error.

any ideas or thoughts?

            New-UDGrid -Title "Service Status" -AutoRefresh -RefreshInterval 120 -Endpoint{
                $serviceStatObjs = @()
                
                function getservicestats {
                    param($servername, 
                        $serviceName,
                        $Cred
                        )
 #                   Write-Output "Hello"
                    New-PSDrive -Name tempDriveName –PSProvider FileSystem –Root "\\$servername\c$" -Credential $Cred
                    $serviceInfo = Get-Service -ComputerName $servername -Name $serviceName -ErrorAction SilentlyContinue | Select-Object Displayname,Name,Status
                    Remove-PSDrive -Name tempDriveName
 #                   Write-Output "ServiceInfo = " + $serviceInfo
                    if($serviceInfo.Status -eq 'Running'){$stat = "Running"}
                    Elseif($serviceInfo.Status -eq "stopped"){$stat = "Stopped"}
                    $serviceObj = [PSCustomObject]@{
                        ServerName =  $servername
                        ServiceDisplayName = $serviceInfo.Displayname
                        Status = $stat
                        }
                    $serviceObj    
                    }
                
               $data = @(
                      [pscustomobject]@{Server="servername01.domain.local";ServiceName="Onlife.MessageService";login=$Creds}
                   )
                

                
                foreach($service in $data){
                    $serviceStatObjs += getservicestats $service.Server $service.ServiceName $service.login

                    }
                
                #output as usable data
                $serviceStatObjs | Out-UDGridData
                }

You might need to escape the $ in your path:

 "\\$servername\c`$"

I gave that a shot and still the same error.

The specified drive root “\servername01.domain.local\c’$” either does not exist, or it is not a folder.

i have tried to hard code the full \servername01.domain.local\c$ and same thing.

also tried to place the entire value in a string and use the string and same error.

could there be some issue where inside the New-UDGrid element that this is running into something it doesnt like?

we can test this with the following in a New-UDGrid

New-PSDrive -Name tempDriveName –PSProvider FileSystem –Root "\\$servername\c$" -Credential $Cred
$serviceInfo = Get-Service -ComputerName $servername -Name $serviceName 
Remove-PSDrive -Name tempDriveName

Is this perhaps some limitation of being inside the New-UDGrid element. I have tried a number of ways but cant seem to get this to work. It works outside New-UDGrid but then I cant get the data to inside the New-UDGrid.

The UDGrid shouldn’t be an issue. Have you tried running this in a PSU script to see if the same error happens there?

i changed my approach to using New-PSSession and that doesnt seem to have any issues.