We currently have a Scheduled Task on each local PC that runs the command "TF.exe /checkin" when triggered remotely using schtasks. this is so that when someone forgets to check-in we have a way to do so remotely.
But we would like to integrate this into a PowerShell script that can be triggered remotely by just entering the computer name but for some reason, it doesn't seem to work.
This runs fine on the local pc
Invoke-Command -ScriptBlock {& 'C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\Common7\IDE\CommonExtensions\Microsoft\TeamFoundation\Team Explorer\tf.exe' checkin /recursive}
However, when adding -ComputerName it fails to work with the following error.
Invoke-Command -ComputerName *ComputerName* -ScriptBlock {& 'C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\Common7\IDE\CommonExtensions\Microsoft\TeamFoundation\Team Explorer\tf.exe' check-in /recursive}
Unable to determine the workspace. You may be able to correct this by running 'tf workspaces /collection:TeamProjetCollectionURL'.
+CategoryInfo: NotSpecified (Unable to deter...ColletionUrl'.:string [], Remoteexpection
Any ideas on how to fix this? I've already tried /collection:https://tfsserver:port/collectionanme but it doesn't work either
Error: Option /collection cannot be used without option /shelveset which is weird as I am not using /shelveset.
-----Update----
See the image below, before running the powershell script, I had a file checked out on my local pc, then running Enter-PSSession to a remote computer and then running invoke-command still only checked in my local files and not on remote computer.
---update 04/12---
I don't know why it doesn't work same issue "unable to determine the workspace"
You need to specify workspace path on remote session with the following code:
$session = New-PSSession -ComputerName ComputerName
Invoke-Command -Session $session -ScriptBlock { Set-Location D:\workspace}
Enter-PSSession -Session $session
Then run tf command:
Invoke-Command -ScriptBlock {& 'C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\Common7\IDE\CommonExtensions\Microsoft\TeamFoundation\Team Explorer\tf.exe' checkin /recursive}
Entire result:
Related
Ok, So I am working on the powershell remote deployment of software to ADUsers.
I have remote access to the computer and all, no problems there.
Right now I am at the point where I have a exe file from Ninite to just install 7zip onto the client pc just to see when it works so i can start deploying some bigger programs to it.
The guide I have found to help me deploy out the software for now looks like this:
Invoke-Command -ComputerName *REDACTED* -Scriptblock {
Start-Process C:\Users\Administrator\Documents\ninite_7_zip\Ninite 7Zip Installer.exe '/silent' -wait
}
When I do run this code, I get the error:
A positional parameter cannot be found that accepts argument 'Installer.exe'.
So I thought to myself, that it might be because of the spaces in the name. So therefore I changed it to:
Invoke-Command -ComputerName *REDACTED* -Scriptblock {
Start-Process C:\Users\Administrator\Documents\ninite_7_zip\Ninite_7Zip_Installer.exe '/silent' -wait
}
And ofcourse also changed it's name within the folder to match the "newly made" code.
But the error now changed into:
This command cannot be run due to the error: The system cannot find the file specified
Even though I use Powershell ISE, and I used it's guideboxes when writing, to enter the folder and find it, when I wrote the directory.
My only goal in this, is that I want to remotely run and complete this installer on the client PC when deployed from the DC upon which the file lies.
Anybody got a qualified guess? Or maybe even so, a solution.
Thanks in advance for your kind answers.
When I do run this code, I get the error:
A positional parameter cannot be found that accepts argument 'Installer.exe'.
You'll want to use quotation marks to qualify path names with spaces in them:
Start-Process 'C:\Users\Administrator\Documents\ninite_7_zip\Ninite 7Zip Installer.exe' '/silent' -wait
But the error now changed into:
This command cannot be run due to the error: The system cannot find the file specified
Even though I use Powershell ISE, and I used it's guideboxes when writing, to enter the folder and find it, when I wrote the directory.
ISE is not smart enough to realize that the scriptblock is to be executed on a remote computer, so it completes the path based on your local file system.
You still need to copy the executable to the remote machine in order to execute it:
# first copy the installer to remote file system
$remoteSession = New-PSSession -ComputerName $computerName
$localInstaller = 'C:\Users\Administrator\Documents\ninite_7_zip\Ninite 7Zip Installer.exe'
$remotePath = Invoke-Command -Session $remoteSession -ScriptBlock { $env:TEMP }
Copy-Item $localInstaller -Destination (Join-Path $remotePath "7zInstaller.exe") -ToSession $remoteSession
# now we can invoke the executable on the remote machine (re-using the same remoting session)
Invoke-Command -Session $remoteSession -ScriptBlock {
Start-Process (Join-Path $env:TEMP "7zInstaller.exe") '/silent' -Wait
}
# clean up
$remoteSession |Remove-PSSession |Out-Null
I have a script that installs an .exe with some arguments remotely to a list of servers that works fine. When I try to do almost the exact same thing but run the uninstall.exe that gets installed to C:\Program Files (x86)\ it won't work.
When I run the scripts on the server locally, it kicks off the uninstall. When I try to run the exact same script or command using the powershell invoke-command it won't work.
$serverlist = Get-Content -Path C:\NagiosInstall\test.txt
ForEach ($server in $serverlist) {
New-Item -Path "\\$server\C$\" -Name "NagiosInstall" -Force -ItemType "directory"
Copy C:\NagiosInstall\ncpa-2.1.6.exe \\$server\C$\NagiosInstall\ncpa-2.1.6.exe
Copy C:\NagiosInstall\install.bat \\$server\C$\NagiosInstall\install.bat
invoke-command -ComputerName $server -ScriptBlock {C:\NagiosInstall\install.bat}
Start-Sleep -s 15
invoke-Command -ComputerName $server -ScriptBlock {Remove-Item -LiteralPath "C:\NagiosInstall" -Force -Recurse}
}
The install .bat is just a simple command to silently install that ncpa-2.1.6.exe.
Above is my install script, that part all works fine.
invoke-command -ComputerName $server -ScriptBlock {Start-Process -FilePath "C:\Program Files (x86)\Nagios\NCPA\uninstall.exe" -ArgumentList "/S"}
Running the above command, nothing happens. No errors, nothing.
& "C:\Program Files (x86)\Nagios\NCPA\uninstall.exe" -ArgumentList "/S"
But running the above command in powershell that's running as admin locally on the server and it works just fine.
I've also tried the same approach to create and copy and run a batch file, very similar to the above "install" code. Same thing... nothing happens but if you run the batch locally on the server, it works just fine. I can post this code if anyone is interested.
I'm guessing it has to do with the invoke-command or the fact that it's in C:\Program Files (x86) which might make the syntax different, but I've tried many things and I'm out of ideas besides making an account and posting here.
The issue is that Invoke-Command runs non-interactively, and therefore cannot run as Administrator and respond to a UAC prompt.
The only workaround is to connect to the computer via a PSSession with credentials, and execute it that way:
$Cred = Get-Credential
$Session = New-PSSession -ComputerName $server -Credential $Cred
Invoke-Command -Session $Session -ScriptBlock {Start-Process -FilePath "C:\Program Files (x86)\Nagios\NCPA\uninstall.exe" -ArgumentList "/S"}
$Session | Exit-PSSession
Edit:
The reason that the installer works is that the UAC prompt for Windows Installs is different than anything else in Windows see: How to Silence the UAC Prompt for Per-Machine MSI Packages for Non-Admins or Using Windows Installer with UAC.
Essentially, Windows Installer (already running as admin and UAC approved), is what runs the install on your behalf, and it is Windows Installer and installer settings that determines if you need to see a UAC prompt or not. Hence, this is why the install works. Windows Installer determined that you did not need to see the UAC prompt, and the install proceeds.
Uninstalling is different. Since you are running uninstall.exe, the executable needs admin access and Windows will do UAC before the uninstall.exe even runs.
I am trying to install exe on remote machine by using below script, but it didn't succeed. The exe file is on remote machine.
Invoke-Command -ComputerName COMPUTER -ScriptBlock{
Start-Process c:\Windows\Temp\ccsetup.exe -ArgumentList '/silent' -Wait
}
I tried giving different paths but it is not helpful.
There is an exe file on backup server “Backupsrv” C:\Program files\Veritas\NetBackup\bin\admincmd\bpdbjobs.exe.
When I run this exe on “Backupsrv” using PowerShell it is giving some output on console
& "C:\Program Files\Veritas\netbackup\bin\admincmd\bpdbjobs.exe" -all_columns
Now I tried to run this exe from an application server remotely using Invoke-Command as below.
$result = Invoke-Command -ComputerName "backupsrv" -Credential $cred -ErrorAction Stop -ScriptBlock {
& "C:\Program Files\Veritas\netbackup\bin\admincmd\bpdbjobs.exe" -all_columns
}
No result When executed remotely.
The exe is being executed but not giving any result.
I am running command to copy a file from shared location to the local machine.
Command is Copy-Item '\\ServerName\share\Setup\Setup.msi' 'C:\Windows\Temp\RiversandSetup'
This command is works fine when I run it from Server1. But when i run same command from remote machine by opening a session it fails with error 'Cannot find path 'SharePath' because it does not exist.'. Command is $sessions = New-PSSession -ComputerName RemoteServerName
Invoke-Command -session $sessions -ScriptBlock {Copy-Item '\\SharePath\share\Setup\Setup.msi' 'C:\Windows\Temp\RiversandSetup'}
Please advice.
You are most likely running into an issue with double-hop authentication. This could help but it depends on your environment:
Setting up CredSSP properly for powershell multi-hop issue