Here is the scenario. I'm creating a SQL Server Job that will use Powershell to test if a session could be made to a remote server before creating one. I was able to successfully create a cmdlet that will test a path that connects to a remote shared server on the same domain. The remote server I'm trying to connect to is on a different domain so I need to pass along the login credentials. I'm stuck in finding the correct way to perform this. Is there a way to pass the login information without a prompt? Below is my test-path command I want to use.
If(Test-Path -path \\IPADDRESS\Folder\subfolder)
{
write-error "Remote connection test passed successfully"
}
else {Write-Error "Could not connect successfully to "\\IPADDRESS\Folder\subfolder" -EA Stop}
Also I was trying to use PSCredential to achieve this but was unsuccessful and received an error. Below is the powershell commands and 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 successfully to "\\IPADDRESS\Folder\subfolder" -EA
Stop}
>>
Test-Path : A positional parameter cannot be found that accepts argument 'System.Management.Automation.PSCredential'.
At line:1 char:14
+ If(Test-Path <<<< -path \\IPADDRESS\Folder\subfolder $Credentials)
+ CategoryInfo : InvalidArgument: (:) [Test-Path], ParameterBindingException
+ FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.TestPathCommand
Thank you in advance