Test-Path with -Credentails parameter fails

Hi,

When I try test-path on remote machine it fails:

read-host -assecurestring | convertfrom-securestring | out-file C:\cred.txt

$remotemachinepassword = get-content C:\cred.txt | convertto-securestring

$credentials = new-object -typename System.Management.Automation.PSCredential -argumentlist "CGLASWEB01D\infra",$remotemachinepassword

test-path \\CGLASWEB01D\c$\donotshutdown.txt -Credential $credentials

it gives me the following error:

Test-Path : Cannot retrieve the dynamic parameters for the cmdlet. The FileSystem provider supports credentials only
on the New-PSDrive cmdlet. Perform the operation again without specifying credentials.
At line:1 char:1
+ test-path \\CGGLASWEB01D\c$\donotshutdown.txt -Credential $credential ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Test-Path], ParameterBindingException
    + FullyQualifiedErrorId : GetDynamicParametersException,Microsoft.PowerShell.Commands.TestPathCommand

Do anyone know how I can get this to work? or do anyone know an alternative I can use?

Regards,
Kay
Kelly GarciaSenior Systems AdministratorAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

footechCommented:
From everything I've read, the -Credential parameter of Test-Path is useless.  I haven't seen a good explanation of why it's included.  The documentation states -
This parameter is not supported by any providers installed with Windows PowerShell.

You can use New-PsDrive with your credentials (essentially creating a mapped drive), and if you like you can then use Test-Path specifying the new drive as the path (or you could just check whether the New-PsDrive command completed successfully).
$cred = Get-Credential
New-PSDrive -Name Z -PSProvider FileSystem -Root \\server\share -Credential $cred
Test-Path Z:\

Open in new window

Kelly GarciaSenior Systems AdministratorAuthor Commented:
basically I have script for auto shutting down servers in azure, however if a file exists (donotshutdown.txt), for the server not to shutdown, therefore I put the test-path in an if conditional statement. Is this possible with the new-psdrive. below is my script:

workflow Shutdown_VMs_8PM {




$Tags=Get-AzureRmVM | ? {$_.Tags.Values -eq '8PM' -and $_.Tags.Keys -eq 'Shutdown-VM' }


Foreach -Parallel ( $VM in $Tags )
{

#$credential= 

if (test-path \\$vm.name\c$\donotshutdown.txt) {

    Write-Output "$($VM.Name) is shuttingdown"
    Stop-Azurermvm -Name $VM.Name -ResourceGroupName $VM.ResourceGroupName -ErrorAction Continue
  
}


}#end test-path

}#end workflow

Open in new window


the above script is within Azure Runbooks.
footechCommented:
You may be better served by adding a tag to the Azure VM which you could check, rather than having a file on the VM.  Seems you're already doing something similar to build your list of which VMs to shut down.  Why not just use that as the control?

I don't know if you might encounter other issues due to Runbooks, etc., but you could try something like below.
workflow Shutdown_VMs_8PM {




$Tags=Get-AzureRmVM | ? {$_.Tags.Values -eq '8PM' -and $_.Tags.Keys -eq 'Shutdown-VM' }


Foreach -Parallel ( $VM in $Tags )
{

    $credential= whatever
    New-PSDrive -Name $VM.Name -PSProvider FileSystem -Root "\\$($vm.name)\c$" -Credential $credential

    if (Test-Path "$($vm.name):\donotshutdown.txt") {

        Write-Output "$($VM.Name) is shuttingdown"
        Stop-AzureRmVM -Name $VM.Name -ResourceGroupName $VM.ResourceGroupName -ErrorAction Continue
  
    }
    Remove-PSDrive -Name $VM.Name


}#end test-path

}#end workflow

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Kelly GarciaSenior Systems AdministratorAuthor Commented:
Problem is developers do not have access to the Azure Portal, and we wanted them to be able to stop the shutdown themselves.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.