Link to home
Start Free TrialLog in
Avatar of Kelly Garcia
Kelly GarciaFlag for United Kingdom of Great Britain and Northern Ireland

asked on

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
Avatar of footech
footech
Flag of United States of America image

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

Avatar of Kelly Garcia

ASKER

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.
ASKER CERTIFIED SOLUTION
Avatar of footech
footech
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Problem is developers do not have access to the Azure Portal, and we wanted them to be able to stop the shutdown themselves.