Script fails on 2nd attempt, why?

Running from ISE as non admin accountand passing admin creds. This code will work with get-childitem but not the start-process. First run shows psdrive working, dir listing, whoami (is the admin account). If I run same code again, it will fail to map a drive, Why?
3rd run the start-process fails, I assume this isn’t a double hop issue since whoami is showing admin, and get-childitem shows me dir listing of UNC mapped drive. Why is start-process not working?

the argumentlist items work fine from CMD prompt. If I take -credentials out of start-process I am prompted but still get access denied so I feel creds are passing fine. How do I debug the start-process?

1st run works as expected

PS C:\Program Files (x86)\Microsoft Visual Studio\Installer> Invoke-Command -ComputerName $pc -Credential $credsWA -ScriptBlock {
        #Map a virtual drive as .wa account and start-process uses the T drive to install. This gets around the double hop problem.
        New-PSDrive -name t -PSProvider FileSystem -Root \\dmz\netinstall$\vs19_Ent -Credential $args[0]
        #Start-Process -FilePath 't:\vs_setup' -ArgumentList "install --quiet --norestart --nocache --noweb --noupdateinstaller --add Microsoft.VisualStudio.Workload.ManagedDesktop --add Microsoft.VisualStudio.Workload.NetWeb --add Component.GitHub.VisualStudio" -Wait -Credential $args[0] -RedirectStandardError c:\temp\err.txt -RedirectStandardOutput c:\temp\out.txt -Verbose -PassThru
        Get-ChildItem 't:\' -Filter *.exe
        whoami
        Remove-PSDrive -Name t
    } -ArgumentList $credsWA 

Name           Used (GB)     Free (GB) Provider      Root                                                             CurrentLocation PSComputerName                             
----           ---------     --------- --------      ----                                                             --------------- --------------                             
t                                                    \\dmz\netinstall$\vs19_Ent                                                       minint-1.domain.local                 

LastWriteTime  : 10/13/2021 9:03:09 AM
Length         : 1468216
Name           : vs_enterprise.exe
PSComputerName : minint-1.domain.local


LastWriteTime  : 10/13/2021 9:03:09 AM
Length         : 1468216
Name           : vs_setup.exe
PSComputerName : minint-1.domain.local

domain\john.doe.wa

2nd run, same command, different results.

Access is denied
    + CategoryInfo          : InvalidOperation: (t:PSDriveInfo) [New-PSDrive], Win32Exception
    + FullyQualifiedErrorId : CouldNotMapNetworkDrive,Microsoft.PowerShell.Commands.NewPSDriveCommand
    + PSComputerName        : minint-1.domain.local
 
Cannot find drive. A drive with the name 't' does not exist.
    + CategoryInfo          : ObjectNotFound: (t:String) [Get-ChildItem], DriveNotFoundException
    + FullyQualifiedErrorId : DriveNotFound,Microsoft.PowerShell.Commands.GetChildItemCommand
    + PSComputerName        : minint-1.domain.local
 
domain\john.doe.wa
Cannot find drive. A drive with the name 't' does not exist.
    + CategoryInfo          : ObjectNotFound: (t:String) [Remove-PSDrive], DriveNotFoundException
    + FullyQualifiedErrorId : DriveNotFound,Microsoft.PowerShell.Commands.RemovePSDriveCommand
    + PSComputerName        : minint-1.domain.local

3rd run, removed get-child, added start-process. Drive works, install fails, i know I had this working


PS C:\Program Files (x86)\Microsoft Visual Studio\Installer>  Invoke-Command -ComputerName $pc -Credential $credsWA -ScriptBlock {
		#Map a virtual drive as .wa account and start-process uses the T drive to install. This gets around the double hop problem.
        New-PSDrive -name t -PSProvider FileSystem -Root \\dmz\netinstall$\vs19_Ent -Credential $args[0]
        Start-Process -FilePath 't:\vs_setup' -ArgumentList "install --quiet --norestart --nocache --noweb --noupdateinstaller --add Microsoft.VisualStudio.Workload.ManagedDesktop --add Microsoft.VisualStudio.Workload.NetWeb --add Component.GitHub.VisualStudio" -Wait -Credential $args[0] -RedirectStandardError c:\temp\err.txt -RedirectStandardOutput c:\temp\out.txt -Verbose -PassThru
        #Get-ChildItem 't:\' -Filter *.exe
        whoami
        Remove-PSDrive -Name t
    } -ArgumentList $credsWA

Name           Used (GB)     Free (GB) Provider      Root                                                             CurrentLocation PSComputerName                             
----           ---------     --------- --------      ----                                                             --------------- --------------                             
t                                                    \\dmz\netinstall$\vs19_Ent                                                       minint-1.domain.local                 
This command cannot be run due to the error: Access is denied.
    + CategoryInfo          : InvalidOperation: (:) [Start-Process], InvalidOperationException
    + FullyQualifiedErrorId : InvalidOperationException,Microsoft.PowerShell.Commands.StartProcessCommand
    + PSComputerName        : minint-1.domain.local
 
domain\john.doe.wa

I have seen the start-process install Studio to remote machine (despite giving access denied error)

I have tried to do this example and couldn’t get it to work
Start-Process powershell -Credential mydomain\mydomainAdmin -ArgumentList ‘-noprofile -command &{Start-Process notepad -verb runas}’

bazinga,
Welcome to the forum. :wave:t4:

What is it what you’re actually trying to do? Installing an apllication on a remote computer from a source of another remote computer?
You may make you life easier by copying the installer files to the remote computer in advance by using UNC paths (without the need of mapping a drive locally or remote). Then you should be able to install the desired application on the remote computer a kind of “locally”.

I am trying to utilize network based install so I don’t have to copy GB worth of data to 40 devs workstations. Installs have to be done offline as well.

I change the code around and this works well in new-pssession -asjob while running PoSh as an admin.
MS docs provided this example
Start-Process -FilePath “$env:comspec” -ArgumentList “/c”,“dir”,""%systemdrive%\program files""

I’m glad you found a solution.

Just for me to get this right - am I wrong or do the data have to be transered to the remote machines anyway to be installed on these remote machines? :wink:

2 Likes