Powershell: Null Array Exception in passing args to Remote Session - powershell

I am trying to run a command on a remote session but I am getting a
Cannot index into a null array.
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : NullArray
+ PSComputerName : serverName
I have checked all the arguments that I am passing to the remote session and none of them are null. Following is the command:
Invoke-Command -Session $session -ScriptBlock {Start-Process $args[0] -ArgumentList $args[1] -RedirectStandardOutput $args[2] -RedirectStandardError $agrs[3]} -Args $Consoledir,$arguments,$stdOutLog,$stdErrLog;
I am using this command to run a console application that is passed in the $dir argument (D:\Temp\console.exe). This application further takes some arguments that are passed in the $arguments parameter.

Try this:
Invoke-Command -Session $session -ScriptBlock {
Param($dir, $args, $outlog, $errlog)
Start-Process $dir -ArgumentList $args -RedirectStandardOutput $outlog -RedirectStandardError $errlog
} -ArgumentList $Consoledir,$arguments,$stdOutLog,$stdErrLog;

Related

starting exe with two param with powershell

I need to create a powershell script that launch an exe file which also has two parameters, I want to put it in a group policy, so that it starts at the power on of each computer.
I tried this command:
$Username = 'user'
$Password = 'pass'
$pass = ConvertTo-SecureString -AsPlainText $Password -Force
$Cred = New-Object System.Management.Automation.PSCredential -ArgumentList $Username, $pass
invoke-command -Credential $Cred -ScriptBlock {& 'C:\myfile.exe' --param1 value --param2}
It tells me
Invoke-Command : Impossibile risolvere il set di parametri utilizzando i parametri denominati specificati.
In riga:1 car:1
+ invoke-command -Credential $Cred -ScriptBlock { & 'C:\Program Files ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Invoke-Command], ParameterBindingException
+ FullyQualifiedErrorId : AmbiguousParameterSet,Microsoft.PowerShell.Commands.InvokeCommandCommand
I also tried this:
invoke-command -Credential $Cred -ScriptBlock {Start-Process -FilePath 'C:\myfile.exe' -ArgumentList "--param1 value", "-psb4"}
but the error that appears is the same.

Pass variable with Invoke-SSHCommand in Powershell

I have a PowerShell script that attempts to invoke bash shell script on a remote Linux machine. I use -ArgumentList to pass a parameter but get an error.
Sample om my script:
param ( [Parameter(mandatory=true)[string]$param1 )
Remove-SshSession
$session = New-SshSession -ComputerName $myServer -Command $StopServer
$result = Invoke-SshCommand -ComputerName $myServer -ScriptBlock {/root/scripts/Test.sh $args[0]} -ArgumentList $param1
The error:
Invoke-SshCommand : A Parameter cannot be found that matches parameter name 'ArgumentList'. At C:\TestProject\InstallBuild.ps1:6
+ ... root/test.sh $args[0]} -ArgumentList $param1...
+
+ CategogyInnfo : InvalidArgument: (:) [Invoke-SshCommand], ParameterBindingException
+ FullyQualifiedErrorId : NamedParameterNotFound,Invoke-SshCommand
How do I pass an argument to -ScriptBlock with Invoke-SshCommand?

Problem with enter-pssession: "A positional parameter cannot be found that accepts argument"

I'm trying to set up PS Remoting/Win-RM and have the following:
$primary = 'server1'
$user = $env:UserName
$admUser = Get-Credential -UserName "domain\adm-$user" -Message 'Enter your *ADMIN* password:'
Enter-PSSession -ComputerName $primary -Credential $admUser {
hostname
}
However, this is returning the following error (I've tested this from a console and it works, so there's something wrong with my script):
Windows PowerShell credential request.
Enter your *ADMIN* password:
Password for user domain\adm-user1: ***************
Enter-PSSession : A positional parameter cannot be found that accepts argument '
hostname
'.
At line:5 char:1
+ Enter-PSSession -ComputerName $primary -Credential $admUser {
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Enter-PSSession], ParameterBindingException
+ FullyQualifiedErrorId :PositionalParameterNotFound,Microsoft.PowerShell.Commands.EnterPSSessionCommand
What am I doing wrong? Thanks in advance!
Enter-PSSession opens a console connection to a remote system.
If you want to run a command on a remote system, you'd want to use Invoke-Command.
Also, you 100% always need to pass scriptblocks into some parameter (minus some edge cases where it's accepted by default). In this case, we need to use -Scriptblock.
Example:
Invoke-Command -ComputerName $primary -Credential $admUser -ScriptBlock {
hostname
}
or
$session = New-PSSession -ComputerName $primary -Credential $admUser
Invoke-Command -Session $session -ScriptBlock {hostname}
Invoke-Command -Session $session -ScriptBlock {$env:USERNAME}
Disconnect-PSSession -Session $session

Invoke-Command with parameters as a job

I can run the below command without any problem:
Invoke-Command -ScriptBlock {
Param($rgName,$VMname)
Get-AzureRmVM -Name $VMname -ResourceGroupName $rgName
} -ArgumentList $rgname,$vmname
But what I really need is to be able to run the command as a job, so I tried the below:
Invoke-Command -ScriptBlock {
Param($rgName,$VMname)
Get-AzureRmVM -Name $VMname -ResourceGroupName $rgName
} -ArgumentList $rgname,$vmname -AsJob
And I'm receiving the following error:
Invoke-Command : Parameter set cannot be resolved using the specified named
parameters.
At line:1 char:1
+ Invoke-Command -ScriptBlock { param($rgName,$VMname) Get-AzureRmVM -N ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Invoke-Command], ParameterBindingException
+ FullyQualifiedErrorId : AmbiguousParameterSet,Microsoft.PowerShell.Commands.InvokeCommandCommand
I've also tried to run the command using Start-Job instead but I'm also receiving errors.
You have to pass the argumentlist as an array
[Array]$ArgumentList = #('rg','vm')
[ScriptBlock]$ScriptBlock = {
Param(
[string]$rgName,
[string]$VMname
)
Get-AzureRmVM -Name $VMname -ResourceGroupName $rgName
}
$nj = Invoke-Command -ScriptBlock $ScriptBlock -ArgumentList $ArgumentList -ComputerName $env:COMPUTERNAME -Credential (Get-Credential) -JobName 'NewJobName' |Wait-Job
Interestingly, I found that I had to specify -ComputerName [-Credntial] to get this to work - modified above.
Get the results of the job as follows...
$nj | Receive-Job

Powershell Start-Process permission denied

Could anyone tell me, why do I get an Error if I invoke:
$log = $env:TEMP + "\File.log"
$stdErrLog = $env:TEMP + "\stderr.log"
$stdOutLog = $env:TEMP + "\stdout.log"
$argument = "Get-Childitem c:\"
Start-Process powershell.exe -Credential $credentials -WindowStyle hidden -WorkingDirectory c:\ -ArgumentList $argument -RedirectStandardOutput $stdOutLog -RedirectStandardError $stdErrLog -wait
but there is no Error if I invoke:
Start-Process powershell.exe -Credential $credentials -WindowStyle hidden -WorkingDirectory c:\ -ArgumentList $argument -RedirectStandardOutput $stdOutLog -RedirectStandardError $stdErrLog
But I need the "-wait" or is there any alternative?
Error:
Start-Process : Permission denied
In Zeile:1 Zeichen:1
+ Start-Process powershell.exe -Credential $cred -WorkingDirectory c:\ -ArgumentLi ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Start-Process], Win32Exception
+ FullyQualifiedErrorId : System.ComponentModel.Win32Exception,Microsoft.PowerShell.Commands.StartProcessCommand
Sounds like your current user doesn't have access to see processes running as the 2nd account. Launch a PowerShell window under the other account, then run the script below as yourself to see if it's visible. If not, you may need to elevate permissions or run as admin.
Get-Process powershell | select -ExpandProperty startinfo | select -expandproperty environmentvariables | ?{$_.Name -eq USERNAME}