Powershell Start-Process for a remote machine not working - powershell

remotePSExecuter.ps1 file:
$InputArgs=$args[0]
$Username=$args[1]
$Password=$args[2]
$ComputerName=$args[3]
$PSToExecute=$args[4]
write-host $Username $Password $ComputerName $PSToExecute
$SecurePassWord = ConvertTo-SecureString -AsPlainText $Password -Force
$Cred = New-Object -TypeName "System.Management.Automation.PSCredential" $Username, $SecurePassWord
$Session = New-PSSession -ComputerName $ComputerName -credential $Cred
$DownloadInstallJob = Invoke-Command -Session $Session -filepath "${PSToExecute}" -ArgumentList "${InputArgs}"
echo $DownloadInstallJob
Remove-PSSession -Session $Session
downloadInstallWinVcops.ps1 file:
$buildId=$args[0]
$storageDir = "C:\vcops-downloads"
$webclient = New-Object System.Net.WebClient
$url = "http://build-squid.com/build/mts/release/${buildId}/publish/Web_Installers/InstData/Windows/VM/file.exe"
write-host url=$url
write-host downloading windows vcops build ${buildId} ...
$file = "$storageDir\winvcops.exe"
$webclient.DownloadFile($url,$file)
write-host downloading windows vcops finished!
write-host installing windows vcops...
Start-Process -FilePath "C:\vcops-downloads\winvcops.exe" -ArgumentList "-i silent" -wait
write-host windows vcops installation finished!
This is the way I am calling on Jenkins machine:
.\EnterpriseAdapters/remotePSExecuter.ps1 sb-xxxx Administrator password 10.xx.xx.xx EnterpriseAdapters/downloadInstallWinVcops.ps1
Problem:
Start-Process -FilePath "C:\vcops-downloads\winvcops.exe" -ArgumentList "-i silent" -wait is not getting called on remote machine.

Related

Start powershell script with credentials of other user isn't working

I try to run script with higher permissions using -credential argument
When I try to do it using this part of code it shows that error which basically means that The name of the directory is incorrect. Both script (that one that contain start-process and the change_permissions.ps1 are in the same folder which is \\srv1\Projekty\nowy_folder.
Without -credential argument script works completely fine.
Here's whole script:
#gui
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
$form_start = New-Object System.Windows.Forms.Form
$form_start.Text = 'Tworzenie projektu'
$form_start.Size = New-Object System.Drawing.Size(340,175)
$form_start.StartPosition = 'CenterScreen'
$permission_button = New-Object System.Windows.Forms.Button
$permission_button.Location = New-Object System.Drawing.Point(95,25)
$permission_button.Size = New-Object System.Drawing.Size(135,23)
$permission_button.Text = 'Przydziel uprawnienia'
$permission_button.add_Click({
$username = "DOMAIN\USER"
$password = "PASSWORD"
$credentials = New-Object System.Management.Automation.PSCredential -ArgumentList #($username,(ConvertTo-SecureString -String $password -AsPlainText -Force))
# Invoke-Command -FilePath "\\srv1\nowy_folder\change_permissions.ps1" -Credential $credentials
Start-Process powershell -argumentlist '.\change_permissions.ps1' -workingdirectory "S:" #-Credential ($credentials)
# Using UNC instead of .\change_permissions.ps1 isn't working
})
$form_start.Controls.Add($permission_button)
#Code below is basically the same but runs other script which also isn't running properly
$create_folders_button = New-Object System.Windows.Forms.Button
$create_folders_button.Location = New-Object System.Drawing.Point(110,75)
$create_folders_button.Size = New-Object System.Drawing.Size(100,23)
$create_folders_button.Text = 'Utwórz katalogi'
$create_folders_button.add_Click({
$username = "DOMAIN\USER"
$password = "PASSWORD"
$credentials = New-Object System.Management.Automation.PSCredential -ArgumentList #($username,(ConvertTo-SecureString -String $password -AsPlainText -Force))
Start-Process powershell -argument "C:\Projekty\nowy_folder\make_project_folder_with_subfolders.ps1" -Credential ($credentials)
# $form_start.close()
})
$form_start.Controls.Add($create_folders_button)
$form_start.Topmost = $true
$form_start.ShowDialog()
As you may noticed I tried the invoke-command as well. While using the invoke-command another error shows up:
Do you have any solution to my problem?
Regarding such approach
Invoke-Command -FilePath c:\scripts\test.ps1 -ComputerName Server01
The FilePath parameter specifies a script that is located on the local computer. The script runs on the remote computer and the results are returned to the local computer.
if your file is locally saved, then you can try:
Invoke-Command -FilePath $localPath -computername srv1 -Credential $credentials
or you can use full path with such approach:
Invoke-Command -computername srv1 -scriptblock { \\srv1\nowy_folder\change_permissions.ps1} -Credential $credentials
or you can use direct path
Invoke-Command -computername srv1 -scriptblock { c:\scripts\change_permissions.ps1} -Credential $credentials

Read and write from a remote PC to a shared network with powershell

I am trying to read a file from a remote machine, and then what I read, write it in a shared folder in network. This is my code.
Write-Host "Remote copy a file"
$username = 'Usuario'
$password = 'Password'
$myfile = [System.IO.File]::ReadAllBytes("C:\user.txt") ;
$Escribir={[System.IO.File]::WriteAllBytes("\\192.x.x.x\foreach\54.txt", $args)} ;
$pw = ConvertTo-SecureString $password -AsPlainText -Force
$cred = New-Object Management.Automation.PSCredential ($username, $pw)
$servers = Get-Content C:\Users\Agent\Desktop\pcs
foreach($server in $servers) {
$s = New-PSSession -computerName $server -credential $cred
Write-Host "PC name: $server ..." -ForegroundColor GREEN -Background BLACK
$Job = Invoke-Command -Session $s -ArgumentList $myfile -ScriptBlock $Escribir -AsJob
$Null = Wait-Job -Job $Job
Write-Host "Completed"
Remove-PSSession -Session $s
}
When I run the .ps1 file, I get the following error.

Call a Powershell Scriptfile that resides on a remote server

how do I call a ps1 file that resides on a target machine? All tutorials mostly say that I run a local ps1 on a remote machine. I tried the following but it just does nothing :/
$username = "theusername"
$password = "thepassword"
$secpassword = ConvertTo-SecureString –String $password –AsPlainText -Force
$credential = New-Object –TypeName "System.Management.Automation.PSCredential" –ArgumentList $username, $secpassword
$so = New-PSSessionOption -SkipCACheck
$session = New-PSSession -ConnectionUri "https://servername:5986/WSMAN" -SessionOption $so -Credential $credential
Invoke-Command -Session $session -ScriptBlock { "powershell E:\\Tools\Powershells\MyPowershell.ps1" }
Exit-PSSession
Executing a script by path (possibly with spaces) is done with
& "path with spaces\script.ps1"
This works just as well when remoted, so to execute a remote script stored remotely, use
Invoke-Command -Session $session -ScriptBlock { & "path with spaces\script.ps1" }

Executing powershell remotely issue

Hi when i try to do some code:
$Username = 'us'
$Password = 'password'
$pass = ConvertTo-SecureString -AsPlainText $Password -Force
$Cred = New-Object System.Management.Automation.PSCredential -ArgumentList $Username,$pass
powershell.exe -command "Invoke-Command -ComputerName server.com -scriptblock {pathCopyAndUnzip.ps1} -Credential $Cred"
This prompt me for a password but when i try to run this command like here (without powershell.exe):
Invoke-Command -ComputerName server.com -scriptblock {pathCopyAndUnzip.ps1} -Credential $Cred
it works without prompt. Do you know how to resolve that? I need to use option 1 because this command is runned from TFS build definition file like here:
<Exec Command="powershell.exe -command "Invoke-Command -ComputerName $(Server) -scriptblock {path} -Credential $Cred"" Condition="'$(RunTests)' == 'True'"/>
You could put your script into it's own file and then call that from TFS rather inline code.
C:\folder\script.ps1:
Param(
[string]$Username,
[string]$Password,
[string]$OtherParam,
)
$Password = $Password | ConvertTo-SecureString -AsPlainText -Force
$Cred = New-Object System.Management.Automation.PSCredential -ArgumentList $Username,$Password
Invoke-Command -ComputerName server.com -FilePath "C:\folder\CopyAndUnzip.ps1 -Something $OtherParam" -Credential $Cred
Then call it like so:
<Exec Command="powershell.exe -command "C:\folder\script.ps1 -username user10 -password P#55w0rd -OtherParam Whatever" Condition="'$(RunTests)' == 'True'"/>
You could try to pipe the commands to powershell.exe like this:
'$Username = "us"; $Password = "password"; $pass = ConvertTo-SecureString -AsPlainText $Password -Force; $Cred = New-Object System.Management.Automation.PSCredential -ArgumentList $Username,$pass; Invoke-Command -ComputerName server.com -scriptblock {pathCopyAndUnzip.ps1} -Credential $Cred' | powershell.exe -command -
<Exec Command="$(PsExecPath) -accepteula \\$(Server) cmd /C powershell -File FILEPATH " Condition="'$(RunTests)' == 'True'"/>
I used old good psExec :) Everything is work now.

how to run remote program on Powershell for long running program

I need to run a long running program on a remote computer. Currently the exe runs as long as the PS session exists. if the session is removed the exe stops. How can i have the exe running even if the PS session is lost
$mmoVMTemplate = "Machine"
for($i=1;$i -le 80; $i++)
{
$mmoVM = $mmoVMTemplate + $i
.\InstallWinRMCertAzureVM.ps1 -SubscriptionName 'my subscription' -ServiceName $mmoVM -Name $mmoVM
$secPassword = ConvertTo-SecureString 'password' -AsPlainText -Force
$credential = New-Object System.Management.Automation.PSCredential('username', $secPassword)
$uri = Get-AzureWinRMUri -ServiceName $mmoVM -Name $mmoVM
$session = New-PSSession -ConnectionUri $uri -Credential $credential
Invoke-Command -Session $session -ScriptBlock {Set-Location "C:\MyProgram\"
start-process -FilePath 'C:\MyProgram\TestTool.exe'}
Disconnect-PSSession -Session $session
Write-Host $mmoVM
}
Use the parameter -Wait by start-process -FilePath 'C:\MyProgram\TestTool.exe' -Wait.
So the invoke comamnd waits until the TestTool.exe is finished.