Kelly Garcia
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.Automati on.PSCrede ntial -argumentlist "CGLASWEB01D\infra",$remot emachinepa ssword
test-path \\CGLASWEB01D\c$\donotshut down.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$\donotshu tdown.txt -Credential $credential ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~ ~~~~~~~~~~ ~~~~~~~~~~ ~~~~~~~~~~ ~~~
+ CategoryInfo : InvalidArgument: (:) [Test-Path], ParameterBindingException
+ FullyQualifiedErrorId : GetDynamicParametersExcept ion,Micros oft.PowerS hell.Comma nds.TestPa thCommand
Do anyone know how I can get this to work? or do anyone know an alternative I can use?
Regards,
Kay
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.Automati
test-path \\CGLASWEB01D\c$\donotshut
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$\donotshu
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Test-Path], ParameterBindingException
+ FullyQualifiedErrorId : GetDynamicParametersExcept
Do anyone know how I can get this to work? or do anyone know an alternative I can use?
Regards,
Kay
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:
the above script is within Azure Runbooks.
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
the above script is within Azure Runbooks.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Problem is developers do not have access to the Azure Portal, and we wanted them to be able to stop the shutdown themselves.
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).
Open in new window