Pass login credentials into a test-path cmdlet


here scenario. i'm creating sql server job use powershell test if session made remote server before creating one. able create cmdlet test path connects remote shared server on same domain. remote server i'm trying connect on different domain need pass along login credentials. i'm stuck in finding correct way perform this. there way pass login information without prompt? below test-path command want use.


if(test-path -path \\ipaddress\folder\subfolder)
  {
    
    write-error "remote connection test passed successfully"
    
    
  }
else {write-error "could not connect "\\ipaddress\folder\subfolder" -ea stop}


also trying use pscredential achieve unsuccessful , received error. below powershell commands , error message.

$securepassword = read-host -prompt "enter password" -assecurestring

$username = "domain\user"

$credentials = new-object system.management.automation.pscredential -argumentlist $username, $securepassword

ps c:\windows\system32>  if(test-path -path \\ipaddress\folder\subfolder $credentials)
>>   {
>>
>>     write-error "remote connection test passed successfully"
>>
>>
>>   }
>> else {write-error "could not connect "\\ipaddress\folder\subfolder" -ea
stop}
>>
test-path : positional parameter cannot found accepts argument 'system.management.automation.pscredential'.
@ line:1 char:14
+  if(test-path <<<<  -path \\ipaddress\folder\subfolder $credentials)
    + categoryinfo          : invalidargument: (:) [test-path], parameterbindingexception
    + fullyqualifiederrorid : positionalparameternotfound,microsoft.powershell.commands.testpathcommand

thank in advance


use named parameter, instead:

test-path -path \\ipaddress\folder\subfolder -credential $credentials



Windows Server  >  Windows PowerShell



Comments

Popular posts from this blog

WIMMount (HSM) causing cluster storage to go redirected (2012r2 DC)

Failed to delete the test record dcdiag-test-record in zone test.com

Azure MFA with Azure AD and RDS