Invoke-Command AsJob parameter set not working - powershell

I am able to do
Invoke-Command -ScriptBlock { Z:\prog.bat }
However, when I do
Invoke-Command -ScriptBlock { Z:\prog.bat } -AsJob
I keep getting
Invoke-Command : Parameter set cannot be resolved using the specified named parameters.
At line:1 char:1
+ Invoke-Command -ScriptBlock { z:\prog.bat } - ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Invoke-Command], ParameterBindingException
+ FullyQualifiedErrorId : AmbiguousParameterSet,Microsoft.PowerShell.Commands.InvokeCommandCommand
My intention is to run Z:\Prog.bat in the background since I will be executing this thru Ansible

Per the comments, this error message is telling you that it's not possible for PowerShell to know which Parameter Set you are trying to use. A Parameter Set is a collection of Parameters that are used together, some mandatory and some optional. Some cmdlets have a single set, some cmdlets have different combinations allowing them to be used in different ways.
You are using -ScriptBlock and -AsJob. Invoke-Command has quite a large number of Parameter Sets and to make your call of these parameters unique you need to use them with one of these parameters:
-Session
-ComputerName
-ConnectionUri
-VMId
-VMName
-ContainerID
E.g:
Invoke-Command -ScriptBlock { Z:\prog.bat } -AsJob -Computername SomeComputer
Alternatively if you're just attempting to run a script block as a background job on your local machine, don't use Invoke-Command instead consider using Start-Job:
Start-Job { Z:\prog.bat }

Related

retrieve vhdx path from a remote server

hi i' m trying to retrieve the vhdx of specified vmname from the remote host server
here part of the script
$pth="C:\path\resize-vm"
$list=gc $pth\list-host.txt
foreach ($hostserver in $list) {
$vm=(Invoke-Command -ComputerName $hostserver -ScriptBlock {Get-VM}).VMName
Write-Host -NoNewline " here the vm installed in " $hostserver `r`n $vm
$vmname=Read-Host -Prompt "please chose a vmname to resize "
#the issue in the last line
$pathvhd=Invoke-Command -ComputerName $hostserver -ScriptBlock {(Get-VMHardDiskDrive -VMName $vmname).path}
When I launch this command $vmname="dc-kozhan"
I am getting this error
Cannot validate argument on parameter 'VMName'. The argument is null
or empty. Provide an argument that is not null or empty, and then try
the command again.
+ CategoryInfo : InvalidData: (:) [Get-VMHardDiskDrive], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.HyperV.PowerShell.Commands.GetVMHardDiskDrive
Command
+ PSComputerName : h-uludag-3
But when I specified dc-kozhan literally it work
PS C:\Users> Invoke-Command -ComputerName $hostserver -ScriptBlock {(Get-VMHardDiskDrive -VMName "DC-KOZAHAN")
.path}
V:\DC-KOZAHAN\DC-KOZAHAN-SYSTEM.vhdx
V:\DC-KOZAHAN\DC-KOZAHAN-DIRECTORY.vhdx
V:\DC-KOZAHAN\DC-KOZAHAN-SYSVOL.vhdx
V:\DC-KOZAHAN\DC-KOZAHAN-BACKUP.vhdx
does anyone have an idea why it does not work when it's specified in a variable
You need to understand how to pass arguments inside the scriptblock. The scope is different when you try to pass the value inside the scriptblock. As a result, $VMname is becoming null in your first statement.
Kindly change your existing Invoke-command statement to the below one:
Invoke-Command -ComputerName $hostserver -ScriptBlock {Param([string]$vmname)(Get-VMHardDiskDrive -VMName $vmname)} -ArgumentList $vmname
Also, my suggestion for you to read about the argumentlist in powershell in case invoke-command
Hope it helps.

How to use variable as an argument for command in "Invoke-Expression" [duplicate]

This question already has answers here:
How do I pass a local variable to a remote `Invoke-Command`? [duplicate]
(2 answers)
Closed 3 years ago.
I'm getting the error
Cannot bind argument to parameter 'Command' because it is null.
+ CategoryInfo : InvalidData: (:) [Invoke-Expression], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.InvokeExpressionCommand
+ PSComputerName : X
when I try to use
$command = "cmd.exe /c $($currentLocation)/Ping.cmd"
Invoke-Command -ComputerName $systemName -credential $credentials -ScriptBlock {Invoke-Expression -Command: $command }
How do I use a variable like this? I have this as a requirement so I can't use it directly, it needs to be dynamic.
You can either use the -ArgumentList parameter with Invoke-Command or access your variable using $using: like this:
Invoke-Command -ComputerName $systemName -credential $credentials -ScriptBlock {
Invoke-Expression -Command: $using:command
}

powershell script is not working [duplicate]

I am trying to follow this article to expand a variable in a scriptblock
My code tries this:
$exe = "setup.exe"
invoke-command -ComputerName $j -Credential $credentials -ScriptBlock {cmd /c 'C:\share\[scriptblock]::Create($exe)'}
How to fix the error:
The filename, directory name, or volume label syntax is incorrect.
+ CategoryInfo : NotSpecified: (The filename, d...x is incorrect.:String) [], RemoteException
+ FullyQualifiedErrorId : NativeCommandError
+ PSComputerName : remote_computer
You definitely don't need to create a new script block for this scenario, see Bruce's comment at the bottom of the linked article for some good reasons why you shouldn't.
Bruce mentions passing parameters to a script block and that works well in this scenario:
$exe = 'setup.exe'
invoke-command -ComputerName $j -Credential $credentials -ScriptBlock { param($exe) & "C:\share\$exe" } -ArgumentList $exe
In PowerShell V3, there is an even easier way to pass parameters via Invoke-Command:
$exe = 'setup.exe'
invoke-command -ComputerName $j -Credential $credentials -ScriptBlock { & "C:\share\$using:exe" }
Note that PowerShell runs exe files just fine, there's usually no reason to run cmd first.
To follow the article, you want to make sure to leverage PowerShell's ability to expand variables in a string and then use [ScriptBlock]::Create() which takes a string to create a new ScriptBlock. What you are currently attempting is to generate a ScriptBlock within a ScriptBlock, which isn't going to work. It should look a little more like this:
$exe = 'setup.exe'
# The below line should expand the variable as needed
[String]$cmd = "cmd /c 'C:\share\$exe'"
# The below line creates the script block to pass in Invoke-Command
[ScriptBlock]$sb = [ScriptBlock]::Create($cmd)
Invoke-Command -ComputerName $j -Credential $credentials -ScriptBlock $sb

Powershell Quoted variables parameters in a job

$buildDef = "Service.xxxx"
$buildDefFull="MyProject/$buildDef"
Start-Job -Name 'Service1' -ScriptBlock { tfsbuild start /collection:"http://yyyy:8080/tfs/DefaultCollection" /builddefinition:"$buildDefFull" }
i get this error:
Option builddefinition requires a value.
+ CategoryInfo : NotSpecified: (Option builddefinition requires a value.:String) [], RemoteException
+ FullyQualifiedErrorId : NativeCommandError
+ PSComputerName : localhost
i can't seem to get tfsbuild to accept the parameter in a start job... it actually runs fine if i just do the tfsbuild part with no job.
Any ideas how i'm supposed to pass that value?
tx
The $buildDefFull variable is outside the scope of the scriptblock.
You have 2 options:
PowerShell 3+
Use the Using scope modifier:
$buildDef = "Service.xxxx"
$buildDefFull="MyProject/$buildDef"
Start-Job -Name 'Service1' -ScriptBlock { tfsbuild start /collection:"http://yyyy:8080/tfs/DefaultCollection" /builddefinition:"$Using:buildDefFull" }
Any Version
Define and pass parameters to the scriptblock:
$buildDef = "Service.xxxx"
$buildDefFull="MyProject/$buildDef"
Start-Job -Name 'Service1' -ScriptBlock { param($bdf) tfsbuild start /collection:"http://yyyy:8080/tfs/DefaultCollection" /builddefinition:"$bdf" } -ArgumentList $buildDefFull

powershell Start-Service fails

I have very simple powershell script that starts service remotely.
Invoke-Command -Session $session -ScriptBlock { Start-Service "My test service v1" }
works fine but
$myval="My test service v1"
Invoke-Command -Session $session -ScriptBlock { Start-Service $myval }
fails with
Cannot validate argument on parameter 'InputObject'. The argument is
null or empty. Supply an argument that is not null or empty and then
try the command again.
+ CategoryInfo : InvalidData: (:) [Start-Service], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.PowerShell.Commands.StartServiceCommand
+ PSComputerName : mdfiletest
To me they are the same. Why is this not working? thanks
It does not work because when the scriptblock is executed on the remote server, the variable $myval does not exist in session state; it only exists on the local (client) side. The powershell v2/v3 compatible way to do this is:
invoke-command -session $session -scriptblock {
param($val); start-service $val } -args $myval
Another powershell v3 (only) way is like this:
invoke-command -session $session -scriptblock { start-service $using:myval }
The $using prefix is a special pseudo-scope which will capture the local variable and try to serialize it and send it remotely. Strings are always serializable (remotable.)