0

Why does PowerShell asks me for Credentials to access a remotecomputer, after I already put in the password for my $PW variable and created the correct PSCredentials with it? The other Issue I have is that Start-Process can't find the FilePath specified (Null or empty).

I guess those two errors are because of the same error, it has to be because my Variable-Values aren't passed to the foreach loop. As you can see, I tried to achieve a solution by trying the -argumentlist parameter, but it doesn't work. What Am I doing wrong?

function Install-Exe {
    param(
        [string[]]$ComputerName,
        [string]$UNCexepath,
        [string[]]$exeargs
    )
    $Admin = "domain\admin"
    $PW = Read-Host "Enter Password" -AsSecureString
    $cred = New-Object System.Management.Automation.PSCredential -argumentlist $Admin, $PW
    foreach ($c in $ComputerName) {
        Invoke-Command -ComputerName $c -Credential $cred -ArgumentList $cred,$UNCexepath {
            Start-Process -Filepath $UNCexepath -Credential $cred -Verb RunAs   
        }
    }}

1 Answer 1

1

You need to declare the parameters inside the Invoke-Command scriptblock:

foreach ($c in $ComputerName) {
    Invoke-Command -ComputerName $c -Credential $cred -ArgumentList $cred,$UNCexepath {
        param($cred,$UNCexepath)
        Start-Process -Filepath $UNCexepath -Credential $cred -Verb RunAs   
    }
}}
Sign up to request clarification or add additional context in comments.

1 Comment

as easy as it gets. I thought I tried that... seems like I didn't. thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.