Forum Discussion
Running an exe install remotely
There's a couple of issues going on here.
First, the Invoke-Command parameters are a mess and need fixing.
Secondly, because you're using a UNC to refer to the file - meaning that file you want to execute is stored on a different server to the one being targeted by the Invoke-Command statement, you're going to run into another error relating to credentials delegation.
For the credentials delegation issue, you can either:
- Implement something called Kerberos Constrained Delegation; or
- Since this is only a basic SMB access issue, use the SMB commandlets to establish an SMB session within the remote command itself.
The first point (Kerberos Constrained Delegation) is complex and more than I'm inclined to discuss here, so I'll provide an example of the second point.
I've also shifter the Get-ADComputer into the -ComputerName parameter of Invoke-Command so that the commands can be run in parallel rather than synchronously, as this will scale up significantly better.
Because of the issues with the arguments, I've taken a "best guess" approach to them in my example, so be sure to double-check what I've put in as it may not be what you had intended.
# Fetch the credential under which the remote job will be running, and for convenience, also used to establish the SMB mapping.
$LocalCredential = Get-Credential;
Invoke-Command -Credential $LocalCredential -ComputerName ((Get-ADComputer -Filter { dNSHostName -like "*" } -SearchBase "OU=Servers,DC=Domain,DC=Local" -Properties dNSHostName).dNSHostName) -ArgumentList $LocalCredential -ScriptBlock {
# Fetch the Credential object passed in via -ArgumentList
Param($InternalCredential);
# Create the network credentials mapping
New-SmbMapping -RemotePath "\\server\share" -UserName ($InternalCredential.GetNetworkCredential().Username) -Password ($InternalCredential.GetNetworkCredential().Password) -Persistent:$false;
# Run the command
\\server\share\install.exe /S /Token="Test$";
}
Cheers,
Lain